(* Mathematical functions of F64, as C's math.h has them, to within the last bit or so. They follow fdlibm, Sun's library that most C ones come from: each brings its argument into a small range, where a polynomial is accurate, and puts the range back on the result. The polynomials' coefficients are fdlibm's. Each is within an ulp, a unit in the result's last place. The square root, mul and divMod's division are the machine's own, from Arith, one file per machine; this file keeps no machine code. Left out: sinh, cosh, fmod and hypot, until a program needs them. *) module Math; import Arith, Check, Rt; const pi* = 3.141592653589793; (* the F64 nearest to pi *) ln2hi = 6.93147180369123816490E-01; (* ln 2 in two parts, whose sum is more exact than one F64 *) ln2lo = 1.90821492927058770002E-10; (* The bits of x, and the F64 with bits u: a view of each, its bytes read as the other type (report section 6.9). *) proc bits*(x: F64): U64; begin return x.(U64); end; proc fromBits*(u: U64): F64; begin return u.(F64); end; (* Whether x has its sign bit set, -0.0 included. *) proc negative(x: F64): BOOL; begin return bits(x) shr 63 = 1; end; (* The square root of x, exact to the last bit; NaN for x below 0: the machine's own. A procedure that only returns is made in place, and then Arith.sqrt's one instruction is too, with no call. *) proc sqrt*(x: F64): F64; begin return Arith.sqrt(x); end; (* The largest whole number not above x. One of 2^52 or more is whole already, and so are an infinity and NaN. *) proc floor*(x: F64): F64; var t: F64; begin if not (x > -4503599627370496.0) or not (x < 4503599627370496.0) or (x = 0.0) then return x; end; t := F64(INT(x)); if t > x then t := t - 1.0; end; return t; end; (* The smallest whole number not below x. *) proc ceil*(x: F64): F64; begin return -floor(-x); end; (* The whole number nearest x, a half away from 0: the distance to floor(|x|) is exact, where x + 0.5 may round up. *) proc round*(x: F64): F64; var a, t: F64; begin a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); t := floor(a); if a - t >= 0.5 then t := t + 1.0; end; if negative(x) then return -t; end; return t; end; (* The whole number nearest x, a half to the even one, as IEEE 754's default and Pascal's Round have it. As for round, the distance to floor(|x|) is exact, where x + 0.5 may round up. *) proc roundEven*(x: F64): F64; var a, t, d: F64; begin a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); if not (a < 4503599627370496.0) then return x; end; (* whole already, or NaN *) t := floor(a); d := a - t; if (d > 0.5) or ((d = 0.5) and (INT(t) & 1 = 1)) then t := t + 1.0; end; if negative(x) then return -t; end; return t; end; (* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits, in two steps where k is past what an F64's exponent holds. *) proc scale(x: F64; k: INT): F64; begin if k > 1023 then x := x * fromBits(U64(2046) shl 52); DEC(k, 1023); end; if k < -1022 then x := x * fromBits(U64(1 + 53) shl 52); INC(k, 1022 - 53); end; return x * fromBits(U64(k + 1023) shl 52); end; (* e^x = 2^k e^r, with r = x - k ln 2 at most ln 2 / 2 across. *) proc exp*(x: F64): F64; var k: INT; hi, lo, r, rr, c: F64; begin if x # x then return x; end; if x > 709.782712893383973096 then return x * fromBits(U64(2046) shl 52); end; (* too big: inf *) if x < -745.13321910194110842 then return 0.0; end; if x < 0.0 then k := INT(x * 1.44269504088896338700 - 0.5); else k := INT(x * 1.44269504088896338700 + 0.5); end; hi := x - F64(k) * ln2hi; lo := F64(k) * ln2lo; r := hi - lo; rr := r * r; c := r - rr * (1.66666666666666019037E-01 + rr * (-2.77777777770155933842E-03 + rr * (6.61375632143793436117E-05 + rr * (-1.65339022054652515390E-06 + rr * 4.13813679705723846039E-08)))); return scale(1.0 + (r * c / (2.0 - c) - lo + hi), k); end; (* e^x - 1, for x from -45 to 45, as fdlibm's expm1: like exp, but the 1 is taken out of the sum, so that no digit is lost near 0. With x = k ln 2 + r, e^x - 1 = 2^k (e^r - 1) + 2^k - 1, and c is the rounding error of r, put back at the end. *) proc expm1(x: F64): F64; var a, hi, lo, c, hfx, hxs, r1, t, e, y: F64; k: INT; begin a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); k := 0; c := 0.0; if a > 0.34657359027997264 then (* more than ln 2 / 2 *) if a < 1.0397207708399179 then (* less than 3 ln 2 / 2 *) if negative(x) then k := -1; else k := 1; end; elsif negative(x) then k := INT(x * 1.44269504088896338700 - 0.5); else k := INT(x * 1.44269504088896338700 + 0.5); end; hi := x - F64(k) * ln2hi; lo := F64(k) * ln2lo; x := hi - lo; c := (hi - x) - lo; elsif a < 5.551115123125783E-17 then return x; (* below 2^-54 *) end; hfx := 0.5 * x; hxs := x * hfx; r1 := 1.0 + hxs * (-3.33333333333331316428E-02 + hxs * (1.58730158725481460165E-03 + hxs * (-7.93650757867487942473E-05 + hxs * (4.00821782732936239552E-06 + hxs * (-2.01099218183624371326E-07))))); t := 3.0 - r1 * hfx; e := hxs * ((r1 - t) / (6.0 - x * t)); if k = 0 then return x - (x * e - hxs); end; e := (x * (e - c) - c) - hxs; if k = -1 then return 0.5 * (x - e) - 0.5; end; if k = 1 then if x < -0.25 then return -2.0 * (e - (x + 0.5)); end; return 1.0 + 2.0 * (x - e); end; if k <= -2 then return scale(1.0 - (e - x), k) - 1.0; end; if k < 20 then y := (1.0 - scale(1.0, -k)) - (e - x); else y := (x - (e + scale(1.0, -k))) + 1.0; end; return scale(y, k); end; (* The hyperbolic tangent of x, from -1 to 1: fdlibm's, from expm1(2|x|), which is accurate for a small x where 1 - 2/(e^2x + 1) would lose every digit. From 22 on it is 1 to the last bit. *) proc tanh*(x: F64): F64; var a, t, z: F64; begin if x # x then return x; end; a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); if a >= 22.0 then z := 1.0; elsif a < 2.7755575615628914E-17 then return x; (* below 2^-55: tanh x = x; keeps -0 *) elsif a >= 1.0 then t := expm1(2.0 * a); z := 1.0 - 2.0 / (t + 2.0); else t := expm1(-2.0 * a); z := -t / (t + 2.0); end; if negative(x) then return -z; end; return z; end; (* ln x = k ln 2 + ln m, with m from sqrt(2)/2 to sqrt(2): ln m is found through s = f / (2 + f), f = m - 1, whose series converges fast. *) proc ln*(x: F64): F64; var u: U64; k: INT; f, hfsq, s, z, w, r: F64; begin if x # x then return x; end; if x < 0.0 then return (x - x) / 0.0; end; (* NaN *) if x = 0.0 then return -1.0 / 0.0; end; (* -inf *) if x > 1.7976931348623157E308 then return x; end; if x < 2.2250738585072014E-308 then (* subnormal: made normal *) x := x * 18014398509481984.0; (* 2^54 *) k := -54; end; (* the exponent, counted from m's lower end, and the bits of m *) u := bits(x) + (3FF0000000000000H - 3FE6A09E00000000H); INC(k, INT(u shr 52) - 3FFH); f := fromBits((u & 0FFFFFFFFFFFFFH) + 3FE6A09E00000000H) - 1.0; hfsq := 0.5 * f * f; s := f / (2.0 + f); z := s * s; w := z * z; r := w * (3.999999999940941908E-01 + w * (2.222219843214978396E-01 + w * 1.531383769920937332E-01)) + z * (6.666666666666735130E-01 + w * (2.857142874366239149E-01 + w * (1.818357216161805012E-01 + w * 1.479819860511658591E-01))); return s * (hfsq + r) + F64(k) * ln2lo - hfsq + f + F64(k) * ln2hi; end; (* pow and the logarithms after fdlibm's pow, which works on the two 32-bit halves of an F64: the high one signed, with the sign and exponent, and the low one. *) proc high(x: F64): INT; var h: INT; begin h := INT(bits(x) shr 32); if h >= 80000000H then DEC(h, 100000000H); end; return h; end; proc low(x: F64): INT; begin return INT(bits(x) & 0FFFFFFFFH); end; (* x with its low half 0: at most 21 bits, so that the product of two such is exact. *) proc noLow(x: F64): F64; begin return fromBits(bits(x) & 0FFFFFFFF00000000H); end; proc withHigh(x: F64; h: INT): F64; begin return fromBits(U64(h & 0FFFFFFFFH) shl 32 | bits(x) & 0FFFFFFFFH); end; (* log2 x, for a finite x above 0, as hi + lo to about 2^-64 of it, hi with its low half 0. x = 2^n m, with m from 1 to sqrt(3); ln m comes from s = (m - b)/(m + b), with b 1 or 1.5 as m is nearest, and the series in s, made exact to its first terms in two parts. *) proc log2Parts(x: F64): (F64, F64); var ix, j, k, n: INT; u, v, ss, sh, sl, th, tl, s2, r, ph, pl, zh, zl, t, hi, b, dh, dl: F64; begin n := 0; ix := high(x); if ix < 00100000H then (* subnormal: made normal *) x := x * 9007199254740992.0; (* 2^53 *) n := -53; ix := high(x); end; INC(n, (ix shr 20) - 3FFH); j := ix & 000FFFFFH; ix := j | 3FF00000H; if j <= 3988EH then k := 0; (* m below sqrt(3/2) *) elsif j < 0BB67AH then k := 1; (* below sqrt(3) *) else k := 0; INC(n); DEC(ix, 00100000H); end; x := withHigh(x, ix); if k = 0 then b := 1.0; dh := 0.0; dl := 0.0; else b := 1.5; dh := 5.84962487220764160156E-01; dl := 1.35003920212974897128E-08; (* log2 1.5 *) end; u := x - b; v := 1.0 / (x + b); ss := u * v; sh := noLow(ss); th := withHigh(0.0, ((ix shr 1) | 20000000H) + 00080000H + k shl 18); (* x + b, its high half *) tl := x - (th - b); sl := v * ((u - sh * th) - sh * tl); s2 := ss * ss; r := s2 * s2 * (5.99999999999994648725E-01 + s2 * (4.28571428578550184252E-01 + s2 * (3.33333329818377432918E-01 + s2 * (2.72728123808534006489E-01 + s2 * (2.30660745775561754067E-01 + s2 * 2.06975017800338417784E-01))))); r := r + sl * (sh + ss); s2 := sh * sh; th := noLow(3.0 + s2 + r); tl := r - ((th - 3.0) - s2); u := sh * th; v := sl * th + tl * ss; ph := noLow(u + v); pl := v - (ph - u); (* 2/(3 ln 2), in two parts, times p *) zh := 9.61796700954437255859E-01 * ph; zl := pl * 9.61796693925975554329E-01 - 7.02846165095275826516E-09 * ph + dl; t := F64(n); hi := noLow(((zh + zl) + dh) + t); return hi, zl - (((hi - t) - dh) - zh); end; (* The logarithm to base 2 of x; exact for a power of 2. *) proc log2*(x: F64): F64; var hi, lo: F64; begin if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x) * 1.44269504088896338700; end; hi, lo := log2Parts(x); return hi + lo; end; (* The logarithm to base 10 of x: log2 x times log10 2, each in two parts, the high parts' product exact. *) proc log10*(x: F64): F64; var hi, lo: F64; begin if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x); end; hi, lo := log2Parts(x); return hi * 0.30102992057800293 + (hi * 7.508597826552624E-08 + lo * 0.3010299956639812); end; (* x to the power y, as fdlibm's pow: within an ulp, and exact for a whole result an F64 holds. log2 |x| in two parts, times y in two parts, is z, and the result 2^z: 2^n times 2^r, for r at most 1/2 across, from a series. *) proc pow*(x, y: F64): F64; var hx, hy, ix, iy, lx, ly, i, j, k, n, yisint: INT; ax, z, ph, pl, y1, t1, t2, r, s, t, u, v, w: F64; begin hx := high(x); lx := low(x); hy := high(y); ly := low(y); ix := hx & 7FFFFFFFH; iy := hy & 7FFFFFFFH; if iy | ly = 0 then return 1.0; end; (* x^0, even for a NaN *) if (hx = 3FF00000H) and (lx = 0) then return 1.0; end; (* 1^y, even for a NaN *) if (x # x) or (y # y) then return x + y; end; (* whether y is whole, as x < 0 needs to know: 0 not, 1 odd, 2 even *) yisint := 0; if hx < 0 then if iy >= 43400000H then yisint := 2; (* 2^53 or more *) elsif iy >= 3FF00000H then k := (iy shr 20) - 3FFH; if k > 20 then j := ly shr (52 - k); if j shl (52 - k) = ly then yisint := 2 - (j & 1); end; elsif ly = 0 then j := iy shr (20 - k); if j shl (20 - k) = iy then yisint := 2 - (j & 1); end; end; end; end; if ly = 0 then if iy = 7FF00000H then (* y an infinity *) if (ix - 3FF00000H) | lx = 0 then return 1.0; end; (* (-1)^inf *) if ix >= 3FF00000H then if hy >= 0 then return y; end; return 0.0; end; if hy < 0 then return -y; end; return 0.0; end; if iy = 3FF00000H then if hy < 0 then return 1.0 / x; end; return x; end; if hy = 40000000H then return x * x; end; if (hy = 3FE00000H) and (hx >= 0) then return sqrt(x); end; end; ax := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); if (lx = 0) and ((ix = 7FF00000H) or (ix = 0) or (ix = 3FF00000H)) then (* x 0, 1 or an infinity *) z := ax; if hy < 0 then z := 1.0 / z; end; if hx < 0 then if (ix - 3FF00000H) | yisint = 0 then z := (z - z) / (z - z); (* (-1)^y, y not whole *) elsif yisint = 1 then z := -z; end; end; return z; end; if (hx < 0) and (yisint = 0) then return (x - x) / (x - x); end; (* NaN *) s := 1.0; if (hx < 0) and (yisint = 1) then s := -1.0; end; if iy > 41E00000H then (* |y| above 2^31 *) if iy > 43F00000H then (* above 2^64: x^y over- or underflows *) if (ix <= 3FEFFFFFH) = (hy < 0) then return 1.0E300 * 1.0E300; end; return 1.0E-300 * 1.0E-300; end; if ix < 3FEFFFFFH then if hy < 0 then return s * 1.0E300 * 1.0E300; end; return s * 1.0E-300 * 1.0E-300; end; if ix > 3FF00000H then if hy > 0 then return s * 1.0E300 * 1.0E300; end; return s * 1.0E-300 * 1.0E-300; end; (* |x - 1| is at most 2^-20: log2 x from the series of ln(1 + t) *) t := ax - 1.0; w := t * t * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); u := 1.44269502162933349609E+00 * t; v := t * 1.92596299112661746887E-08 - w * 1.44269504088896338700; t1 := noLow(u + v); t2 := v - (t1 - u); else t1, t2 := log2Parts(ax); end; (* z = y log2 x, as ph + pl *) y1 := noLow(y); pl := (y - y1) * t1 + y * t2; ph := y1 * t1; z := pl + ph; j := high(z); i := low(z); if j >= 40900000H then (* z at least 1024 *) if ((j - 40900000H) | i # 0) or (pl + 8.0085662595372944372E-17 > z - ph) then return s * 1.0E300 * 1.0E300; end; elsif j & 7FFFFFFFH >= 4090CC00H then (* z at most -1075 *) if (j # -3F6F3400H) or (i # 0) or (pl <= z - ph) then return s * 1.0E-300 * 1.0E-300; end; end; (* 2^z: n the nearest whole number, taken from ph *) i := j & 7FFFFFFFH; k := (i shr 20) - 3FFH; n := 0; if i > 3FE00000H then n := j + 00100000H shr (k + 1); k := ((n & 7FFFFFFFH) shr 20) - 3FFH; t := withHigh(0.0, n - (n & (000FFFFFH shr k))); n := ((n & 000FFFFFH) | 00100000H) shr (20 - k); if j < 0 then n := -n; end; ph := ph - t; end; t := noLow(pl + ph); u := t * 6.93147182464599609375E-01; v := (pl - (t - ph)) * 6.93147180559945286227E-01 - t * 1.90465429995776804525E-09; z := u + v; w := v - (z - u); t := z * z; t1 := z - t * (1.66666666666666019037E-01 + t * (-2.77777777770155933842E-03 + t * (6.61375632143793436117E-05 + t * (-1.65339022054652515390E-06 + t * 4.13813679705723846039E-08)))); r := (z * t1) / (t1 - 2.0) - (w + z * w); z := 1.0 - (r - z); j := high(z) + n shl 20; if j < 00100000H then z := scale(z, n); (* a subnormal result *) else z := withHigh(z, j); end; return s * z; end; (* a times b: the low 64 bits of the product, and the high. *) proc mul*(a, b: U64): (U64, U64); begin return Arith.mul(a, b); end; (* hi, lo (a 128-bit number, hi its high word) divided by d: the quotient and the remainder. hi >= d, whose quotient doesn't fit a word, and d = 0 stop the program, as a division by zero does. *) proc divMod*(hi, lo, d: U64): (U64, U64); begin if hi >= d then Rt.stop("Math.divMod: hi >= d, the quotient doesn't fit a word"); end; return Arith.div128(hi, lo, d); end divMod; (* 64 bits of t from bit s on, the first bit of t[0] being 0. *) proc window(t: []const U64; s: INT): U64; var q, r: INT; begin q := s div 64; r := s mod 64; if r = 0 then return t[q]; end; return t[q] shl r | t[q + 1] shr (64 - r); end; (* reduce's work, for an x too big for it, by Payne and Hanek's way. x is m 2^e, m a whole number of 53 bits, so x 2/pi is m times the bits of 2/pi moved by e: those that land at 4 or above add whole turns, which change no sine, and are left out; the 192 after them give the two bits of n mod 4 and the fraction past n, exact far beyond an F64. *) proc reduceLarge(x: F64): (INT, F64, F64); var t: [21]U64; (* 2/pi's bits after its point, behind a word of zeros *) m, w0, w1, w2, p0, p1, p2, lo: U64; e, s, n, k: INT; y: F64; neg, up: BOOL; begin t := [0, 0A2F9836E4E441529H, 0FC2757D1F534DDC0H, 0DB6295993C439041H, 0FE5163ABDEBBC561H, 0B7246E3A424DD2E0H, 06492EEA09D1921CH, 0FE1DEB1CB129A73EH, 0E88235F52EBB4484H, 0E99C7026B45F7E41H, 03991D639835339F4H, 09C845F8BBDF9283BH, 01FF897FFDE05980FH, 0EF2F118B5A0A6D1FH, 06D367ECF27CB09B7H, 04F463F669E5FEA2DH, 07527BAC7EBE5F17BH, 03D0739F78A5292EAH, 06BFB5FB11F8D5D08H, 056033046FC7B6BABH, 0F0CFBC209AF4361DH]; neg := x < 0.0; if neg then x := -x; end; m := bits(x) & 0FFFFFFFFFFFFFH | 10000000000000H; e := INT(bits(x) shr 52) - 1075; (* W, the 192 bits of 2/pi from the one worth 2^(1 - e), so that x 2/pi is m W 2^-190, of which the bits from 2^192 up are left out *) s := e + 62; w0 := window(t, s); w1 := window(t, s + 64); w2 := window(t, s + 128); p0, p1 := mul(m, w2); lo, p2 := mul(m, w1); INC(p1, lo); INC(p2, m * w0); if p1 < lo then INC(p2); end; (* n is the two bits from 2^190, the fraction those below; from a half up, n is the next and the fraction 1 less, taken as 1 less the fraction, negated: its bits complemented, one in 2^190 short *) n := INT(p2 shr 62); p2 := p2 & 3FFFFFFFFFFFFFFFH; up := p2 shr 61 = 1; if up then INC(n); p0 := ~p0; p1 := ~p1; p2 := ~p2 & 3FFFFFFFFFFFFFFFH; end; (* the fraction as an F64, from the 64 bits that start at its first one *) k := 0; while (p2 shr 61 = 0) and (k < 128) do p2 := p2 shl 1 | p1 shr 63; p1 := p1 shl 1 | p0 shr 63; p0 := p0 shl 1; INC(k); end; y := scale(F64(p2 shl 2 | p1 shr 62) * (pi / 2.0), -64 - k); if up then y := -y; end; if neg then return -n, -y, 0.0; end; return n, y, 0.0; end; (* x less the whole number n of pi/2 nearest it, as y + yy, more exact than one F64; and n. pi/2 is taken in three parts of 33 bits, each with what is left of it after, so that n times each is exact while n is below 2^20, about x = 1.6E6. *) proc reduce(x: F64): (INT, F64, F64); var n: INT; fn, r, t, w, y: F64; begin if (x > 1.6E6) or (x < -1.6E6) then return reduceLarge(x); end; if x < 0.0 then n := INT(x * 6.36619772367581382433E-01 - 0.5); else n := INT(x * 6.36619772367581382433E-01 + 0.5); end; fn := F64(n); r := x - fn * 1.57079632673412561417E+00; t := r; w := fn * 6.07710050630396597660E-11; r := t - w; w := fn * 2.02226624879595063154E-21 - ((t - r) - w); t := r; w := fn * 2.02226624871116645580E-21; r := t - w; w := fn * 8.47842766036889956997E-32 - ((t - r) - w); y := r - w; return n, y, (r - y) - w; end; (* sin and cos of x + y, x at most pi/4 and y far below it. *) proc kernelSin(x, y: F64): F64; var z, v, r: F64; begin z := x * x; v := z * x; r := 8.33333333332248946124E-03 + z * (-1.98412698298579493134E-04 + z * 2.75573137070700676789E-06) + z * z * z * (-2.50507602534068634195E-08 + z * 1.58969099521155010221E-10); return x - ((z * (0.5 * y - v * r) - y) + v * 1.66666666666666324348E-01); end; proc kernelCos(x, y: F64): F64; var z, r, hz, w: F64; begin z := x * x; r := z * (4.16666666666666019037E-02 + z * (-1.38888888888741095749E-03 + z * 2.48015872894767294178E-05)) + z * z * z * z * (-2.75573143513906633035E-07 + z * (2.08757232129817482790E-09 - z * 1.13596475577881948265E-11)); hz := 0.5 * z; w := 1.0 - hz; return w + (((1.0 - w) - hz) + (z * r - x * y)); end; (* The sine of x, in radians; NaN for an infinity or NaN. *) proc sin*(x: F64): F64; var n: INT; y, yy: F64; begin if (x = 0.0) or (x # x) then return x; end; if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *) n, y, yy := reduce(x); n := n & 3; if n = 0 then return kernelSin(y, yy); elsif n = 1 then return kernelCos(y, yy); elsif n = 2 then return -kernelSin(y, yy); end; return -kernelCos(y, yy); end; (* The cosine of x, in radians; NaN for an infinity or NaN. *) proc cos*(x: F64): F64; var n: INT; y, yy: F64; begin if (x - x) # 0.0 then return x - x; end; (* an infinity or NaN: NaN *) n, y, yy := reduce(x); n := n & 3; if n = 0 then return kernelCos(y, yy); elsif n = 1 then return -kernelSin(y, yy); elsif n = 2 then return -kernelCos(y, yy); end; return kernelSin(y, yy); end; (* tan(x + y), for x + y at most pi/4 across and y below an ulp of x, or, for k -1, -1/tan(x + y): fdlibm's. Past 0.6744, tan(pi/4 - x) is found and turned back; -1/w is made exact to its first part. *) proc kernelTan(x, y: F64; k: INT): F64; var hx, ix: INT; z, r, v, w, s, a, t: F64; begin hx := high(x); ix := hx & 7FFFFFFFH; if ix < 3E300000H then (* below 2^-28 *) if k = 1 then return x; end; if x = 0.0 then return 1.0 / fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); end; w := x + y; z := noLow(w); v := y - (z - x); a := -1.0 / w; t := noLow(a); s := 1.0 + t * z; return t + a * (s + t * v); end; if ix >= 3FE59428H then if hx < 0 then x := -x; y := -y; end; z := 7.85398163397448278999E-01 - x; (* pi/4, in two parts *) w := 3.06161699786838301793E-17 - y; x := z + w; y := 0.0; end; z := x * x; w := z * z; r := 1.33333333333201242699E-01 + w * (2.18694882948595424599E-02 + w * (3.59207910759131235356E-03 + w * (5.88041240820264096874E-04 + w * (7.81794442939557092300E-05 - w * 1.85586374855275456654E-05)))); v := z * (5.39682539762260521377E-02 + w * (8.86323982359930005737E-03 + w * (1.45620945432529025516E-03 + w * (2.46463134818469906812E-04 + w * (7.14072491382608190305E-05 + w * 2.59073051863633712884E-05))))); s := z * x; r := y + z * (s * (r + v) + y); r := r + 3.33333333333334091986E-01 * s; w := x + r; if ix >= 3FE59428H then v := F64(k); w := v - 2.0 * (x - (w * w / (w + v) - r)); if hx < 0 then return -w; end; return w; end; if k = 1 then return w; end; z := noLow(w); v := r - (z - x); a := -1.0 / w; t := noLow(a); s := 1.0 + t * z; return t + a * (s + t * v); end; (* The tangent of x, in radians; NaN for an infinity or NaN. *) proc tan*(x: F64): F64; var n: INT; y, yy: F64; begin if (x = 0.0) or (x # x) then return x; end; if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *) n, y, yy := reduce(x); return kernelTan(y, yy, 1 - (n & 1) * 2); end; (* The arc tangent of x, at least 0: atan(x) = atan(c) + atan(t), t small, for c one of 1/2, 1, 3/2 and infinity, as x is nearest; the atan(c) in two parts as pi is. *) proc atanAbove(x: F64): F64; var hi, lo, z, w, s1, s2: F64; begin if x < 0.4375 then hi := 0.0; lo := 0.0; elsif x < 0.6875 then hi := 4.63647609000806093515E-01; lo := 2.26987774529616870924E-17; x := (2.0 * x - 1.0) / (2.0 + x); elsif x < 1.1875 then hi := 7.85398163397448278999E-01; lo := 3.06161699786838301793E-17; x := (x - 1.0) / (x + 1.0); elsif x < 2.4375 then hi := 9.82793723247329054082E-01; lo := 1.39033110312309984516E-17; x := (x - 1.5) / (1.0 + 1.5 * x); else hi := 1.57079632679489655800E+00; lo := 6.12323399573676603587E-17; x := -1.0 / x; end; z := x * x; w := z * z; s1 := z * (3.33333333333329318027E-01 + w * (1.42857142725034663711E-01 + w * (9.09088713343650656196E-02 + w * (6.66107313738753120669E-02 + w * (4.97687799461593236017E-02 + w * 1.62858201153657823623E-02))))); s2 := w * (-1.99999999998764832476E-01 + w * (-1.11111104054623557880E-01 + w * (-7.69187620504482999495E-02 + w * (-5.83357013379057348645E-02 - w * 3.65315727442169155270E-02)))); return hi - ((x * (s1 + s2) - lo) - x); end; (* The angle of the point (x, y) from the x axis, from -pi to pi. *) proc atan2*(y, x: F64): F64; var a: F64; (* the angle from the x axis on x's side, at least 0 *) begin if (x # x) or (y # y) then return x + y; end; if y = 0.0 then if not negative(x) then return y; end; a := pi; else if ((x - x) # 0.0) and ((y - y) # 0.0) then a := pi / 4.0; (* both infinite *) else a := y / x; if a < 0.0 then a := -a; end; a := atanAbove(a); end; (* pi less a, with what pi lacks of its true value *) if negative(x) then a := pi - (a - 1.2246467991473531772E-16); end; end; if negative(y) then return -a; end; return a; end; (* The arc tangent of x, from -pi/2 to pi/2. *) proc atan*(x: F64): F64; begin if x # x then return x; end; if negative(x) then return -atanAbove(-x); end; return atanAbove(x); end; (* asin(sqrt t)/sqrt t - 1, near enough, for t up to 1/4: fdlibm's rational function, for asin and acos. *) proc arcSeries(t: F64): F64; begin return t * (1.66666666666666657415E-01 + t * (-3.25565818622400915405E-01 + t * (2.01212532134862925881E-01 + t * (-4.00555345006794114027E-02 + t * (7.91534994289814532176E-04 + t * 3.47933107596021167570E-05))))) / (1.0 + t * (-2.40339491173441421878E+00 + t * (2.02094576023350569471E+00 + t * (-6.88283971605453293030E-01 + t * 7.70381505559019352791E-02)))); end; (* The arc sine of x, from -pi/2 to pi/2; NaN past -1 and 1. Past 1/2, asin x = pi/2 - 2 asin(sqrt((1 - |x|)/2)), the root in two parts. *) proc asin*(x: F64): F64; var ix: INT; a, t, s, w, c, p, q: F64; begin ix := high(x) & 7FFFFFFFH; if ix >= 3FF00000H then if (ix = 3FF00000H) and (low(x) = 0) then return x * 1.57079632679489655800 + x * 6.12323399573676603587E-17; end; return (x - x) / (x - x); (* NaN *) end; if ix < 3FE00000H then (* below 1/2 *) if ix < 3E400000H then return x; end; (* below 2^-27 *) return x + x * arcSeries(x * x); end; a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); t := (1.0 - a) * 0.5; s := sqrt(t); if ix >= 3FEF3333H then (* past 0.975 *) t := 1.57079632679489655800 - (2.0 * (s + s * arcSeries(t)) - 6.12323399573676603587E-17); else w := noLow(s); c := (t - w * w) / (s + w); p := 2.0 * s * arcSeries(t) - (6.12323399573676603587E-17 - 2.0 * c); q := 7.85398163397448278999E-01 - 2.0 * w; t := 7.85398163397448278999E-01 - (p - q); end; if negative(x) then return -t; end; return t; end; (* The arc cosine of x, from 0 to pi; NaN past -1 and 1: pi/2 - asin x near 0, and 2 asin(sqrt((1 -+ x)/2)) past 1/2 either way. *) proc acos*(x: F64): F64; var hx, ix: INT; z, s, w, c, df: F64; begin hx := high(x); ix := hx & 7FFFFFFFH; if ix >= 3FF00000H then if (ix = 3FF00000H) and (low(x) = 0) then if hx > 0 then return 0.0; end; return pi + 2.0 * 6.12323399573676603587E-17; end; return (x - x) / (x - x); (* NaN *) end; if ix < 3FE00000H then (* below 1/2 *) if ix <= 3C600000H then return 1.57079632679489655800 + 6.12323399573676603587E-17; end; return 1.57079632679489655800 - (x - (6.12323399573676603587E-17 - x * arcSeries(x * x))); end; if hx < 0 then z := (1.0 + x) * 0.5; s := sqrt(z); w := arcSeries(z) * s - 6.12323399573676603587E-17; return pi - 2.0 * (s + w); end; z := (1.0 - x) * 0.5; s := sqrt(z); df := noLow(s); c := (z - df * df) / (s + df); w := arcSeries(z) * s + c; return 2.0 * (df + w); end; (* The module's test: values C's library gives, to within two units in the last place, and the edges. *) (* Reading a real from text exactly: the F64 nearest to its digits times 10 to its exponent, ties to the even one, as the compiler reads a constant (src/Scan.raz). The number is a fraction of two big integers, num / den, scaled by 2^e until its whole part has 53 or 54 bits; that part and the remainder give the bits and the rounding. *) const MaxDigits = 780; (* significant digits kept; past them, a 1 if any left out isn't 0 *) type Big = record n: INT; (* words in use; 0 is zero *) w: [128]U32; (* least significant first *) end; (* 768 significant digits are as many as can matter to rounding an F64, so the ones past MaxDigits matter only by being 0 or not, which a last digit 1 keeps. Then den is at most 10^(330 + 781), about 2^3691, and num below 10^310; the shift that follows makes the larger at most 54 bits longer than den, and the long division shifts den 53 more: under 3,800 bits, of the 4,096 here. *) (* a := a * m + add *) proc bigMul(var a: Big; m, add: U32); var i: INT; c: U64; begin c := U64(add); for i := 0 to a.n - 1 do c := U64(a.w[i]) * U64(m) + c; a.w[i] := U32(c); c := c shr 32; end; if c # 0 then a.w[a.n] := U32(c); INC(a.n); end; end; proc bigShl(var a: Big; k: INT); var i, ws, bits: INT; begin if a.n = 0 then return; end; ws := k div 32; bits := k mod 32; a.w[a.n + ws] := 0; for i := a.n - 1 to 0 by -1 do if bits > 0 then a.w[i + ws + 1] := a.w[i + ws + 1] | (a.w[i] shr (32 - bits)); end; a.w[i + ws] := a.w[i] shl bits; end; for i := 0 to ws - 1 do a.w[i] := 0; end; INC(a.n, ws + 1); while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; end; proc bigShr1(var a: Big); var i: INT; begin for i := 0 to a.n - 1 do a.w[i] := a.w[i] shr 1; if i + 1 < a.n then a.w[i] := a.w[i] | (a.w[i + 1] shl 31); end; end; while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; end; (* -1, 0 or 1 as a is below, equal to or above b *) proc bigCmp(var a, b: Big): INT; var i: INT; begin if a.n # b.n then if a.n > b.n then return 1; end; return -1; end; for i := a.n - 1 to 0 by -1 do if a.w[i] # b.w[i] then if a.w[i] > b.w[i] then return 1; end; return -1; end; end; return 0; end; (* a := a - b, b not above a *) proc bigSub(var a, b: Big); var i: INT; d: I64; begin d := 0; for i := 0 to a.n - 1 do d := d + I64(a.w[i]); if i < b.n then d := d - I64(b.w[i]); end; a.w[i] := U32(d); if d < 0 then d := -1; else d := 0; end; (* a borrow *) end; while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end; end; proc bigBits(var a: Big): INT; var t: U32; bits: INT; begin if a.n = 0 then return 0; end; bits := (a.n - 1) * 32; t := a.w[a.n - 1]; while t # 0 do INC(bits); t := t shr 1; end; return bits; end; (* Whether s[i] is a decimal digit. *) proc digitAt(s: []const U8; i: INT): BOOL; begin return (i < LEN(s)) and (s[i] >= '0') and (s[i] <= '9'); end; (* s as a real: a sign if any, digits, a point and digits if any, and E or e, a sign if any and digits if any; nothing else. The F64 nearest it, ties to the even one, and false if s isn't one, or is past the largest F64; one below the smallest is 0. *) proc toReal*(s: []const U8): (F64, BOOL); var num, den, d: Big; i, digits, e, ex: INT; neg, eneg, point, half, sticky: BOOL; q, b: U64; x: F64; begin x := 0.0; num.n := 0; den.n := 1; den.w[0] := 1; digits := 0; e := 0; sticky := false; neg := (LEN(s) > 0) and (s[0] = '-'); i := 0; if (LEN(s) > 0) and ((s[0] = '-') or (s[0] = '+')) then i := 1; end; if not digitAt(s, i) then return x, false; end; (* the digits: num, and e, the power of 10 it is to be taken to *) point := false; while digitAt(s, i) or not point and (i < LEN(s)) and (s[i] = '.') and digitAt(s, i + 1) do if s[i] = '.' then point := true; elsif digits < MaxDigits then bigMul(num, 10, U32(s[i] - '0')); if num.n > 0 then INC(digits); end; (* counted from the first that isn't 0 *) if point then DEC(e); end; else (* a digit not kept, which still counts if it is before the point *) if s[i] # '0' then sticky := true; end; if not point then INC(e); end; end; INC(i); end; if sticky then bigMul(num, 10, 1); DEC(e); end; ex := 0; if (i < LEN(s)) and ((s[i] = 'E') or (s[i] = 'e')) then INC(i); eneg := (i < LEN(s)) and (s[i] = '-'); if (i < LEN(s)) and ((s[i] = '+') or (s[i] = '-')) then INC(i); end; if not digitAt(s, i) then return x, false; end; while digitAt(s, i) do if ex < 100000 then ex := ex * 10 + INT(s[i]) - '0'; end; INC(i); end; if eneg then ex := -ex; end; end; if i < LEN(s) then return x, false; end; INC(e, ex); if neg then x := -x; end; if num.n = 0 then return x, true; end; if digits + e > 310 then return x, false; end; (* 10^310 is past every F64 *) if digits + e < -330 then return x, true; end; (* and 10^-330 below half the least *) for i := 1 to e do bigMul(num, 10, 0); end; for i := 1 to -e do bigMul(den, 10, 0); end; (* num / den 2^-e from 2^52 to 2^54, but not past the least exponent *) e := bigBits(num) - bigBits(den) - 53; if e < -1074 then e := -1074; end; if e < 0 then bigShl(num, -e); else bigShl(den, e); end; (* q, its whole part, by long division; num is left the remainder *) d := den; bigShl(d, 53); q := 0; for i := 53 to 0 by -1 do if bigCmp(num, d) >= 0 then bigSub(num, d); q := q | (U64(1) shl i); end; bigShr1(d); end; if q >= U64(1) shl 53 then (* a bit too many: it rounds *) half := (q & 1) # 0; sticky := num.n > 0; q := q shr 1; INC(e); else (* the remainder against half of den *) bigShl(num, 1); i := bigCmp(num, den); half := i >= 0; sticky := i > 0; end; if half and (sticky or ((q & 1) # 0)) then INC(q); if q = U64(1) shl 53 then q := q shr 1; INC(e); end; end; if e > 971 then return x, false; end; if q >= U64(1) shl 52 then b := U64(e + 1075) shl 52 | (q & (U64(1) shl 52 - 1)); else b := q; (* below the least normal, e is -1074 *) end; if neg then b := b | (U64(1) shl 63); end; return fromBits(b), true; end; (* Whether toReal reads s as want, exactly, or as nothing if not ok. *) proc reads(s: []const U8; want: F64; ok: BOOL): BOOL; var x: F64; o: BOOL; begin x, o := toReal(s); return (o = ok) and (not ok or (bits(x) = bits(want))); end; proc testToReal; var b: [900]U8; i: INT; begin Check.ok(reads("2", 2.0, true) and reads("-0.5", -0.5, true) and reads("1.5e-3", 1.5E-3, true) and reads("+1.25E+2", 125.0, true) and reads("0.1", 0.1, true) and reads("1.7976931348623157E308", 1.7976931348623157E308, true) and reads("4.9e-324", fromBits(1), true) and reads("1e-400", 0.0, true) and reads("-0", -0.0, true) and reads("9007199254740993", 9007199254740992.0, true) and reads("1e309", 0.0, false) and reads("1.8E308", 0.0, false), "Math.toReal"); Check.ok(reads("", 0.0, false) and reads("-", 0.0, false) and reads("1.", 0.0, false) and reads(".5", 0.0, false) and reads("1e", 0.0, false) and reads("1.5.2", 0.0, false) and reads(" 1", 0.0, false) and reads("1x", 0.0, false) and reads("0x10", 0.0, false), "Math.toReal: not a real"); (* 2^53 + 1, then 801 zeros and a 1: just above the half between two F64s, though the 1 is past the digits kept, so it rounds up *) COPY(b[0..16], "9007199254740993"); for i := 16 to 816 do b[i] := '0'; end; b[817] := '1'; COPY(b[818..823], "e-802"); Check.ok(reads(b[0..823], 9007199254740994.0, true) and reads(b[0..817], 0.0, false), "Math.toReal: many digits"); end; proc near(got, want: F64): BOOL; var d: F64; begin d := got - want; if d < 0.0 then d := -d; end; if want < 0.0 then want := -want; end; return d <= want * 4.5E-16; end; (* The module's test. *) (* divMod against mul: the product of a and b, divided by b, gives a back. *) proc testDiv; var a, b, hi, lo, q, r, x: U64; i: INT; begin q, r := divMod(0, 7, 2); Check.ok((q = 3) and (r = 1), "Math.divMod small"); q, r := divMod(0, 12345, 1); Check.ok((q = 12345) and (r = 0), "Math.divMod by 1"); q, r := divMod(5, 9, 6); Check.ok((q = 0D555555555555556H) and (r = 5), "Math.divMod known"); q, r := divMod(0FFFFFFFFFFFFFFFEH, 0FFFFFFFFFFFFFFFFH, 0FFFFFFFFFFFFFFFFH); Check.ok((q = 0FFFFFFFFFFFFFFFFH) and (r = 0FFFFFFFFFFFFFFFEH), "Math.divMod maximal hi"); x := 88172645463325252; for i := 1 to 20 do x := x xor (x shl 13); x := x xor (x shr 7); x := x xor (x shl 17); a := x; x := x xor (x shl 13); x := x xor (x shr 7); x := x xor (x shl 17); b := x | 1; lo, hi := mul(a, b); q, r := divMod(hi, lo, b); Check.ok((q = a) and (r = 0), "Math.divMod of a product"); if b > 1 then r := b - 1; if lo + r >= lo then q, r := divMod(hi, lo + r, b); (* no carry out of lo *) Check.ok((q = a) and (r = b - 1), "Math.divMod with a remainder"); end; end; end; end; proc main*; var inf, nan, hard: F64; begin inf := 1.0 / 0.0; nan := inf - inf; hard := scale(6381956970095103.0, 797); (* of all F64s, the nearest a multiple of pi/2 *) Check.ok(not ((sqrt(2.0) # 1.4142135623730951) or (sqrt(0.0) # 0.0) or (sqrt(-1.0) = sqrt(-1.0))), "Math.sqrt"); Check.ok(not ((floor(2.5) # 2.0) or (floor(-2.5) # -3.0) or (floor(-2.0) # -2.0) or (floor(1.0E300) # 1.0E300) or not negative(floor(-0.0)) or (floor(nan) = floor(nan))), "Math.floor"); Check.ok(not (not near(exp(1.0), 2.718281828459045) or not near(exp(-3.5), 0.0301973834223185) or not near(exp(700.0), 1.0142320547350045E304) or not near(exp(-740.0), 4.2E-322) or (exp(0.0) # 1.0) or (exp(710.0) # inf) or (exp(-746.0) # 0.0) or (exp(-inf) # 0.0)), "Math.exp"); Check.ok(not (not near(ln(10.0), 2.302585092994046) or not near(ln(0.3), -1.2039728043259361) or not near(ln(1.0E-310), -713.8013788281542) or (ln(1.0) # 0.0) or (ln(0.0) # -inf) or (ln(inf) # inf) or (ln(-1.0) = ln(-1.0))), "Math.ln"); Check.ok(not (not near(sin(1.0), 0.8414709848078965) or not near(sin(-100.0), 0.5063656411097588) or not near(sin(pi), 1.2246467991473532E-16) or not near(sin(1.0E6), -0.34999350217129294) or not near(sin(1.0E22), -0.8522008497671888) or not negative(sin(-0.0)) or (sin(inf) = sin(inf))), "Math.sin"); Check.ok(not (not near(cos(1.0), 0.5403023058681398) or not near(cos(-100.0), 0.8623188722876839) or not near(cos(pi / 2.0), 6.123233995736766E-17) or not near(cos(1.0E6), 0.9367521275331447) or not near(cos(1.0E300), -0.5753861119575491) or not near(cos(hard), -4.687165924254628E-19) or (cos(0.0) # 1.0) or (cos(nan) = cos(nan))), "Math.cos"); Check.ok(not (not near(atan2(1.0, 2.0), 0.4636476090008061) or not near(atan2(-3.0, -4.0), -2.498091544796509) or not near(atan2(5.0, 0.1), 1.550798992821746) or not near(atan2(1.0, -1.0E-20), 1.5707963267948966) or (atan2(0.0, -1.0) # pi) or not negative(atan2(-0.0, 1.0)) or (atan2(-inf, inf) # -pi / 4.0) or not near(atan2(inf, -inf), 2.356194490192345) or (atan2(1.0, 0.0) # pi / 2.0)), "Math.atan2"); Check.ok(not ((ceil(2.5) # 3.0) or (ceil(-2.5) # -2.0) or not negative(ceil(-0.5)) or (ceil(1.0E300) # 1.0E300) or (round(2.5) # 3.0) or (round(-2.5) # -3.0) or (round(0.49999999999999994) # 0.0) or not negative(round(-0.3))), "Math.ceil, round"); Check.ok(not (not near(pow(2.5, 3.7), 29.67413253642086) or not near(pow(0.3, -12.5), 3435455.4096325925) or not near(pow(1.0000001, 1.0E9), 2.6881038582144647E43) or (pow(-2.0, -3.0) # -0.125) or (pow(10.0, 22.0) # 1.0E22) or (pow(2.0, -1074.0) # scale(1.0, -1074)) or (pow(2.0, 1024.0) # inf) or (pow(nan, 0.0) # 1.0) or (pow(1.0, nan) # 1.0) or (pow(-1.0, inf) # 1.0) or (pow(0.0, -1.0) # inf) or (pow(-0.0, -1.0) # -inf) or (pow(-2.0, 0.5) = pow(-2.0, 0.5))), "Math.pow"); Check.ok(not ((log2(8.0) # 3.0) or (log2(0.5) # -1.0) or not near(log2(10.0), 3.321928094887362) or not near(log2(3.0E-310), -1028.212746914361) or (log10(1000.0) # 3.0) or (log10(1.0E22) # 22.0) or not near(log10(2.0), 0.3010299956639812) or not near(log10(7.0E-300), -299.15490195998575) or (log2(0.0) # -inf) or (log10(-1.0) = log10(-1.0))), "Math.log2, log10"); Check.ok(not (not near(tan(1.0), 1.5574077246549023) or not near(tan(-1.5707963267948966), -1.633123935319537E16) or not near(tan(1.0E22), -1.6287782256068988) or not negative(tan(-0.0)) or (tan(inf) = tan(inf))), "Math.tan"); Check.ok(not (not near(asin(0.3), 0.3046926540153975) or not near(asin(-0.99), -1.4292568534704693) or (asin(1.0) # pi / 2.0) or (asin(1.5) = asin(1.5)) or not near(acos(0.7), 0.7953988301841436) or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi) or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965) or (atan(inf) # pi / 2.0) or not negative(atan(-0.0))), "Math.asin, acos, atan"); Check.ok(not (not near(tanh(0.5), 0.46211715726000974) or not near(tanh(1.0), 0.7615941559557649) or not near(tanh(-0.3), -0.2913126124515909) or not near(tanh(3.0), 0.9950547536867305) or not near(tanh(1.0E-10), 1.0E-10) or (tanh(-1.0E-300) # -1.0E-300) or (tanh(20.0) # 1.0) or (tanh(-30.0) # -1.0) or (tanh(inf) # 1.0) or not negative(tanh(-0.0)) or (tanh(nan) = tanh(nan))), "Math.tanh"); Check.ok(not ((roundEven(0.5) # 0.0) or (roundEven(1.5) # 2.0) or (roundEven(2.5) # 2.0) or (roundEven(3.5) # 4.0) or (roundEven(-2.5) # -2.0) or (roundEven(-3.5) # -4.0) or (roundEven(2.6) # 3.0) or (roundEven(-2.4) # -2.0) or not negative(roundEven(-0.5)) or negative(roundEven(0.5)) or (roundEven(0.49999999999999994) # 0.0) or (roundEven(4503599627370495.5) # 4503599627370496.0) or (roundEven(4503599627370497.0) # 4503599627370497.0) or (roundEven(-1.0E300) # -1.0E300) or (roundEven(inf) # inf) or (roundEven(nan) = roundEven(nan))), "Math.roundEven"); testDiv; testToReal; end; end Math.