Version 20 (modified by cowan, 12 months ago) (diff)



Flonums are a subset of the inexact real numbers provided by a Scheme implementation. In most Schemes, the flonums and the inexact reals are the same.


Flonum arithmetic is already supported by many systems, mainly to remove type-dispatching overhead. Standardizing flonum arithmetic increases the portability of code that uses it. Standardizing the range or precision of flonums would make flonum operations inefficient on some systems, which would defeat their purpose. Therefore, this SRFI specifies some of the semantics of flonums, but makes the range and precision implementation-dependent.

The sources of the procedures in this SRFI are R7RS-small, SRFI 141, the R6RS flonum library, and the C99/Posix library <math.h>, which should be available directly or indirectly to Scheme implementers. (The C90 version of <math.h> lacks arcsinh, arccosh, arctanh, erf, and tgamma.)

Scheme implementations that use IEEE 754 floating point numbers should follow the specifications of that standard.


It is required that if two flonums are equal in the sense of =, they are also equal in the sense of eqv?. That is, if 12.0f0 is a 32-bit inexact number, and 12.0 is a 64-bit inexact number, they cannot both be flonums. In this situation, it is recommended that the 64-bit numbers be flonums.

It is an error, except as otherwise noted, for an argument not to be a flonum. If the mathematically correct result is a non-real number, the result is +nan.0 if the implementation supports that number, or an arbitrary flonum if not.

Flonum operations must be at least as accurate as their generic counterparts applied to flonum arguments. In some cases, operations should be more accurate than their naive generic expansions because they have a smaller total roundoff error.

This SRFI uses x, y, z as parameter names for flonum arguments, and ix as a name for an integer-valued flonum argument, i.e., a flonum for which the integer? predicate returns true.


The following (mostly C99) constants are provided as Scheme variables.


Bound to the value of the mathematical constant e. (C99 M_E)


Bound to the value of (fllog fl-e 2.0). (C99 M_LOG2E)


Bound to the value of (fllog fl-e 10.0). (C99 M_LOG10E)


Bound to the value of (fllog 2.0) (C99 M_LN2)


Bound to the value of (fllog 10.0) (C99 M_LN10)


Bound to the value of the mathematical constant π. (C99 M_PI)


Bound to the value of (fl/ fl-pi 2.0) (C99 M_PI_2)


Bound to the value of (fl/ fl-pi 4.0) (C99 M_PI_4)


Bound to the value of (fl/ 1.0 fl-pi). (C99 M_1_PI)


Bound to the value of (fl/ 2.0 fl-pi). (C99 M_2_PI)


Bound to the value of (fl/ 2.0 (flsqrt fl-pi)). (C99 M_2_SQRTPI)


Bound to the value of (flsqrt 2.0). (C99 M_SQRT2)


Bound to the value of (fl/ 1,0 (flsqrt 2.0)). (C99 M_SQRT1_2)


Bound to the value of the largest finite flonum.


Bound to #t if (fl+* x y z) is known to be faster than (fl+ (fl* x y) z), or #f otherwise. (C99 FP_FAST_FMA`)


Bound to whatever value is returned by (flinteger-binary-log 0). (C99 FP_ILOGB0)


Bound to whatever value is returned by (flinteger-binary-log +0.nan). (C99 FP_ILOGBNAN)


Value of the floating-point radix (2 on most machines).


(flonum number)

Returns the closest flonum equivalent to number in the sense of = and <.

(fladjacent x y)

Returns a flonum adjacent to x in the direction of y. Specifically: if x < y, returns the smallest flonum larger than x; if x > y, returns the largest flonum smaller than x; if x = y, returns x. (C99 nextafter)

(flcopysign x y)

Returns a flonum whose magnitude is the magnitude of x and whose sign is the sign of y. (C99 copysign)


(flonum? obj)

Returns #t if obj is a flonum and #f otherwise.

(fl= x y z ...)

(fl< x y z ...)

(fl> x y z ...)

(fl<= x y z ...)

(fl>= x y z ...)

These procedures return #t if their arguments are (respectively): equal, monotonically increasing, monotonically decreasing, monotonically nondecreasing, or monotonically nonincreasing; they return #f otherwise. These predicates must be transitive.

(flinteger? x`)‌‌

(flzero? x)

(flpositive? x)

(flnegative? x)

(flodd? ix)

(fleven? ix)

(flfinite? x)

(flinfinite? x)

(flnan? x)

These numerical predicates test a flonum for a particular property, returning #t or #f. The flinteger? procedure tests whether the flonum is an integer, flzero? tests whether it is fl=? to zero, flpositive? tests whether it is greater than zero, flnegative? tests whether it is less than zero, flodd? tests whether it is odd, fleven? tests whether it is even, flfinite? tests whether it is not an infinity and not a NaN, flinfinite? tests whether it is an infinity, and flnan? tests whether it is a NaN.

Note that (flnegative? -0.0) must return #f; otherwise it would lose the correspondence with (fl< -0.0 0.0), which is #f according to IEEE 754.


(fl+ x)

(fl* x)

Return the flonum sum or product of their flonum arguments. In general, they should return the flonum that best approximates the mathematical sum or product.

(fl+* x y z)

Returns (fl+ (fl* x y) z), possibly faster. If the constant fl-fast-fl+* is #f, it will definitely be faster. (C99 fma)

(fl- x y ...)

(fl/ x y ...)

With two or more arguments, these procedures return the difference or quotient of their arguments, associating to the left. With one argument, however, they return the additive or multiplicative inverse of their argument.

In general, they should return the flonum that best approximates the mathematical difference or quotient.

(flmax x ...)

(flmin x ...)

Return the maximum/minimum argument. If there are no arguments, these procedures return +inf.0/-inf.0 if the implementation provides these numbers, and fl-maximum / its negation otherwise.

(flabs x)

Returns the absolute value of x.

(flabsdiff x y)

Returns (flabs (fl- x y)) without internal overflow. (C99 fdim)

(flsgn x)

Returns (flcopy-sign 1.0 x).

(flnumerator x)

(fldenominator x)

Returns the numerator/denominator of x as a flonum; the result is computed as if x was represented as a fraction in lowest terms. The denominator is always positive. The denominator of 0.0 is defined to be 1.0.

(flfloor x)

(flceiling x)

(flround x)

(fltruncate x)

These procedures return integral flonums for flonum arguments that are not infinities or NaNs. For such arguments, flfloor returns the largest integral flonum not larger than x. The flceiling procedure returns the smallest integral flonum not smaller than x. The fltruncate procedure returns the integral flonum closest to x whose absolute value is not larger than the absolute value of x. The flround procedure returns the closest integral flonum to x, rounding to even when x represents a number halfway between two integers.

Although infinities and NaNs are not integers, these procedures return an infinity when given an infinity as an argument, and a NaN when given a NaN.

Exponents and logarithms

flexp	double exp(double)	ex
fl2	double exp2(double)	base-2 exponential
flexp-minus-1	double expm1(double)	exp-1
flcbrt	double cbrt(double)	cube root
flhypot	double hypot(double, double)	sqrt(x2+y2)
fl2exp	double ldexp(double x, int n)	x*2n
flexponent	double logb(double x)	the exponent of x,
which is the integral part of
log_r(|x|), as a signed floating-point value, for non-zero x,
where r is the radix of the machine's floating-point arithmetic
flfraction-exponent	double modf(double, double *
returns two values, fraction and int exponent
flinteger-exponent	int ilogb(double)	binary log as int
fllog2	double log2(double)	log base 2
fllog10	double log10(double)	log base 10
fllog+1	double log1p(double x)	log (1+x)
flnormalized-fraction-exponent	double frexp(double, int *)
returns two values, fraction in range [1/2,1) and int exponent
flscalbn	double scalbn(double x, int y)	x*ry,
where r is the machine float radix

The flsqrt procedure returns the principal square root of x. For -0.0, flsqrt should return -0.0.

The flexpt procedure returns x raised to the power y if x is non-negative or y is an integral flonum. If x is negative and y is not an integer, the result may be a NaN, or may be some unspecified flonum. If x is zero, then the result is zero.

Trigonometric functions

(flsin x) (C99 sin)

(flcos x) (C99 cos)

(fltan x) (C99 tan)

(flasin x) (C99 asin)

(flacos x) (C99 acos)

(flatan x [y]) (C99 atan and atan2)

(flsinh x) (C99 sinh)

(flcosh x) (C99 cosh)

(fltanh x) (C99 tanh)

(flasinh x) (C99 asinh)

(flacosh x) (C99 acosh)

(flatanh x) (C99 atanh)

These are the usual trigonometric functions. The flatan function, when passed two arguments, returns (flatan (/ y x)) without requiring the use of complex numbers.

(flremquo x y)

Returns two values, the result of (flround-remainder x y) and the low-order n bits (as a correctly signed exact integer) of the rounded quotient. The value of n is implementation-dependent but at least 3. This function can be used to reduce the argument of the inverse trigonometric functions, while preserving the correct quadrant or octant.

Integer division

The following procedures are the flonum counterparts of procedures from SRFI 141:

flfloor/ flfloor-quotient flfloor-remainder
flceiling/ flceiling-quotient flceiling-remainder
fltruncate/ fltruncate-quotient fltruncate-remainder
flround/ flround-quotient flround-remainder
fleuclidean/ fleuclidean-quotient fleuclidean-remainder
flbalanced/ flbalanced-quotient flbalanced-remainder

They have the same arguments and semantics as their generic counterparts, except that it is an error if the arguments are not flonums.

Special functions

(flgamma x)

Returns Γ(x), the gamma function applied to x. This is equal to (x-1)! for integers. (C99 tgamma)

(flloggamma x)

Returns two values, log Γ(|x|) without internal overflow, and sgn(Γ(x)). (C99 lgamma)

(first-bessel x n)

Returns the nth order Bessel function of the first kind applied to x, Jn(x). (C99 j0, j1, jn)

(second-bessel x n)

Returns the nth order Bessel function of the second kind applied to x, Yn(x). (C99 y0, y1, yn)

(erf x)

Returns the error function erf(x). (C99 erf)

(erfc x)

Returns the complementary error function, (- 1 (erf x)). (C99 erfc)