wiki:FlonumsCowan

Version 8 (modified by cowan, 5 years ago) (diff)

--

Flonums package

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.

The procedures in this package don't have hard-coded prefixes. The intent is that they be placed in a module. Users can then import this module with their own prefix such as fl, or ƒ if their Scheme implementation supports that character, or with no prefix at all if the intent is to override the normal Scheme arithmetic routines.

Basic procedures

(flonum? obj)

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

(real->flonum x)

Returns the best flonum representation of x.

R7RS-small-compatible procedures

The following R7RS-small procedures have flonum equivalents:

= < <= > >= 
integer? zero? positive? negative? odd? even? finite? infinite? nan?
+ - * /
abs numerator denominator floor ceiling truncate round
log sin cos tan asin acos atan
sqrt expt

TODO: need to see about integer division procedures

C99 <math.h> constants

The following constants are defined in terms of the constants of the <math.h> header of ISO/IEC 9899:1999 (C language).

Scheme nameC nameComments
eM_EValue of e
log2-eM_LOG2EValue of log2 e
log10-eM_LOG10EValue of log10 e
ln-2M_LN2Value of loge 2
ln-10M_LN10Value of loge 10
piM_PIValue of pi
pi/2M_PI_2Value of pi/2
pi/4M_PI_4Value of pi/4
one-over-piM_1_PIValue of 1/pi
two-over-piM_2_PIValue of 2/pi
two-over-sqrt-piM_2_SQRTPIValue of 2/sqrt(pi)
sqrt-2M_SQRT2Value of sqrt(2)
one-over-sqrt-2M_SQRT1_2Value of 1/sqrt(2)
maximum-flonumHUGE_VAL+inf.0 or else
the largest finite flonum
fast-multiply-add#t if FP_FAST_FMA is 1,
or #f otherwise
multiply-add is fast
integer-exponent-zeroFP_ILOGB0what (integer-binary-log 0) returns
integer-exponent-nanFP_ILOGBNANwhat (integer-binary-log +0.nan) returns

C99 <math.h> procedures

The following procedures are defined in terms of the functions of the <math.h> header of ISO/IEC 9899:1999 (C language). In the C signatures, the types "double" and "int" are mapped to Scheme flonums and (suitably bounded) Scheme exact integers respectively.

Scheme nameC signatureComments
acoshdouble acosh(double)hyperbolic arc cosine
asinhdouble asinh(double)hyperbolic arc sine
atanhdouble atanh(double)hyperbolic arc tangent
cbrtdouble cbrt(double);cube root
complementary-error-functiondouble erfc(double)-
copy-signdouble copysign(double x, double y)result has magnitude of x and sign of y
coshdouble cosh(double)hyperbolic cosine
make-flonumdouble ldexp(double x, int n)x*2n
error-functiondouble erf(double)-
expdouble exp(double)ex
exp-binarydouble exp,,2,,(double)base-2 exponential
exp-minus-1double expm1(double)ex-1
exponentdouble 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
first-bessel-order-0double j0(double)bessel function of the first kind, order 0
first-bessel-order-1double j1(double)bessel function of the first kind, order 1
first-besseldouble jn(int n, double)bessel function of the first kind, order n
fraction-exponentdouble modf(double, double *)returns two values, fraction and int exponent
gammadouble tgamma(double)-
hypotenusedouble hypot(double, double)sqrt(x2+y2)
integer-exponentint ilogb(double)binary log as int
log-binarydouble log2(double)log base 2
log-decimaldouble log10(double)log base 10
log-gammadouble lgamma(double)returns two values, log(|gamma(x)|) and sgn(gamma(x))
log-one-plusdouble log1p(double x)log (1+x)
multiply-adddouble fma(double a, double b, double c)a*b+c
next-afterdouble nextafter(double, double)next flonum following x in the direction of y
normalized-fraction-exponentdouble frexp(double, int *)returns two values, fraction in range [1/2,1) and int exponent
positive-differencedouble fdim(double, double)-
remquodouble remquo(double, double, int *)returns two values, rounded remainder and low-order n bits of the quotient (n is implementation-defined)
scalbndouble scalbn(double x, int y)x*ry, where r is the machine float radix
second-bessel-order-0double y0(double)bessel function of the second kind, order 0
second-bessel-order-1double y1(double)bessel function of the second kind, order 1
second-besseldouble yn(int n, double)bessel function of the second kind, order n
sinhdouble cosh(double)hyperbolic sine
tanhdouble cosh(double)hyperbolic tangent

General remarks

In the event that these operations do not yield a real result for the given arguments, the result may be +nan.0, or may be some unspecified flonum.

Implementations that use IEEE binary floating-point arithmetic should follow the relevant standards for these procedures.

Compnum procedures from <complex.h>

A compnum is a general complex number whose real-part and imag-part are both flonums. The following procedures should be in a different module from the flonum procedures, since they will only be relevant to Schemes that support general complex numbers, and since there are conflicting names.

Scheme nameC signatureComments
absdouble cabs(double complex)same as magnitude
acosdouble complex cacos(double complex)-
acoshdouble complex cacosh(double complex)-
angledouble carg(double complex)-
asindouble complex casin(double complex)-
asinhdouble complex casinh(double complex)-
atandouble complex catan(double complex)-
atanhdouble complex catanh(double complex)-
conjugatedouble complex conj(double complex)complex conjugate
cosdouble complex ccos(double complex)-
coshdouble complex ccosh(double complex)-
expdouble complex cexp(double complex)-
exptdouble complex cpow(double complex, double complex)-
imag-partdouble cimag(double complex)-
logdouble complex clog(double complex)-
magnitudedouble cabs(double complex)same as abs
projectiondouble complex cproj(double complex)projects to Riemann sphere
real-partdouble creal(double complex)-
sindouble complex csin(double complex)-
sinhdouble complex csinh(double complex)-
sqrtdouble complex csqrt(double complex)-
tandouble complex ctan(double complex)-
tanhdouble complex ctanh(double complex)-