Learn R Programming

round (version 0.21-0.2)

mathC99: C99 'math' Library Functions (where Not in Standard aka 'Base' R)

Description

Provides simple R versions of those C99 “math lib” / “libmath” / “libm” functions that are not (yet) in standard (aka ‘base’ R).

Usage

logB(x) # C's logb(x), numeric integer-valued "log2".
         # R's logb() is defined as "log wrt base"
ilogb(x) # == logB(), but of *type* integer

fpclassify(x) isnormal(x) nearbyint(x) signbit(x)

nextafter(x, y) nexttoward(x, y)

Value

a numeric (double or

integer) vector of the same (or recycled) length of

x (and y where appropriate) with the values of

<fn>(x) for the corresponding C99 libmath function <fn>.

Arguments

x, y

numeric vector(s); will be recycled to common length.

Author

Martin Maechler

References

Wikipedia (2020) C mathematical functions https://en.wikipedia.org/wiki/C_mathematical_functions

See Also

Examples

Run this code
x <- (1:20)*pi
stopifnot(ilogb (x) == logB (x),  is.integer(ilogb(x)),
          ilogb(-x) == logB(-x),  is.double ( logB(x)))
cbind(x, "2^il(x)"= 2^logB(x), ilogb = ilogb(x), signbit = signbit(x),
         fpclassify = fpclassify(x), isnormal = isnormal(x))

x <- c(2^-(10:22), rexp(1000));
summary(x / 2^ilogb(x)) # in   [1, 2)  interval
stopifnot(nearbyint(x) == round(x))

nextafter(-0, +0)
nextafter(+0,  1)
nextafter(+0, -1)
nextafter(Inf, -1)
nextafter(-Inf, 0)

Run the code above in your browser using DataLab