[Top] [Prev] [Next]


fp - floating point operations


include "math.m";
math:= load Math Math->PATH;

Infinity, NaN, MachEps, Pi, Degree         : real;
INVAL, ZDIV, OVFL, UNFL, INEX              : int;

getFPcontrol, getFPstatus: fn()            : int;
FPcontrol, FPstatus: fn(r, mask: int)      : int;
ilogb              : fn(x: real)           : int;
scalbn             : fn(x: real, n: int)   : real;
copysign           : fn(x, s: real)        : real;
finite, isnan      : fn(x: real)           : int;
nextafter          : fn(x, y: real)        : real;

fdim, fmin, fmax   : fn(x, y: real)        : real;
fabs               : fn(x: real)           : real;
ceil, floor        : fn(x: real)           : real;
remainder          : fn(x, p: real)        : real;
fmod               : fn(x, y: real)        : real;
modf               : fn(x: real)           :(int, real);
rint               : fn(x: real)           : real;


These constants and functions provide control over rounding modes, exceptions, and other properties of floating point arithmetic.
Infinity and NaN

Infinity and NaN are constants containing the positive infinity and quiet not-a-number values of the IEEE binary floating point standard, double precision.


The constant MachEps is 2-52, the smallest e such that 1+e is not equal to 1.


Pi is the nearest machine number to the infinitely precise value.


Degree is Pi/180.

FPcontrol (r, mask) and

FPstatus (r, mask)

Each thread has a floating point control word (governing rounding mode and whether a particular floating point exception causes a trap) and a floating point status word (storing accumulated exception flags). The functions FPcontrol and status copy bits to the control or status word, in positions specified by mask, returning the previous values of the bits specified in the mask.

getFPcontrol ( ) and getFPstatus ( )

The functions getFPcontrol and getFPstatus return the words unchanged.

Constants for non-overlapping single-bit masks are provided for use in arguments or return values. They stand for the five IEEE exceptions:

invalid operation

0/0, 0+NaN, Infinity-Infinity, or sqrt(-1)


division by zero











By default, INEX is quiet, OVFL, UNFL, and ZDIV are fatal, and rounding (details on rounding given shortly) is to nearest even. Limbo modules are entitled to assume this, and if they wish to use quiet underflow, overflow, or zero-divide, they must either set and restore the control register or clearly document that their callers must do so.

Constants (distinct bit patterns) are defined for interfacing with the floating point control word.

round to nearest even


round toward negative infinity


round toward infinity


round toward zero

Any of the above can be set or extracted from the floating point control word using RND_MASK. Several examples follow:
FPcontrol(0, UNFL);

Make underflow silent.

FPstatus(0, INEX);

Check and clear the inexact flag.


Set directed rounding.

ilogb (x)

The ilogb function returns the nearest integral logarithm base 2 of the absolute value of x: for positive finite x,

1 <= x*2-ilogb(x)< 2, and ilogb(-x) = ilogb(x).

scalbn (x, n)

The scalbn function returns a scaled power of two:

x *2n.

copysign (x, s)

The copysign function returns the magnitude of x and the sign bit of s.

nextafter (x, y)

The nextafter function returns the machine number nearest x closer to y.

finite (x)

The finite function returns is 0 if x is Nan or and Infinity, 1 otherwise.

isnan (x)

The isnan function returns 1 if x is Nan and 0 otherwise.

fdim (x, y)

The fdim function equals x - y if x is greater than y, otherwise it is 0.

fmin (x, y)

The minimum of x and y.

fmax (x, y)

The maximum of x and y.

fabs (x)

The absolute value of x.

ceil (x)

The ceiling (round-up) of x.

floor (x)

The floor (round-down) of x.

fmod (x, y)

The fmod function conforms to the C language standard. It returns the value x - i *y for some i such that the remainder has the sign of x and magnitude less than the magnitude of y.

remainder (x, y)

The remainder function conforms to the IEEE standard which gives a remainder of magnitude no more than half the magnitude of y.

modf (x)

The modf function breaks x into integer and fractional parts returned in a tuple.

rint (x)

The rint function rounds to an integer, following the rounding mode specified in the floating point control word.

See Also

Limbo Math Modules

[Top] [Prev] [Next]

Copyright © 1996,Lucent Technologies, Inc. All rights reserved.