This library provides an alternate interface to MPFR (compared to math/bigfloat), emphasizing the emulation of floating-point
formats such as binary128, binary16, bfloat16, etc.
While the math/bigfloat
interface is sufficient for most high-precision computing, it is lacking in a couple areas. Mainly, it does not
properly emulate subnormal arithmetic
allow the exponent range to be changed
Normally, neither of these problems cause concern. For example, if a user intends to find an approximate value
for some computation on the reals, then subnormal arithmetic or a narrower exponent range is not particular useful.
However, if a user wants to know the result of a computation specifically in some format, say half-precision, then
At half-precision, (exp -10) and (exp 20) evaluate to 4.5419e-05 and +inf.0, respectively. On the other hand,
evaluating (bfexp (bf -10)) and (bfexp (bf -10)) with (bf-precision 11) returns (bf "4.5389e-5") and
(bf "#e4.8523e8"). While the latter results are certainly more accurate, they do not reflect proper behavior in half-precision.
The standard bigfloat library does not subnormalize the first result (no subnormal arithmetic), nor does it recognize the overflow in the
second result (fixed exponent range).
This library fixes the issues mentioned above by automatically emulating subnormal arithmetic when
necessary and providing a way to change the exponent range. In addition, the interface is quite
similar to math/bigfloat, so it will feel familiar to anyone who has used the standard bigfloat library
before. There are also a few extra operations from the C math library such as gflfma, gflmod, and
gflremainder that the bigfloat library does not support.
See math/bigfloat for more information on bigfloats.
2 Type and Constructors
A generic flonum is a wrapper that stores a bigfloat and the significand and exponent size
when it was initialized.
Returns #t if v is a generic-flonum.
Constructs a generic-flonum from a string or a real number.
Returns a generic-flonum with the same value as x except rounded at the current
precision and rounding mode.
Converts a real v to a generic-flonum x and back.
Converts a bigfloat v to a generic-flonum x and back.
Converts a string s to a generic-flonum x and back.
The standard bigfloat library defines (bf-precision) and (bf-rounding-mode) to control
significand size and the rounding of results. The following parameters are similar and can be used at the
same time as their bigfloat counterparts, although this behavior may change in the future.
A parameter that defines the current exponent size of values returned from most functions
in this library. Default value is the exponent size of a flonum.
A parameter that defines the current sum of the exponent size and significand size of
values returned from most functions in this library. More concisely, it defines the significand
size indirectly. The significand size corresponds to (bf-precision)
and is equal to
(- (gfl-bits) (gfl-exponent))
. Default value is the length of a flonum in bits.
A parameter that determines the mode used to round the result of most functions in this library.
Note that (bf-rounding-mode)
accepts all values except 'away
Approximations of π, φ, γ, G, and log(2).
Returns #t if x is a subnormal value.
Standard comparators corresponding to =
, and <=
. Infinities are larger or smaller than any
other value, and comparing to +nan.gfl
always returns #f
7 Mathematical Operations
Standard arithmetic functions, corresponding to +
. Similar to bf/
, division by zero returns
When giving more than two arguments, gfl+ and gfl- compute the answer without
any intermediate rounding.
Returns the maximum and minimum of their arguments, respectively. When given no arguments, bfmin
, and bfmax
Returns the modulo and remainder of x and y.
Returns the arithmetic-geometric mean of x and y.
Computes (gfl+ (gfl* x y) z)
without intermediate overflow or rounding.
Returns the square root, the reciprocal square root, and the cube root of x.
Returns the n-th root of x. n must be a nonnegative fixnum.
Returns the log of x in base e, 2, and 10.
Returns the exponential of x in base e, 2, and 10.
Function corresponding to expt
Standard trigonmetric functions and their inverses.
Standard hyperbolic functions and their inverses.
Standard reciprocal trigonometric functions.
Standard reciprocal hyperbolic functions.
Simultaneously computes the sine and cosine of x.
Simultaneously computes the hyperbolic sine and cosine of x.
Compute the gamma, log-gamma, and digamma function.
Compute the error function and complementary error function.
Returns the exponetial integral of x.
Returns the dilogarithm of x.
Computes the Riemann zeta function.
Compute the Bessel functions. The first three correspond to Bessel functions of
the first kind of order 0, 1, and n, while the other three correspond to
Bessel functions of the second kind of order 0, 1, and n.
Computes the Airy function of the first kind.
Returns a generic-flonum with the magnitude of x and the sign of y.