'round2' works like 'round' but the rounding has base 2 under consideration so that bits (binary digits) beyond a certain theshold are zeroed.

round2(x, digits10 = 0)

Arguments

x

Same as round.

digits10

Same as digits in round. The "10" is to emphasize the usual base 10 used by humans.

Details

round2() is intended to allow reliable and safe for == comparisons provided both sides have the function applied to the same value of digits10. Internally a numeric has its binary representation (bits) past a certain point set to all 0s, while retaining a certain degree of accuracy. Algorithmically, x is multiplied by 2^exponent and then rounded, and then divided by 2^exponent. The value of exponent is approximately 3 * digits10 when digits10 is positive. If digits10 is negative then what is returned is round(x, digits10). The value of exponent guarantees that x has been rounded to at least digits10 decimal places (often around digits10 + 1 for safety).

Value

Something similar to round.

Author

T. W. Yee.

See also

Examples

set.seed(1); x <- sort(rcauchy(10))
x3 <- round2(x, 3)
x3 == round2(x, 3)  # Supposed to be reliable (all TRUE)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
rbind(x, x3)  # Comparison
#>         [,1]      [,2]      [,3]       [,4]       [,5]       [,6]      [,7]
#> x  -4.292626 -2.328624 -1.808243 -0.3305220 -0.2966426 -0.1755794 0.1965824
#> x3 -4.292480 -2.328613 -1.808105 -0.3305664 -0.2968750 -0.1757812 0.1967773
#>         [,8]     [,9]    [,10]
#> x  0.7346469 1.102520 2.353831
#> x3 0.7348633 1.102539 2.354004
(x3[1]  * 2^(0:9)) / 2^(0:9)
#>  [1] -4.29248 -4.29248 -4.29248 -4.29248 -4.29248 -4.29248 -4.29248 -4.29248
#>  [9] -4.29248 -4.29248
print((x3[1]  * 2^(0:11)), digits = 14)
#>  [1]    -4.29248046875    -8.58496093750   -17.16992187500   -34.33984375000
#>  [5]   -68.67968750000  -137.35937500000  -274.71875000000  -549.43750000000
#>  [9] -1098.87500000000 -2197.75000000000 -4395.50000000000 -8791.00000000000

# Round to approx 1 d.p.
x1 <- round2(x, 1)
x1 == round2(x, 1)  # Supposed to be reliable (all TRUE)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
rbind(x, x1)
#>         [,1]      [,2]      [,3]      [,4]       [,5]       [,6]      [,7]
#> x  -4.292626 -2.328624 -1.808243 -0.330522 -0.2966426 -0.1755794 0.1965824
#> x1 -4.281250 -2.343750 -1.812500 -0.343750 -0.2812500 -0.1875000 0.1875000
#>         [,8]    [,9]    [,10]
#> x  0.7346469 1.10252 2.353831
#> x1 0.7500000 1.09375 2.343750
x1[8] == 0.75  # 3/4
#> [1] TRUE
print((x1[1]  * 2^(0:11)), digits = 9)
#>  [1]    -4.28125    -8.56250   -17.12500   -34.25000   -68.50000  -137.00000
#>  [7]  -274.00000  -548.00000 -1096.00000 -2192.00000 -4384.00000 -8768.00000
seq(31) / 32
#>  [1] 0.03125 0.06250 0.09375 0.12500 0.15625 0.18750 0.21875 0.25000 0.28125
#> [10] 0.31250 0.34375 0.37500 0.40625 0.43750 0.46875 0.50000 0.53125 0.56250
#> [19] 0.59375 0.62500 0.65625 0.68750 0.71875 0.75000 0.78125 0.81250 0.84375
#> [28] 0.87500 0.90625 0.93750 0.96875