99 lines · 4.0 KB · Text
RawDownload
= math-fp: floating point     include   "math.m";    math := load Math Math->PATH;     Infinity, NaN, MachEps, Pi, Degree: real;    INVAL, ZDIV, OVFL, UNFL, INEX: int;    RND_NR, RND_NINF, RND_PINF, RND_Z, RND_MASK: 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 roundingmodes, exceptions, and other  properties  of  floating  pointarithmetic. Infinity and NaN are constants containing the positive infin‐ity and quiet not-a-number values of the IEEE binary floatingpoint  standard,  double  precision.   MachEps  is  2-52, thesmallest e such that 1+e is not equal to 1.  Pi is the  near‐est  machine  number to the infinitely precise value.  Degreeis Pi/180. Each thread has a  floating  point  control  word  (governingrounding  mode and whether a particular floating point excep‐tion causes a trap) and a floating point status word (storingaccumulated  exception  flags).   The functions FPcontrol andFPstatus copy bits to the control or status  word,  in  posi‐tions  specified  by a mask, returning the previous values ofthe bits specified in the mask.  The  functions  getFPcontroland getFPstatus return the words unchanged. INVAL, ZDIV, OVFL, UNFL, and INEX are non-overlapping single-bit masks used to compose arguments or return  values.   Theystand  for  the  five  IEEE  exceptions:  `invalid operation'(0/0,0+NaN,Infinity-Infinity,sqrt(-1)),  `division  by  zero'(1/0),  `overflow'  (1.8e308),  `underflow'  (1.1e-308),  and`inexact' (.3*.3). RND_NR, RND_NINF, RND_PINF, and RND_Z are distinct  bit  pat‐terns  for  `round  to  nearest even', `round toward negativeinfinity', `round toward infinity', and `round toward 0', anyof which can be set or extracted from the floating point con‐trol word using RND_MASK.  For  example,  FPcontrol(0,  UNFL)makes  underflow  silent; FPstatus(0, INEX) checks and clearsthe inexact  flag;  and  FPcontrol(RND_PINF,  RND_MASK)  setsdirected rounding. By  default,  INEX  is quiet, OVFL, UNFL, and ZDIV are fatal,and rounding is to nearest even.  Limbo modules are  entitledto  assume  this,  and  if  they wish to use quiet underflow,overflow, or zero-divide, they must either  set  and  restorethe  control  register  or clearly document that their callermust do so. The ilogb function returns  the  nearest  integral  logarithmbase 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) is ascaled  power  of two: x*2n.  Copysign(x,s) has the magnitudeof x and the sign bit of s.  Nextafter(x,y)  is  the  machinenumber  nearest  x closer to y.  Finally, finite(x) is 0 if xis Nan or Infinity, 1 otherwise, and isnan(x) is 1  if  x  isNan and 0 otherwise. The  function  fdim(x,y) = x-y if x is greater than y, other‐wise it is 0.  The functions  fmin,  fmax,  fabs,  ceil,  andfloor are the customary minimum, maximum, absolute value, andinteger rounding routines. There are two functions for computing the modulus:  fmod(x,y)is  the  function  defined  by the C standard which gives thevalue x-i*y for some i such that the remainder has  the  signof  x  and  magnitude  less  than  the  magnitude of y, whileremainder(x,y) is the function defined by the  IEEE  standardwhich  gives  a  remainder of magnitude no more than half themagnitude of y.  The function modf(x) breaks x  into  integerand  fractional parts returned in a tuple, and rint rounds toan integer, following the  rounding  mode  specified  in  thefloating point control word. == source/libinterp/math.c == see ALSOmath-intro(2)