raz / lib / Math.raz

1135 lines · 43.3 KiB · raw · history · last change 89524972

1(* Mathematical functions of F64, as C's math.h has them, to within the
2 last bit or so. They follow fdlibm, Sun's library that most C ones
3 come from: each brings its argument into a small range, where a
4 polynomial is accurate, and puts the range back on the result. The
5 polynomials' coefficients are fdlibm's. Each is within an ulp, a
6 unit in the result's last place.
7
8 The square root, mul and divMod's division are the machine's own,
9 from Arith, one file per machine; this file keeps no machine code.
10
11 Left out: sinh, cosh, fmod and hypot, until a program needs them. *)
12module Math;
13import Arith, Check, Rt;
14
15const
16 pi* = 3.141592653589793; (* the F64 nearest to pi *)
17 ln2hi = 6.93147180369123816490E-01; (* ln 2 in two parts, whose sum is more exact than one F64 *)
18 ln2lo = 1.90821492927058770002E-10;
19
20(* The bits of x, and the F64 with bits u: a view of each, its bytes
21 read as the other type (report section 6.9). *)
22proc bits*(x: F64): U64;
23begin
24 return x.(U64);
25end;
26
27proc fromBits*(u: U64): F64;
28begin
29 return u.(F64);
30end;
31
32(* Whether x has its sign bit set, -0.0 included. *)
33proc negative(x: F64): BOOL;
34begin
35 return bits(x) shr 63 = 1;
36end;
37
38(* The square root of x, exact to the last bit; NaN for x below 0: the
39 machine's own. A procedure that only returns is made in place, and
40 then Arith.sqrt's one instruction is too, with no call. *)
41proc sqrt*(x: F64): F64;
42begin
43 return Arith.sqrt(x);
44end;
45
46(* The largest whole number not above x. One of 2^52 or more is whole
47 already, and so are an infinity and NaN. *)
48proc floor*(x: F64): F64;
49var t: F64;
50begin
51 if not (x > -4503599627370496.0) or not (x < 4503599627370496.0) or (x = 0.0) then return x; end;
52 t := F64(INT(x));
53 if t > x then t := t - 1.0; end;
54 return t;
55end;
56
57(* The smallest whole number not below x. *)
58proc ceil*(x: F64): F64;
59begin
60 return -floor(-x);
61end;
62
63(* The whole number nearest x, a half away from 0: the distance to
64 floor(|x|) is exact, where x + 0.5 may round up. *)
65proc round*(x: F64): F64;
66var a, t: F64;
67begin
68 a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH);
69 t := floor(a);
70 if a - t >= 0.5 then t := t + 1.0; end;
71 if negative(x) then return -t; end;
72 return t;
73end;
74
75(* The whole number nearest x, a half to the even one, as IEEE 754's
76 default and Pascal's Round have it. As for round, the distance to
77 floor(|x|) is exact, where x + 0.5 may round up. *)
78proc roundEven*(x: F64): F64;
79var a, t, d: F64;
80begin
81 a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH);
82 if not (a < 4503599627370496.0) then return x; end; (* whole already, or NaN *)
83 t := floor(a);
84 d := a - t;
85 if (d > 0.5) or ((d = 0.5) and (INT(t) & 1 = 1)) then t := t + 1.0; end;
86 if negative(x) then return -t; end;
87 return t;
88end;
89
90(* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits,
91 in two steps where k is past what an F64's exponent holds. *)
92proc scale(x: F64; k: INT): F64;
93begin
94 if k > 1023 then x := x * fromBits(U64(2046) shl 52); DEC(k, 1023); end;
95 if k < -1022 then x := x * fromBits(U64(1 + 53) shl 52); INC(k, 1022 - 53); end;
96 return x * fromBits(U64(k + 1023) shl 52);
97end;
98
99(* e^x = 2^k e^r, with r = x - k ln 2 at most ln 2 / 2 across. *)
100proc exp*(x: F64): F64;
101var k: INT; hi, lo, r, rr, c: F64;
102begin
103 if x # x then return x; end;
104 if x > 709.782712893383973096 then return x * fromBits(U64(2046) shl 52); end; (* too big: inf *)
105 if x < -745.13321910194110842 then return 0.0; end;
106 if x < 0.0 then k := INT(x * 1.44269504088896338700 - 0.5);
107 else k := INT(x * 1.44269504088896338700 + 0.5);
108 end;
109 hi := x - F64(k) * ln2hi;
110 lo := F64(k) * ln2lo;
111 r := hi - lo;
112 rr := r * r;
113 c := r - rr * (1.66666666666666019037E-01 + rr * (-2.77777777770155933842E-03
114 + rr * (6.61375632143793436117E-05 + rr * (-1.65339022054652515390E-06
115 + rr * 4.13813679705723846039E-08))));
116 return scale(1.0 + (r * c / (2.0 - c) - lo + hi), k);
117end;
118
119(* e^x - 1, for x from -45 to 45, as fdlibm's expm1: like exp, but
120 the 1 is taken out of the sum, so that no digit is lost near 0.
121 With x = k ln 2 + r, e^x - 1 = 2^k (e^r - 1) + 2^k - 1, and c is
122 the rounding error of r, put back at the end. *)
123proc expm1(x: F64): F64;
124var a, hi, lo, c, hfx, hxs, r1, t, e, y: F64; k: INT;
125begin
126 a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH);
127 k := 0;
128 c := 0.0;
129 if a > 0.34657359027997264 then (* more than ln 2 / 2 *)
130 if a < 1.0397207708399179 then (* less than 3 ln 2 / 2 *)
131 if negative(x) then k := -1; else k := 1; end;
132 elsif negative(x) then k := INT(x * 1.44269504088896338700 - 0.5);
133 else k := INT(x * 1.44269504088896338700 + 0.5);
134 end;
135 hi := x - F64(k) * ln2hi;
136 lo := F64(k) * ln2lo;
137 x := hi - lo;
138 c := (hi - x) - lo;
139 elsif a < 5.551115123125783E-17 then return x; (* below 2^-54 *)
140 end;
141 hfx := 0.5 * x;
142 hxs := x * hfx;
143 r1 := 1.0 + hxs * (-3.33333333333331316428E-02 + hxs * (1.58730158725481460165E-03
144 + hxs * (-7.93650757867487942473E-05 + hxs * (4.00821782732936239552E-06
145 + hxs * (-2.01099218183624371326E-07)))));
146 t := 3.0 - r1 * hfx;
147 e := hxs * ((r1 - t) / (6.0 - x * t));
148 if k = 0 then return x - (x * e - hxs); end;
149 e := (x * (e - c) - c) - hxs;
150 if k = -1 then return 0.5 * (x - e) - 0.5; end;
151 if k = 1 then
152 if x < -0.25 then return -2.0 * (e - (x + 0.5)); end;
153 return 1.0 + 2.0 * (x - e);
154 end;
155 if k <= -2 then return scale(1.0 - (e - x), k) - 1.0; end;
156 if k < 20 then y := (1.0 - scale(1.0, -k)) - (e - x);
157 else y := (x - (e + scale(1.0, -k))) + 1.0;
158 end;
159 return scale(y, k);
160end;
161
162(* The hyperbolic tangent of x, from -1 to 1: fdlibm's, from
163 expm1(2|x|), which is accurate for a small x where 1 - 2/(e^2x + 1)
164 would lose every digit. From 22 on it is 1 to the last bit. *)
165proc tanh*(x: F64): F64;
166var a, t, z: F64;
167begin
168 if x # x then return x; end;
169 a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH);
170 if a >= 22.0 then z := 1.0;
171 elsif a < 2.7755575615628914E-17 then return x; (* below 2^-55: tanh x = x; keeps -0 *)
172 elsif a >= 1.0 then
173 t := expm1(2.0 * a);
174 z := 1.0 - 2.0 / (t + 2.0);
175 else
176 t := expm1(-2.0 * a);
177 z := -t / (t + 2.0);
178 end;
179 if negative(x) then return -z; end;
180 return z;
181end;
182
183(* ln x = k ln 2 + ln m, with m from sqrt(2)/2 to sqrt(2): ln m is
184 found through s = f / (2 + f), f = m - 1, whose series converges
185 fast. *)
186proc ln*(x: F64): F64;
187var u: U64; k: INT; f, hfsq, s, z, w, r: F64;
188begin
189 if x # x then return x; end;
190 if x < 0.0 then return (x - x) / 0.0; end; (* NaN *)
191 if x = 0.0 then return -1.0 / 0.0; end; (* -inf *)
192 if x > 1.7976931348623157E308 then return x; end;
193 if x < 2.2250738585072014E-308 then (* subnormal: made normal *)
194 x := x * 18014398509481984.0; (* 2^54 *)
195 k := -54;
196 end;
197 (* the exponent, counted from m's lower end, and the bits of m *)
198 u := bits(x) + (3FF0000000000000H - 3FE6A09E00000000H);
199 INC(k, INT(u shr 52) - 3FFH);
200 f := fromBits((u & 0FFFFFFFFFFFFFH) + 3FE6A09E00000000H) - 1.0;
201 hfsq := 0.5 * f * f;
202 s := f / (2.0 + f);
203 z := s * s;
204 w := z * z;
205 r := w * (3.999999999940941908E-01 + w * (2.222219843214978396E-01 + w * 1.531383769920937332E-01))
206 + z * (6.666666666666735130E-01 + w * (2.857142874366239149E-01
207 + w * (1.818357216161805012E-01 + w * 1.479819860511658591E-01)));
208 return s * (hfsq + r) + F64(k) * ln2lo - hfsq + f + F64(k) * ln2hi;
209end;
210
211(* pow and the logarithms after fdlibm's pow, which works on the two
212 32-bit halves of an F64: the high one signed, with the sign and
213 exponent, and the low one. *)
214proc high(x: F64): INT;
215var h: INT;
216begin
217 h := INT(bits(x) shr 32);
218 if h >= 80000000H then DEC(h, 100000000H); end;
219 return h;
220end;
221
222proc low(x: F64): INT;
223begin
224 return INT(bits(x) & 0FFFFFFFFH);
225end;
226
227(* x with its low half 0: at most 21 bits, so that the product of two
228 such is exact. *)
229proc noLow(x: F64): F64;
230begin
231 return fromBits(bits(x) & 0FFFFFFFF00000000H);
232end;
233
234proc withHigh(x: F64; h: INT): F64;
235begin
236 return fromBits(U64(h & 0FFFFFFFFH) shl 32 | bits(x) & 0FFFFFFFFH);
237end;
238
239(* log2 x, for a finite x above 0, as hi + lo to about 2^-64 of it,
240 hi with its low half 0. x = 2^n m, with m from 1 to sqrt(3); ln m
241 comes from s = (m - b)/(m + b), with b 1 or 1.5 as m is nearest, and
242 the series in s, made exact to its first terms in two parts. *)
243proc log2Parts(x: F64): (F64, F64);
244var
245 ix, j, k, n: INT;
246 u, v, ss, sh, sl, th, tl, s2, r, ph, pl, zh, zl, t, hi, b, dh, dl: F64;
247begin
248 n := 0;
249 ix := high(x);
250 if ix < 00100000H then (* subnormal: made normal *)
251 x := x * 9007199254740992.0; (* 2^53 *)
252 n := -53;
253 ix := high(x);
254 end;
255 INC(n, (ix shr 20) - 3FFH);
256 j := ix & 000FFFFFH;
257 ix := j | 3FF00000H;
258 if j <= 3988EH then k := 0; (* m below sqrt(3/2) *)
259 elsif j < 0BB67AH then k := 1; (* below sqrt(3) *)
260 else
261 k := 0;
262 INC(n);
263 DEC(ix, 00100000H);
264 end;
265 x := withHigh(x, ix);
266 if k = 0 then b := 1.0; dh := 0.0; dl := 0.0;
267 else b := 1.5; dh := 5.84962487220764160156E-01; dl := 1.35003920212974897128E-08; (* log2 1.5 *)
268 end;
269 u := x - b;
270 v := 1.0 / (x + b);
271 ss := u * v;
272 sh := noLow(ss);
273 th := withHigh(0.0, ((ix shr 1) | 20000000H) + 00080000H + k shl 18); (* x + b, its high half *)
274 tl := x - (th - b);
275 sl := v * ((u - sh * th) - sh * tl);
276 s2 := ss * ss;
277 r := s2 * s2 * (5.99999999999994648725E-01 + s2 * (4.28571428578550184252E-01 + s2 * (3.33333329818377432918E-01
278 + s2 * (2.72728123808534006489E-01 + s2 * (2.30660745775561754067E-01 + s2 * 2.06975017800338417784E-01)))));
279 r := r + sl * (sh + ss);
280 s2 := sh * sh;
281 th := noLow(3.0 + s2 + r);
282 tl := r - ((th - 3.0) - s2);
283 u := sh * th;
284 v := sl * th + tl * ss;
285 ph := noLow(u + v);
286 pl := v - (ph - u);
287 (* 2/(3 ln 2), in two parts, times p *)
288 zh := 9.61796700954437255859E-01 * ph;
289 zl := pl * 9.61796693925975554329E-01 - 7.02846165095275826516E-09 * ph + dl;
290 t := F64(n);
291 hi := noLow(((zh + zl) + dh) + t);
292 return hi, zl - (((hi - t) - dh) - zh);
293end;
294
295(* The logarithm to base 2 of x; exact for a power of 2. *)
296proc log2*(x: F64): F64;
297var hi, lo: F64;
298begin
299 if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x) * 1.44269504088896338700; end;
300 hi, lo := log2Parts(x);
301 return hi + lo;
302end;
303
304(* The logarithm to base 10 of x: log2 x times log10 2, each in two
305 parts, the high parts' product exact. *)
306proc log10*(x: F64): F64;
307var hi, lo: F64;
308begin
309 if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x); end;
310 hi, lo := log2Parts(x);
311 return hi * 0.30102992057800293 + (hi * 7.508597826552624E-08 + lo * 0.3010299956639812);
312end;
313
314(* x to the power y, as fdlibm's pow: within an ulp, and exact for a
315 whole result an F64 holds. log2 |x| in two parts, times y in two
316 parts, is z, and the result 2^z: 2^n times 2^r, for r at most 1/2
317 across, from a series. *)
318proc pow*(x, y: F64): F64;
319var
320 hx, hy, ix, iy, lx, ly, i, j, k, n, yisint: INT;
321 ax, z, ph, pl, y1, t1, t2, r, s, t, u, v, w: F64;
322begin
323 hx := high(x); lx := low(x); hy := high(y); ly := low(y);
324 ix := hx & 7FFFFFFFH;
325 iy := hy & 7FFFFFFFH;
326 if iy | ly = 0 then return 1.0; end; (* x^0, even for a NaN *)
327 if (hx = 3FF00000H) and (lx = 0) then return 1.0; end; (* 1^y, even for a NaN *)
328 if (x # x) or (y # y) then return x + y; end;
329 (* whether y is whole, as x < 0 needs to know: 0 not, 1 odd, 2 even *)
330 yisint := 0;
331 if hx < 0 then
332 if iy >= 43400000H then yisint := 2; (* 2^53 or more *)
333 elsif iy >= 3FF00000H then
334 k := (iy shr 20) - 3FFH;
335 if k > 20 then
336 j := ly shr (52 - k);
337 if j shl (52 - k) = ly then yisint := 2 - (j & 1); end;
338 elsif ly = 0 then
339 j := iy shr (20 - k);
340 if j shl (20 - k) = iy then yisint := 2 - (j & 1); end;
341 end;
342 end;
343 end;
344 if ly = 0 then
345 if iy = 7FF00000H then (* y an infinity *)
346 if (ix - 3FF00000H) | lx = 0 then return 1.0; end; (* (-1)^inf *)
347 if ix >= 3FF00000H then
348 if hy >= 0 then return y; end;
349 return 0.0;
350 end;
351 if hy < 0 then return -y; end;
352 return 0.0;
353 end;
354 if iy = 3FF00000H then
355 if hy < 0 then return 1.0 / x; end;
356 return x;
357 end;
358 if hy = 40000000H then return x * x; end;
359 if (hy = 3FE00000H) and (hx >= 0) then return sqrt(x); end;
360 end;
361 ax := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH);
362 if (lx = 0) and ((ix = 7FF00000H) or (ix = 0) or (ix = 3FF00000H)) then (* x 0, 1 or an infinity *)
363 z := ax;
364 if hy < 0 then z := 1.0 / z; end;
365 if hx < 0 then
366 if (ix - 3FF00000H) | yisint = 0 then z := (z - z) / (z - z); (* (-1)^y, y not whole *)
367 elsif yisint = 1 then z := -z;
368 end;
369 end;
370 return z;
371 end;
372 if (hx < 0) and (yisint = 0) then return (x - x) / (x - x); end; (* NaN *)
373 s := 1.0;
374 if (hx < 0) and (yisint = 1) then s := -1.0; end;
375 if iy > 41E00000H then (* |y| above 2^31 *)
376 if iy > 43F00000H then (* above 2^64: x^y over- or underflows *)
377 if (ix <= 3FEFFFFFH) = (hy < 0) then return 1.0E300 * 1.0E300; end;
378 return 1.0E-300 * 1.0E-300;
379 end;
380 if ix < 3FEFFFFFH then
381 if hy < 0 then return s * 1.0E300 * 1.0E300; end;
382 return s * 1.0E-300 * 1.0E-300;
383 end;
384 if ix > 3FF00000H then
385 if hy > 0 then return s * 1.0E300 * 1.0E300; end;
386 return s * 1.0E-300 * 1.0E-300;
387 end;
388 (* |x - 1| is at most 2^-20: log2 x from the series of ln(1 + t) *)
389 t := ax - 1.0;
390 w := t * t * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
391 u := 1.44269502162933349609E+00 * t;
392 v := t * 1.92596299112661746887E-08 - w * 1.44269504088896338700;
393 t1 := noLow(u + v);
394 t2 := v - (t1 - u);
395 else
396 t1, t2 := log2Parts(ax);
397 end;
398 (* z = y log2 x, as ph + pl *)
399 y1 := noLow(y);
400 pl := (y - y1) * t1 + y * t2;
401 ph := y1 * t1;
402 z := pl + ph;
403 j := high(z);
404 i := low(z);
405 if j >= 40900000H then (* z at least 1024 *)
406 if ((j - 40900000H) | i # 0) or (pl + 8.0085662595372944372E-17 > z - ph) then
407 return s * 1.0E300 * 1.0E300;
408 end;
409 elsif j & 7FFFFFFFH >= 4090CC00H then (* z at most -1075 *)
410 if (j # -3F6F3400H) or (i # 0) or (pl <= z - ph) then return s * 1.0E-300 * 1.0E-300; end;
411 end;
412 (* 2^z: n the nearest whole number, taken from ph *)
413 i := j & 7FFFFFFFH;
414 k := (i shr 20) - 3FFH;
415 n := 0;
416 if i > 3FE00000H then
417 n := j + 00100000H shr (k + 1);
418 k := ((n & 7FFFFFFFH) shr 20) - 3FFH;
419 t := withHigh(0.0, n - (n & (000FFFFFH shr k)));
420 n := ((n & 000FFFFFH) | 00100000H) shr (20 - k);
421 if j < 0 then n := -n; end;
422 ph := ph - t;
423 end;
424 t := noLow(pl + ph);
425 u := t * 6.93147182464599609375E-01;
426 v := (pl - (t - ph)) * 6.93147180559945286227E-01 - t * 1.90465429995776804525E-09;
427 z := u + v;
428 w := v - (z - u);
429 t := z * z;
430 t1 := z - t * (1.66666666666666019037E-01 + t * (-2.77777777770155933842E-03
431 + t * (6.61375632143793436117E-05 + t * (-1.65339022054652515390E-06 + t * 4.13813679705723846039E-08))));
432 r := (z * t1) / (t1 - 2.0) - (w + z * w);
433 z := 1.0 - (r - z);
434 j := high(z) + n shl 20;
435 if j < 00100000H then z := scale(z, n); (* a subnormal result *)
436 else z := withHigh(z, j);
437 end;
438 return s * z;
439end;
440
441(* a times b: the low 64 bits of the product, and the high. *)
442proc mul*(a, b: U64): (U64, U64);
443begin
444 return Arith.mul(a, b);
445end;
446
447(* hi, lo (a 128-bit number, hi its high word) divided by d: the quotient and the remainder.
448 hi >= d, whose quotient doesn't fit a word, and d = 0 stop the program, as a division by zero does. *)
449proc divMod*(hi, lo, d: U64): (U64, U64);
450begin
451 if hi >= d then Rt.stop("Math.divMod: hi >= d, the quotient doesn't fit a word"); end;
452 return Arith.div128(hi, lo, d);
453end divMod;
454
455(* 64 bits of t from bit s on, the first bit of t[0] being 0. *)
456proc window(t: []const U64; s: INT): U64;
457var q, r: INT;
458begin
459 q := s div 64;
460 r := s mod 64;
461 if r = 0 then return t[q]; end;
462 return t[q] shl r | t[q + 1] shr (64 - r);
463end;
464
465(* reduce's work, for an x too big for it, by Payne and Hanek's way.
466 x is m 2^e, m a whole number of 53 bits, so x 2/pi is m times the
467 bits of 2/pi moved by e: those that land at 4 or above add whole
468 turns, which change no sine, and are left out; the 192 after them
469 give the two bits of n mod 4 and the fraction past n, exact far
470 beyond an F64. *)
471proc reduceLarge(x: F64): (INT, F64, F64);
472var
473 t: [21]U64; (* 2/pi's bits after its point, behind a word of zeros *)
474 m, w0, w1, w2, p0, p1, p2, lo: U64; e, s, n, k: INT; y: F64; neg, up: BOOL;
475begin
476 t := [0, 0A2F9836E4E441529H, 0FC2757D1F534DDC0H, 0DB6295993C439041H, 0FE5163ABDEBBC561H,
477 0B7246E3A424DD2E0H, 06492EEA09D1921CH, 0FE1DEB1CB129A73EH, 0E88235F52EBB4484H,
478 0E99C7026B45F7E41H, 03991D639835339F4H, 09C845F8BBDF9283BH, 01FF897FFDE05980FH,
479 0EF2F118B5A0A6D1FH, 06D367ECF27CB09B7H, 04F463F669E5FEA2DH, 07527BAC7EBE5F17BH,
480 03D0739F78A5292EAH, 06BFB5FB11F8D5D08H, 056033046FC7B6BABH, 0F0CFBC209AF4361DH];
481 neg := x < 0.0;
482 if neg then x := -x; end;
483 m := bits(x) & 0FFFFFFFFFFFFFH | 10000000000000H;
484 e := INT(bits(x) shr 52) - 1075;
485 (* W, the 192 bits of 2/pi from the one worth 2^(1 - e), so that
486 x 2/pi is m W 2^-190, of which the bits from 2^192 up are left out *)
487 s := e + 62;
488 w0 := window(t, s);
489 w1 := window(t, s + 64);
490 w2 := window(t, s + 128);
491 p0, p1 := mul(m, w2);
492 lo, p2 := mul(m, w1);
493 INC(p1, lo);
494 INC(p2, m * w0);
495 if p1 < lo then INC(p2); end;
496 (* n is the two bits from 2^190, the fraction those below; from a
497 half up, n is the next and the fraction 1 less, taken as 1 less
498 the fraction, negated: its bits complemented, one in 2^190 short *)
499 n := INT(p2 shr 62);
500 p2 := p2 & 3FFFFFFFFFFFFFFFH;
501 up := p2 shr 61 = 1;
502 if up then
503 INC(n);
504 p0 := ~p0; p1 := ~p1; p2 := ~p2 & 3FFFFFFFFFFFFFFFH;
505 end;
506 (* the fraction as an F64, from the 64 bits that start at its first one *)
507 k := 0;
508 while (p2 shr 61 = 0) and (k < 128) do
509 p2 := p2 shl 1 | p1 shr 63;
510 p1 := p1 shl 1 | p0 shr 63;
511 p0 := p0 shl 1;
512 INC(k);
513 end;
514 y := scale(F64(p2 shl 2 | p1 shr 62) * (pi / 2.0), -64 - k);
515 if up then y := -y; end;
516 if neg then return -n, -y, 0.0; end;
517 return n, y, 0.0;
518end;
519
520(* x less the whole number n of pi/2 nearest it, as y + yy, more exact
521 than one F64; and n. pi/2 is taken in three parts of 33 bits, each
522 with what is left of it after, so that n times each is exact while
523 n is below 2^20, about x = 1.6E6. *)
524proc reduce(x: F64): (INT, F64, F64);
525var n: INT; fn, r, t, w, y: F64;
526begin
527 if (x > 1.6E6) or (x < -1.6E6) then return reduceLarge(x); end;
528 if x < 0.0 then n := INT(x * 6.36619772367581382433E-01 - 0.5);
529 else n := INT(x * 6.36619772367581382433E-01 + 0.5);
530 end;
531 fn := F64(n);
532 r := x - fn * 1.57079632673412561417E+00;
533 t := r;
534 w := fn * 6.07710050630396597660E-11;
535 r := t - w;
536 w := fn * 2.02226624879595063154E-21 - ((t - r) - w);
537 t := r;
538 w := fn * 2.02226624871116645580E-21;
539 r := t - w;
540 w := fn * 8.47842766036889956997E-32 - ((t - r) - w);
541 y := r - w;
542 return n, y, (r - y) - w;
543end;
544
545(* sin and cos of x + y, x at most pi/4 and y far below it. *)
546proc kernelSin(x, y: F64): F64;
547var z, v, r: F64;
548begin
549 z := x * x;
550 v := z * x;
551 r := 8.33333333332248946124E-03 + z * (-1.98412698298579493134E-04 + z * 2.75573137070700676789E-06)
552 + z * z * z * (-2.50507602534068634195E-08 + z * 1.58969099521155010221E-10);
553 return x - ((z * (0.5 * y - v * r) - y) + v * 1.66666666666666324348E-01);
554end;
555
556proc kernelCos(x, y: F64): F64;
557var z, r, hz, w: F64;
558begin
559 z := x * x;
560 r := z * (4.16666666666666019037E-02 + z * (-1.38888888888741095749E-03 + z * 2.48015872894767294178E-05))
561 + z * z * z * z * (-2.75573143513906633035E-07 + z * (2.08757232129817482790E-09
562 - z * 1.13596475577881948265E-11));
563 hz := 0.5 * z;
564 w := 1.0 - hz;
565 return w + (((1.0 - w) - hz) + (z * r - x * y));
566end;
567
568(* The sine of x, in radians; NaN for an infinity or NaN. *)
569proc sin*(x: F64): F64;
570var n: INT; y, yy: F64;
571begin
572 if (x = 0.0) or (x # x) then return x; end;
573 if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *)
574 n, y, yy := reduce(x);
575 n := n & 3;
576 if n = 0 then return kernelSin(y, yy);
577 elsif n = 1 then return kernelCos(y, yy);
578 elsif n = 2 then return -kernelSin(y, yy);
579 end;
580 return -kernelCos(y, yy);
581end;
582
583(* The cosine of x, in radians; NaN for an infinity or NaN. *)
584proc cos*(x: F64): F64;
585var n: INT; y, yy: F64;
586begin
587 if (x - x) # 0.0 then return x - x; end; (* an infinity or NaN: NaN *)
588 n, y, yy := reduce(x);
589 n := n & 3;
590 if n = 0 then return kernelCos(y, yy);
591 elsif n = 1 then return -kernelSin(y, yy);
592 elsif n = 2 then return -kernelCos(y, yy);
593 end;
594 return kernelSin(y, yy);
595end;
596
597(* tan(x + y), for x + y at most pi/4 across and y below an ulp of x,
598 or, for k -1, -1/tan(x + y): fdlibm's. Past 0.6744, tan(pi/4 - x)
599 is found and turned back; -1/w is made exact to its first part. *)
600proc kernelTan(x, y: F64; k: INT): F64;
601var hx, ix: INT; z, r, v, w, s, a, t: F64;
602begin
603 hx := high(x);
604 ix := hx & 7FFFFFFFH;
605 if ix < 3E300000H then (* below 2^-28 *)
606 if k = 1 then return x; end;
607 if x = 0.0 then return 1.0 / fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); end;
608 w := x + y;
609 z := noLow(w);
610 v := y - (z - x);
611 a := -1.0 / w;
612 t := noLow(a);
613 s := 1.0 + t * z;
614 return t + a * (s + t * v);
615 end;
616 if ix >= 3FE59428H then
617 if hx < 0 then x := -x; y := -y; end;
618 z := 7.85398163397448278999E-01 - x; (* pi/4, in two parts *)
619 w := 3.06161699786838301793E-17 - y;
620 x := z + w;
621 y := 0.0;
622 end;
623 z := x * x;
624 w := z * z;
625 r := 1.33333333333201242699E-01 + w * (2.18694882948595424599E-02 + w * (3.59207910759131235356E-03
626 + w * (5.88041240820264096874E-04 + w * (7.81794442939557092300E-05 - w * 1.85586374855275456654E-05))));
627 v := z * (5.39682539762260521377E-02 + w * (8.86323982359930005737E-03 + w * (1.45620945432529025516E-03
628 + w * (2.46463134818469906812E-04 + w * (7.14072491382608190305E-05 + w * 2.59073051863633712884E-05)))));
629 s := z * x;
630 r := y + z * (s * (r + v) + y);
631 r := r + 3.33333333333334091986E-01 * s;
632 w := x + r;
633 if ix >= 3FE59428H then
634 v := F64(k);
635 w := v - 2.0 * (x - (w * w / (w + v) - r));
636 if hx < 0 then return -w; end;
637 return w;
638 end;
639 if k = 1 then return w; end;
640 z := noLow(w);
641 v := r - (z - x);
642 a := -1.0 / w;
643 t := noLow(a);
644 s := 1.0 + t * z;
645 return t + a * (s + t * v);
646end;
647
648(* The tangent of x, in radians; NaN for an infinity or NaN. *)
649proc tan*(x: F64): F64;
650var n: INT; y, yy: F64;
651begin
652 if (x = 0.0) or (x # x) then return x; end;
653 if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *)
654 n, y, yy := reduce(x);
655 return kernelTan(y, yy, 1 - (n & 1) * 2);
656end;
657
658(* The arc tangent of x, at least 0: atan(x) = atan(c) + atan(t), t
659 small, for c one of 1/2, 1, 3/2 and infinity, as x is nearest; the
660 atan(c) in two parts as pi is. *)
661proc atanAbove(x: F64): F64;
662var hi, lo, z, w, s1, s2: F64;
663begin
664 if x < 0.4375 then
665 hi := 0.0; lo := 0.0;
666 elsif x < 0.6875 then
667 hi := 4.63647609000806093515E-01; lo := 2.26987774529616870924E-17;
668 x := (2.0 * x - 1.0) / (2.0 + x);
669 elsif x < 1.1875 then
670 hi := 7.85398163397448278999E-01; lo := 3.06161699786838301793E-17;
671 x := (x - 1.0) / (x + 1.0);
672 elsif x < 2.4375 then
673 hi := 9.82793723247329054082E-01; lo := 1.39033110312309984516E-17;
674 x := (x - 1.5) / (1.0 + 1.5 * x);
675 else
676 hi := 1.57079632679489655800E+00; lo := 6.12323399573676603587E-17;
677 x := -1.0 / x;
678 end;
679 z := x * x;
680 w := z * z;
681 s1 := z * (3.33333333333329318027E-01 + w * (1.42857142725034663711E-01 + w * (9.09088713343650656196E-02
682 + w * (6.66107313738753120669E-02 + w * (4.97687799461593236017E-02 + w * 1.62858201153657823623E-02)))));
683 s2 := w * (-1.99999999998764832476E-01 + w * (-1.11111104054623557880E-01 + w * (-7.69187620504482999495E-02
684 + w * (-5.83357013379057348645E-02 - w * 3.65315727442169155270E-02))));
685 return hi - ((x * (s1 + s2) - lo) - x);
686end;
687
688(* The angle of the point (x, y) from the x axis, from -pi to pi. *)
689proc atan2*(y, x: F64): F64;
690var a: F64; (* the angle from the x axis on x's side, at least 0 *)
691begin
692 if (x # x) or (y # y) then return x + y; end;
693 if y = 0.0 then
694 if not negative(x) then return y; end;
695 a := pi;
696 else
697 if ((x - x) # 0.0) and ((y - y) # 0.0) then a := pi / 4.0; (* both infinite *)
698 else
699 a := y / x;
700 if a < 0.0 then a := -a; end;
701 a := atanAbove(a);
702 end;
703 (* pi less a, with what pi lacks of its true value *)
704 if negative(x) then a := pi - (a - 1.2246467991473531772E-16); end;
705 end;
706 if negative(y) then return -a; end;
707 return a;
708end;
709
710(* The arc tangent of x, from -pi/2 to pi/2. *)
711proc atan*(x: F64): F64;
712begin
713 if x # x then return x; end;
714 if negative(x) then return -atanAbove(-x); end;
715 return atanAbove(x);
716end;
717
718(* asin(sqrt t)/sqrt t - 1, near enough, for t up to 1/4: fdlibm's
719 rational function, for asin and acos. *)
720proc arcSeries(t: F64): F64;
721begin
722 return t * (1.66666666666666657415E-01 + t * (-3.25565818622400915405E-01 + t * (2.01212532134862925881E-01
723 + t * (-4.00555345006794114027E-02 + t * (7.91534994289814532176E-04 + t * 3.47933107596021167570E-05)))))
724 / (1.0 + t * (-2.40339491173441421878E+00 + t * (2.02094576023350569471E+00
725 + t * (-6.88283971605453293030E-01 + t * 7.70381505559019352791E-02))));
726end;
727
728(* The arc sine of x, from -pi/2 to pi/2; NaN past -1 and 1. Past
729 1/2, asin x = pi/2 - 2 asin(sqrt((1 - |x|)/2)), the root in two
730 parts. *)
731proc asin*(x: F64): F64;
732var ix: INT; a, t, s, w, c, p, q: F64;
733begin
734 ix := high(x) & 7FFFFFFFH;
735 if ix >= 3FF00000H then
736 if (ix = 3FF00000H) and (low(x) = 0) then return x * 1.57079632679489655800 + x * 6.12323399573676603587E-17; end;
737 return (x - x) / (x - x); (* NaN *)
738 end;
739 if ix < 3FE00000H then (* below 1/2 *)
740 if ix < 3E400000H then return x; end; (* below 2^-27 *)
741 return x + x * arcSeries(x * x);
742 end;
743 a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH);
744 t := (1.0 - a) * 0.5;
745 s := sqrt(t);
746 if ix >= 3FEF3333H then (* past 0.975 *)
747 t := 1.57079632679489655800 - (2.0 * (s + s * arcSeries(t)) - 6.12323399573676603587E-17);
748 else
749 w := noLow(s);
750 c := (t - w * w) / (s + w);
751 p := 2.0 * s * arcSeries(t) - (6.12323399573676603587E-17 - 2.0 * c);
752 q := 7.85398163397448278999E-01 - 2.0 * w;
753 t := 7.85398163397448278999E-01 - (p - q);
754 end;
755 if negative(x) then return -t; end;
756 return t;
757end;
758
759(* The arc cosine of x, from 0 to pi; NaN past -1 and 1: pi/2 - asin x
760 near 0, and 2 asin(sqrt((1 -+ x)/2)) past 1/2 either way. *)
761proc acos*(x: F64): F64;
762var hx, ix: INT; z, s, w, c, df: F64;
763begin
764 hx := high(x);
765 ix := hx & 7FFFFFFFH;
766 if ix >= 3FF00000H then
767 if (ix = 3FF00000H) and (low(x) = 0) then
768 if hx > 0 then return 0.0; end;
769 return pi + 2.0 * 6.12323399573676603587E-17;
770 end;
771 return (x - x) / (x - x); (* NaN *)
772 end;
773 if ix < 3FE00000H then (* below 1/2 *)
774 if ix <= 3C600000H then return 1.57079632679489655800 + 6.12323399573676603587E-17; end;
775 return 1.57079632679489655800 - (x - (6.12323399573676603587E-17 - x * arcSeries(x * x)));
776 end;
777 if hx < 0 then
778 z := (1.0 + x) * 0.5;
779 s := sqrt(z);
780 w := arcSeries(z) * s - 6.12323399573676603587E-17;
781 return pi - 2.0 * (s + w);
782 end;
783 z := (1.0 - x) * 0.5;
784 s := sqrt(z);
785 df := noLow(s);
786 c := (z - df * df) / (s + df);
787 w := arcSeries(z) * s + c;
788 return 2.0 * (df + w);
789end;
790
791(* The module's test: values C's library gives, to within two units in
792 the last place, and the edges. *)
793(* Reading a real from text exactly: the F64 nearest to its digits
794 times 10 to its exponent, ties to the even one, as the compiler
795 reads a constant (src/Scan.raz). The number is a fraction of two big
796 integers, num / den, scaled by 2^e until its whole part has 53 or 54
797 bits; that part and the remainder give the bits and the rounding. *)
798const
799 MaxDigits = 780; (* significant digits kept; past them, a 1 if any left out isn't 0 *)
800
801type
802 Big = record
803 n: INT; (* words in use; 0 is zero *)
804 w: [128]U32; (* least significant first *)
805 end;
806(* 768 significant digits are as many as can matter to rounding an
807 F64, so the ones past MaxDigits matter only by being 0 or not, which
808 a last digit 1 keeps. Then den is at most 10^(330 + 781), about
809 2^3691, and num below 10^310; the shift that follows makes the
810 larger at most 54 bits longer than den, and the long division
811 shifts den 53 more: under 3,800 bits, of the 4,096 here. *)
812
813(* a := a * m + add *)
814proc bigMul(var a: Big; m, add: U32);
815var i: INT; c: U64;
816begin
817 c := U64(add);
818 for i := 0 to a.n - 1 do
819 c := U64(a.w[i]) * U64(m) + c;
820 a.w[i] := U32(c);
821 c := c shr 32;
822 end;
823 if c # 0 then
824 a.w[a.n] := U32(c);
825 INC(a.n);
826 end;
827end;
828
829proc bigShl(var a: Big; k: INT);
830var i, ws, bits: INT;
831begin
832 if a.n = 0 then return; end;
833 ws := k div 32;
834 bits := k mod 32;
835 a.w[a.n + ws] := 0;
836 for i := a.n - 1 to 0 by -1 do
837 if bits > 0 then a.w[i + ws + 1] := a.w[i + ws + 1] | (a.w[i] shr (32 - bits)); end;
838 a.w[i + ws] := a.w[i] shl bits;
839 end;
840 for i := 0 to ws - 1 do a.w[i] := 0; end;
841 INC(a.n, ws + 1);
842 while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end;
843end;
844
845proc bigShr1(var a: Big);
846var i: INT;
847begin
848 for i := 0 to a.n - 1 do
849 a.w[i] := a.w[i] shr 1;
850 if i + 1 < a.n then a.w[i] := a.w[i] | (a.w[i + 1] shl 31); end;
851 end;
852 while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end;
853end;
854
855(* -1, 0 or 1 as a is below, equal to or above b *)
856proc bigCmp(var a, b: Big): INT;
857var i: INT;
858begin
859 if a.n # b.n then
860 if a.n > b.n then return 1; end;
861 return -1;
862 end;
863 for i := a.n - 1 to 0 by -1 do
864 if a.w[i] # b.w[i] then
865 if a.w[i] > b.w[i] then return 1; end;
866 return -1;
867 end;
868 end;
869 return 0;
870end;
871
872(* a := a - b, b not above a *)
873proc bigSub(var a, b: Big);
874var i: INT; d: I64;
875begin
876 d := 0;
877 for i := 0 to a.n - 1 do
878 d := d + I64(a.w[i]);
879 if i < b.n then d := d - I64(b.w[i]); end;
880 a.w[i] := U32(d);
881 if d < 0 then d := -1; else d := 0; end; (* a borrow *)
882 end;
883 while (a.n > 0) and (a.w[a.n - 1] = 0) do DEC(a.n); end;
884end;
885
886proc bigBits(var a: Big): INT;
887var t: U32; bits: INT;
888begin
889 if a.n = 0 then return 0; end;
890 bits := (a.n - 1) * 32;
891 t := a.w[a.n - 1];
892 while t # 0 do
893 INC(bits);
894 t := t shr 1;
895 end;
896 return bits;
897end;
898
899(* Whether s[i] is a decimal digit. *)
900proc digitAt(s: []const U8; i: INT): BOOL;
901begin
902 return (i < LEN(s)) and (s[i] >= '0') and (s[i] <= '9');
903end;
904
905(* s as a real: a sign if any, digits, a point and digits if any, and E
906 or e, a sign if any and digits if any; nothing else. The F64 nearest
907 it, ties to the even one, and false if s isn't one, or is past the
908 largest F64; one below the smallest is 0. *)
909proc toReal*(s: []const U8): (F64, BOOL);
910var
911 num, den, d: Big; i, digits, e, ex: INT; neg, eneg, point, half, sticky: BOOL;
912 q, b: U64; x: F64;
913begin
914 x := 0.0;
915 num.n := 0;
916 den.n := 1;
917 den.w[0] := 1;
918 digits := 0;
919 e := 0;
920 sticky := false;
921 neg := (LEN(s) > 0) and (s[0] = '-');
922 i := 0;
923 if (LEN(s) > 0) and ((s[0] = '-') or (s[0] = '+')) then i := 1; end;
924 if not digitAt(s, i) then return x, false; end;
925 (* the digits: num, and e, the power of 10 it is to be taken to *)
926 point := false;
927 while digitAt(s, i) or not point and (i < LEN(s)) and (s[i] = '.') and digitAt(s, i + 1) do
928 if s[i] = '.' then
929 point := true;
930 elsif digits < MaxDigits then
931 bigMul(num, 10, U32(s[i] - '0'));
932 if num.n > 0 then INC(digits); end; (* counted from the first that isn't 0 *)
933 if point then DEC(e); end;
934 else
935 (* a digit not kept, which still counts if it is before the point *)
936 if s[i] # '0' then sticky := true; end;
937 if not point then INC(e); end;
938 end;
939 INC(i);
940 end;
941 if sticky then bigMul(num, 10, 1); DEC(e); end;
942 ex := 0;
943 if (i < LEN(s)) and ((s[i] = 'E') or (s[i] = 'e')) then
944 INC(i);
945 eneg := (i < LEN(s)) and (s[i] = '-');
946 if (i < LEN(s)) and ((s[i] = '+') or (s[i] = '-')) then INC(i); end;
947 if not digitAt(s, i) then return x, false; end;
948 while digitAt(s, i) do
949 if ex < 100000 then ex := ex * 10 + INT(s[i]) - '0'; end;
950 INC(i);
951 end;
952 if eneg then ex := -ex; end;
953 end;
954 if i < LEN(s) then return x, false; end;
955 INC(e, ex);
956 if neg then x := -x; end;
957 if num.n = 0 then return x, true; end;
958 if digits + e > 310 then return x, false; end; (* 10^310 is past every F64 *)
959 if digits + e < -330 then return x, true; end; (* and 10^-330 below half the least *)
960 for i := 1 to e do bigMul(num, 10, 0); end;
961 for i := 1 to -e do bigMul(den, 10, 0); end;
962 (* num / den 2^-e from 2^52 to 2^54, but not past the least exponent *)
963 e := bigBits(num) - bigBits(den) - 53;
964 if e < -1074 then e := -1074; end;
965 if e < 0 then bigShl(num, -e); else bigShl(den, e); end;
966 (* q, its whole part, by long division; num is left the remainder *)
967 d := den;
968 bigShl(d, 53);
969 q := 0;
970 for i := 53 to 0 by -1 do
971 if bigCmp(num, d) >= 0 then
972 bigSub(num, d);
973 q := q | (U64(1) shl i);
974 end;
975 bigShr1(d);
976 end;
977 if q >= U64(1) shl 53 then (* a bit too many: it rounds *)
978 half := (q & 1) # 0;
979 sticky := num.n > 0;
980 q := q shr 1;
981 INC(e);
982 else (* the remainder against half of den *)
983 bigShl(num, 1);
984 i := bigCmp(num, den);
985 half := i >= 0;
986 sticky := i > 0;
987 end;
988 if half and (sticky or ((q & 1) # 0)) then
989 INC(q);
990 if q = U64(1) shl 53 then
991 q := q shr 1;
992 INC(e);
993 end;
994 end;
995 if e > 971 then return x, false; end;
996 if q >= U64(1) shl 52 then
997 b := U64(e + 1075) shl 52 | (q & (U64(1) shl 52 - 1));
998 else
999 b := q; (* below the least normal, e is -1074 *)
1000 end;
1001 if neg then b := b | (U64(1) shl 63); end;
1002 return fromBits(b), true;
1003end;
1004
1005(* Whether toReal reads s as want, exactly, or as nothing if not ok. *)
1006proc reads(s: []const U8; want: F64; ok: BOOL): BOOL;
1007var x: F64; o: BOOL;
1008begin
1009 x, o := toReal(s);
1010 return (o = ok) and (not ok or (bits(x) = bits(want)));
1011end;
1012
1013proc testToReal;
1014var b: [900]U8; i: INT;
1015begin
1016 Check.ok(reads("2", 2.0, true) and reads("-0.5", -0.5, true) and reads("1.5e-3", 1.5E-3, true)
1017 and reads("+1.25E+2", 125.0, true) and reads("0.1", 0.1, true)
1018 and reads("1.7976931348623157E308", 1.7976931348623157E308, true)
1019 and reads("4.9e-324", fromBits(1), true) and reads("1e-400", 0.0, true)
1020 and reads("-0", -0.0, true) and reads("9007199254740993", 9007199254740992.0, true)
1021 and reads("1e309", 0.0, false) and reads("1.8E308", 0.0, false), "Math.toReal");
1022 Check.ok(reads("", 0.0, false) and reads("-", 0.0, false) and reads("1.", 0.0, false)
1023 and reads(".5", 0.0, false) and reads("1e", 0.0, false) and reads("1.5.2", 0.0, false)
1024 and reads(" 1", 0.0, false) and reads("1x", 0.0, false) and reads("0x10", 0.0, false), "Math.toReal: not a real");
1025 (* 2^53 + 1, then 801 zeros and a 1: just above the half between two
1026 F64s, though the 1 is past the digits kept, so it rounds up *)
1027 COPY(b[0..16], "9007199254740993");
1028 for i := 16 to 816 do b[i] := '0'; end;
1029 b[817] := '1';
1030 COPY(b[818..823], "e-802");
1031 Check.ok(reads(b[0..823], 9007199254740994.0, true) and reads(b[0..817], 0.0, false), "Math.toReal: many digits");
1032end;
1033
1034proc near(got, want: F64): BOOL;
1035var d: F64;
1036begin
1037 d := got - want;
1038 if d < 0.0 then d := -d; end;
1039 if want < 0.0 then want := -want; end;
1040 return d <= want * 4.5E-16;
1041end;
1042
1043(* The module's test. *)
1044(* divMod against mul: the product of a and b, divided by b, gives a back. *)
1045proc testDiv;
1046var a, b, hi, lo, q, r, x: U64;
1047 i: INT;
1048begin
1049 q, r := divMod(0, 7, 2);
1050 Check.ok((q = 3) and (r = 1), "Math.divMod small");
1051 q, r := divMod(0, 12345, 1);
1052 Check.ok((q = 12345) and (r = 0), "Math.divMod by 1");
1053 q, r := divMod(5, 9, 6);
1054 Check.ok((q = 0D555555555555556H) and (r = 5), "Math.divMod known");
1055 q, r := divMod(0FFFFFFFFFFFFFFFEH, 0FFFFFFFFFFFFFFFFH, 0FFFFFFFFFFFFFFFFH);
1056 Check.ok((q = 0FFFFFFFFFFFFFFFFH) and (r = 0FFFFFFFFFFFFFFFEH), "Math.divMod maximal hi");
1057 x := 88172645463325252;
1058 for i := 1 to 20 do
1059 x := x xor (x shl 13); x := x xor (x shr 7); x := x xor (x shl 17);
1060 a := x;
1061 x := x xor (x shl 13); x := x xor (x shr 7); x := x xor (x shl 17);
1062 b := x | 1;
1063 lo, hi := mul(a, b);
1064 q, r := divMod(hi, lo, b);
1065 Check.ok((q = a) and (r = 0), "Math.divMod of a product");
1066 if b > 1 then
1067 r := b - 1;
1068 if lo + r >= lo then q, r := divMod(hi, lo + r, b); (* no carry out of lo *)
1069 Check.ok((q = a) and (r = b - 1), "Math.divMod with a remainder");
1070 end;
1071 end;
1072 end;
1073end;
1074
1075proc main*;
1076var inf, nan, hard: F64;
1077begin
1078 inf := 1.0 / 0.0;
1079 nan := inf - inf;
1080 hard := scale(6381956970095103.0, 797); (* of all F64s, the nearest a multiple of pi/2 *)
1081 Check.ok(not ((sqrt(2.0) # 1.4142135623730951) or (sqrt(0.0) # 0.0) or (sqrt(-1.0) = sqrt(-1.0))), "Math.sqrt");
1082 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)
1083 or not negative(floor(-0.0)) or (floor(nan) = floor(nan))), "Math.floor");
1084 Check.ok(not (not near(exp(1.0), 2.718281828459045) or not near(exp(-3.5), 0.0301973834223185)
1085 or not near(exp(700.0), 1.0142320547350045E304) or not near(exp(-740.0), 4.2E-322)
1086 or (exp(0.0) # 1.0) or (exp(710.0) # inf) or (exp(-746.0) # 0.0) or (exp(-inf) # 0.0)), "Math.exp");
1087 Check.ok(not (not near(ln(10.0), 2.302585092994046) or not near(ln(0.3), -1.2039728043259361)
1088 or not near(ln(1.0E-310), -713.8013788281542) or (ln(1.0) # 0.0) or (ln(0.0) # -inf)
1089 or (ln(inf) # inf) or (ln(-1.0) = ln(-1.0))), "Math.ln");
1090 Check.ok(not (not near(sin(1.0), 0.8414709848078965) or not near(sin(-100.0), 0.5063656411097588)
1091 or not near(sin(pi), 1.2246467991473532E-16) or not near(sin(1.0E6), -0.34999350217129294)
1092 or not near(sin(1.0E22), -0.8522008497671888) or not negative(sin(-0.0))
1093 or (sin(inf) = sin(inf))), "Math.sin");
1094 Check.ok(not (not near(cos(1.0), 0.5403023058681398) or not near(cos(-100.0), 0.8623188722876839)
1095 or not near(cos(pi / 2.0), 6.123233995736766E-17) or not near(cos(1.0E6), 0.9367521275331447)
1096 or not near(cos(1.0E300), -0.5753861119575491) or not near(cos(hard), -4.687165924254628E-19)
1097 or (cos(0.0) # 1.0) or (cos(nan) = cos(nan))), "Math.cos");
1098 Check.ok(not (not near(atan2(1.0, 2.0), 0.4636476090008061) or not near(atan2(-3.0, -4.0), -2.498091544796509)
1099 or not near(atan2(5.0, 0.1), 1.550798992821746) or not near(atan2(1.0, -1.0E-20), 1.5707963267948966)
1100 or (atan2(0.0, -1.0) # pi) or not negative(atan2(-0.0, 1.0)) or (atan2(-inf, inf) # -pi / 4.0)
1101 or not near(atan2(inf, -inf), 2.356194490192345) or (atan2(1.0, 0.0) # pi / 2.0)), "Math.atan2");
1102 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)
1103 or (round(2.5) # 3.0) or (round(-2.5) # -3.0) or (round(0.49999999999999994) # 0.0)
1104 or not negative(round(-0.3))), "Math.ceil, round");
1105 Check.ok(not (not near(pow(2.5, 3.7), 29.67413253642086) or not near(pow(0.3, -12.5), 3435455.4096325925)
1106 or not near(pow(1.0000001, 1.0E9), 2.6881038582144647E43) or (pow(-2.0, -3.0) # -0.125)
1107 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)
1108 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)
1109 or (pow(-0.0, -1.0) # -inf) or (pow(-2.0, 0.5) = pow(-2.0, 0.5))), "Math.pow");
1110 Check.ok(not ((log2(8.0) # 3.0) or (log2(0.5) # -1.0) or not near(log2(10.0), 3.321928094887362)
1111 or not near(log2(3.0E-310), -1028.212746914361) or (log10(1000.0) # 3.0) or (log10(1.0E22) # 22.0)
1112 or not near(log10(2.0), 0.3010299956639812) or not near(log10(7.0E-300), -299.15490195998575)
1113 or (log2(0.0) # -inf) or (log10(-1.0) = log10(-1.0))), "Math.log2, log10");
1114 Check.ok(not (not near(tan(1.0), 1.5574077246549023) or not near(tan(-1.5707963267948966), -1.633123935319537E16)
1115 or not near(tan(1.0E22), -1.6287782256068988) or not negative(tan(-0.0)) or (tan(inf) = tan(inf))), "Math.tan");
1116 Check.ok(not (not near(asin(0.3), 0.3046926540153975) or not near(asin(-0.99), -1.4292568534704693)
1117 or (asin(1.0) # pi / 2.0) or (asin(1.5) = asin(1.5)) or not near(acos(0.7), 0.7953988301841436)
1118 or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi)
1119 or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965)
1120 or (atan(inf) # pi / 2.0) or not negative(atan(-0.0))), "Math.asin, acos, atan");
1121 Check.ok(not (not near(tanh(0.5), 0.46211715726000974) or not near(tanh(1.0), 0.7615941559557649)
1122 or not near(tanh(-0.3), -0.2913126124515909) or not near(tanh(3.0), 0.9950547536867305)
1123 or not near(tanh(1.0E-10), 1.0E-10) or (tanh(-1.0E-300) # -1.0E-300) or (tanh(20.0) # 1.0)
1124 or (tanh(-30.0) # -1.0) or (tanh(inf) # 1.0) or not negative(tanh(-0.0)) or (tanh(nan) = tanh(nan))),
1125 "Math.tanh");
1126 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)
1127 or (roundEven(-2.5) # -2.0) or (roundEven(-3.5) # -4.0) or (roundEven(2.6) # 3.0) or (roundEven(-2.4) # -2.0)
1128 or not negative(roundEven(-0.5)) or negative(roundEven(0.5)) or (roundEven(0.49999999999999994) # 0.0)
1129 or (roundEven(4503599627370495.5) # 4503599627370496.0) or (roundEven(4503599627370497.0) # 4503599627370497.0)
1130 or (roundEven(-1.0E300) # -1.0E300) or (roundEven(inf) # inf) or (roundEven(nan) = roundEven(nan))), "Math.roundEven");
1131 testDiv;
1132 testToReal;
1133end;
1134
1135end Math.