| | @@ -2,10 +2,11 @@ |
| 2 | 2 | last bit or so. They follow fdlibm, Sun's library that most C ones |
| 3 | 3 | come from: each brings its argument into a small range, where a |
| 4 | 4 | polynomial is accurate, and puts the range back on the result. The |
| 5 | | - polynomials' coefficients are fdlibm's. |
| 5 | + polynomials' coefficients are fdlibm's. Each is within an ulp, a |
| 6 | + unit in the result's last place. |
| 6 | 7 | |
| 7 | | - Left out: tan, the inverse sines and powers, until a program needs |
| 8 | | - them. *) |
| 8 | + Left out: the hyperbolic functions, fmod and hypot, until a program |
| 9 | + needs them. *) |
| 9 | 10 | module Math; |
| 10 | 11 | import Rt; |
| 11 | 12 | |
| | @@ -53,6 +54,24 @@ |
| 53 | 54 | return t; |
| 54 | 55 | end; |
| 55 | 56 | |
| 57 | +(* The smallest whole number not below x. *) |
| 58 | +proc ceil*(x: F64): F64; |
| 59 | +begin |
| 60 | + return -floor(-x); |
| 61 | +end; |
| 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. *) |
| 65 | +proc round*(x: F64): F64; |
| 66 | +var a, t: F64; |
| 67 | +begin |
| 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; |
| 73 | +end; |
| 74 | + |
| 56 | 75 | (* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits, |
| 57 | 76 | in two steps where k is past what an F64's exponent holds. *) |
| 58 | 77 | proc scale(x: F64; k: INT): F64; |
| | @@ -108,8 +127,238 @@ |
| 108 | 127 | + z * (6.666666666666735130E-01 + w * (2.857142874366239149E-01 |
| 109 | 128 | + w * (1.818357216161805012E-01 + w * 1.479819860511658591E-01))); |
| 110 | 129 | return s * (hfsq + r) + F64(k) * ln2lo - hfsq + f + F64(k) * ln2hi; |
| 130 | +end; |
| 131 | + |
| 132 | +(* pow and the logarithms after fdlibm's pow, which works on the two |
| 133 | + 32-bit halves of an F64: the high one signed, with the sign and |
| 134 | + exponent, and the low one. *) |
| 135 | +proc high(x: F64): INT; |
| 136 | +var h: INT; |
| 137 | +begin |
| 138 | + h := INT(bits(x) shr 32); |
| 139 | + if h >= 80000000H then DEC(h, 100000000H); end; |
| 140 | + return h; |
| 141 | +end; |
| 142 | + |
| 143 | +proc low(x: F64): INT; |
| 144 | +begin |
| 145 | + return INT(bits(x) & 0FFFFFFFFH); |
| 146 | +end; |
| 147 | + |
| 148 | +(* x with its low half 0: at most 21 bits, so that the product of two |
| 149 | + such is exact. *) |
| 150 | +proc noLow(x: F64): F64; |
| 151 | +begin |
| 152 | + return fromBits(bits(x) & 0FFFFFFFF00000000H); |
| 153 | +end; |
| 154 | + |
| 155 | +proc withHigh(x: F64; h: INT): F64; |
| 156 | +begin |
| 157 | + return fromBits(U64(h & 0FFFFFFFFH) shl 32 | bits(x) & 0FFFFFFFFH); |
| 111 | 158 | end; |
| 112 | 159 | |
| 160 | +(* log2 x, for a finite x above 0, as hi + lo to about 2^-64 of it, |
| 161 | + hi with its low half 0. x = 2^n m, with m from 1 to sqrt(3); ln m |
| 162 | + comes from s = (m - b)/(m + b), with b 1 or 1.5 as m is nearest, and |
| 163 | + the series in s, made exact to its first terms in two parts. *) |
| 164 | +proc log2Parts(x: F64): (F64, F64); |
| 165 | +var |
| 166 | + ix, j, k, n: INT; |
| 167 | + u, v, ss, sh, sl, th, tl, s2, r, ph, pl, zh, zl, t, hi, b, dh, dl: F64; |
| 168 | +begin |
| 169 | + n := 0; |
| 170 | + ix := high(x); |
| 171 | + if ix < 00100000H then (* subnormal: made normal *) |
| 172 | + x := x * 9007199254740992.0; (* 2^53 *) |
| 173 | + n := -53; |
| 174 | + ix := high(x); |
| 175 | + end; |
| 176 | + INC(n, (ix shr 20) - 3FFH); |
| 177 | + j := ix & 000FFFFFH; |
| 178 | + ix := j | 3FF00000H; |
| 179 | + if j <= 3988EH then k := 0; (* m below sqrt(3/2) *) |
| 180 | + elsif j < 0BB67AH then k := 1; (* below sqrt(3) *) |
| 181 | + else |
| 182 | + k := 0; |
| 183 | + INC(n); |
| 184 | + DEC(ix, 00100000H); |
| 185 | + end; |
| 186 | + x := withHigh(x, ix); |
| 187 | + if k = 0 then b := 1.0; dh := 0.0; dl := 0.0; |
| 188 | + else b := 1.5; dh := 5.84962487220764160156E-01; dl := 1.35003920212974897128E-08; (* log2 1.5 *) |
| 189 | + end; |
| 190 | + u := x - b; |
| 191 | + v := 1.0 / (x + b); |
| 192 | + ss := u * v; |
| 193 | + sh := noLow(ss); |
| 194 | + th := withHigh(0.0, ((ix shr 1) | 20000000H) + 00080000H + k shl 18); (* x + b, its high half *) |
| 195 | + tl := x - (th - b); |
| 196 | + sl := v * ((u - sh * th) - sh * tl); |
| 197 | + s2 := ss * ss; |
| 198 | + r := s2 * s2 * (5.99999999999994648725E-01 + s2 * (4.28571428578550184252E-01 + s2 * (3.33333329818377432918E-01 |
| 199 | + + s2 * (2.72728123808534006489E-01 + s2 * (2.30660745775561754067E-01 + s2 * 2.06975017800338417784E-01))))); |
| 200 | + r := r + sl * (sh + ss); |
| 201 | + s2 := sh * sh; |
| 202 | + th := noLow(3.0 + s2 + r); |
| 203 | + tl := r - ((th - 3.0) - s2); |
| 204 | + u := sh * th; |
| 205 | + v := sl * th + tl * ss; |
| 206 | + ph := noLow(u + v); |
| 207 | + pl := v - (ph - u); |
| 208 | + (* 2/(3 ln 2), in two parts, times p *) |
| 209 | + zh := 9.61796700954437255859E-01 * ph; |
| 210 | + zl := pl * 9.61796693925975554329E-01 - 7.02846165095275826516E-09 * ph + dl; |
| 211 | + t := F64(n); |
| 212 | + hi := noLow(((zh + zl) + dh) + t); |
| 213 | + return hi, zl - (((hi - t) - dh) - zh); |
| 214 | +end; |
| 215 | + |
| 216 | +(* The logarithm to base 2 of x; exact for a power of 2. *) |
| 217 | +proc log2*(x: F64): F64; |
| 218 | +var hi, lo: F64; |
| 219 | +begin |
| 220 | + if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x) * 1.44269504088896338700; end; |
| 221 | + hi, lo := log2Parts(x); |
| 222 | + return hi + lo; |
| 223 | +end; |
| 224 | + |
| 225 | +(* The logarithm to base 10 of x: log2 x times log10 2, each in two |
| 226 | + parts, the high parts' product exact. *) |
| 227 | +proc log10*(x: F64): F64; |
| 228 | +var hi, lo: F64; |
| 229 | +begin |
| 230 | + if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x); end; |
| 231 | + hi, lo := log2Parts(x); |
| 232 | + return hi * 0.30102992057800293 + (hi * 7.508597826552624E-08 + lo * 0.3010299956639812); |
| 233 | +end; |
| 234 | + |
| 235 | +(* x to the power y, as fdlibm's pow: within an ulp, and exact for a |
| 236 | + whole result an F64 holds. log2 |x| in two parts, times y in two |
| 237 | + parts, is z, and the result 2^z: 2^n times 2^r, for r at most 1/2 |
| 238 | + across, from a series. *) |
| 239 | +proc pow*(x, y: F64): F64; |
| 240 | +var |
| 241 | + hx, hy, ix, iy, lx, ly, i, j, k, n, yisint: INT; |
| 242 | + ax, z, ph, pl, y1, t1, t2, r, s, t, u, v, w: F64; |
| 243 | +begin |
| 244 | + hx := high(x); lx := low(x); hy := high(y); ly := low(y); |
| 245 | + ix := hx & 7FFFFFFFH; |
| 246 | + iy := hy & 7FFFFFFFH; |
| 247 | + if iy | ly = 0 then return 1.0; end; (* x^0, even for a NaN *) |
| 248 | + if (hx = 3FF00000H) and (lx = 0) then return 1.0; end; (* 1^y, even for a NaN *) |
| 249 | + if (x # x) or (y # y) then return x + y; end; |
| 250 | + (* whether y is whole, as x < 0 needs to know: 0 not, 1 odd, 2 even *) |
| 251 | + yisint := 0; |
| 252 | + if hx < 0 then |
| 253 | + if iy >= 43400000H then yisint := 2; (* 2^53 or more *) |
| 254 | + elsif iy >= 3FF00000H then |
| 255 | + k := (iy shr 20) - 3FFH; |
| 256 | + if k > 20 then |
| 257 | + j := ly shr (52 - k); |
| 258 | + if j shl (52 - k) = ly then yisint := 2 - (j & 1); end; |
| 259 | + elsif ly = 0 then |
| 260 | + j := iy shr (20 - k); |
| 261 | + if j shl (20 - k) = iy then yisint := 2 - (j & 1); end; |
| 262 | + end; |
| 263 | + end; |
| 264 | + end; |
| 265 | + if ly = 0 then |
| 266 | + if iy = 7FF00000H then (* y an infinity *) |
| 267 | + if (ix - 3FF00000H) | lx = 0 then return 1.0; end; (* (-1)^inf *) |
| 268 | + if ix >= 3FF00000H then |
| 269 | + if hy >= 0 then return y; end; |
| 270 | + return 0.0; |
| 271 | + end; |
| 272 | + if hy < 0 then return -y; end; |
| 273 | + return 0.0; |
| 274 | + end; |
| 275 | + if iy = 3FF00000H then |
| 276 | + if hy < 0 then return 1.0 / x; end; |
| 277 | + return x; |
| 278 | + end; |
| 279 | + if hy = 40000000H then return x * x; end; |
| 280 | + if (hy = 3FE00000H) and (hx >= 0) then return sqrt(x); end; |
| 281 | + end; |
| 282 | + ax := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); |
| 283 | + if (lx = 0) and ((ix = 7FF00000H) or (ix = 0) or (ix = 3FF00000H)) then (* x 0, 1 or an infinity *) |
| 284 | + z := ax; |
| 285 | + if hy < 0 then z := 1.0 / z; end; |
| 286 | + if hx < 0 then |
| 287 | + if (ix - 3FF00000H) | yisint = 0 then z := (z - z) / (z - z); (* (-1)^y, y not whole *) |
| 288 | + elsif yisint = 1 then z := -z; |
| 289 | + end; |
| 290 | + end; |
| 291 | + return z; |
| 292 | + end; |
| 293 | + if (hx < 0) and (yisint = 0) then return (x - x) / (x - x); end; (* NaN *) |
| 294 | + s := 1.0; |
| 295 | + if (hx < 0) and (yisint = 1) then s := -1.0; end; |
| 296 | + if iy > 41E00000H then (* |y| above 2^31 *) |
| 297 | + if iy > 43F00000H then (* above 2^64: x^y over- or underflows *) |
| 298 | + if (ix <= 3FEFFFFFH) = (hy < 0) then return 1.0E300 * 1.0E300; end; |
| 299 | + return 1.0E-300 * 1.0E-300; |
| 300 | + end; |
| 301 | + if ix < 3FEFFFFFH then |
| 302 | + if hy < 0 then return s * 1.0E300 * 1.0E300; end; |
| 303 | + return s * 1.0E-300 * 1.0E-300; |
| 304 | + end; |
| 305 | + if ix > 3FF00000H then |
| 306 | + if hy > 0 then return s * 1.0E300 * 1.0E300; end; |
| 307 | + return s * 1.0E-300 * 1.0E-300; |
| 308 | + end; |
| 309 | + (* |x - 1| is at most 2^-20: log2 x from the series of ln(1 + t) *) |
| 310 | + t := ax - 1.0; |
| 311 | + w := t * t * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); |
| 312 | + u := 1.44269502162933349609E+00 * t; |
| 313 | + v := t * 1.92596299112661746887E-08 - w * 1.44269504088896338700; |
| 314 | + t1 := noLow(u + v); |
| 315 | + t2 := v - (t1 - u); |
| 316 | + else |
| 317 | + t1, t2 := log2Parts(ax); |
| 318 | + end; |
| 319 | + (* z = y log2 x, as ph + pl *) |
| 320 | + y1 := noLow(y); |
| 321 | + pl := (y - y1) * t1 + y * t2; |
| 322 | + ph := y1 * t1; |
| 323 | + z := pl + ph; |
| 324 | + j := high(z); |
| 325 | + i := low(z); |
| 326 | + if j >= 40900000H then (* z at least 1024 *) |
| 327 | + if ((j - 40900000H) | i # 0) or (pl + 8.0085662595372944372E-17 > z - ph) then |
| 328 | + return s * 1.0E300 * 1.0E300; |
| 329 | + end; |
| 330 | + elsif j & 7FFFFFFFH >= 4090CC00H then (* z at most -1075 *) |
| 331 | + if (j # -3F6F3400H) or (i # 0) or (pl <= z - ph) then return s * 1.0E-300 * 1.0E-300; end; |
| 332 | + end; |
| 333 | + (* 2^z: n the nearest whole number, taken from ph *) |
| 334 | + i := j & 7FFFFFFFH; |
| 335 | + k := (i shr 20) - 3FFH; |
| 336 | + n := 0; |
| 337 | + if i > 3FE00000H then |
| 338 | + n := j + 00100000H shr (k + 1); |
| 339 | + k := ((n & 7FFFFFFFH) shr 20) - 3FFH; |
| 340 | + t := withHigh(0.0, n - (n & (000FFFFFH shr k))); |
| 341 | + n := ((n & 000FFFFFH) | 00100000H) shr (20 - k); |
| 342 | + if j < 0 then n := -n; end; |
| 343 | + ph := ph - t; |
| 344 | + end; |
| 345 | + t := noLow(pl + ph); |
| 346 | + u := t * 6.93147182464599609375E-01; |
| 347 | + v := (pl - (t - ph)) * 6.93147180559945286227E-01 - t * 1.90465429995776804525E-09; |
| 348 | + z := u + v; |
| 349 | + w := v - (z - u); |
| 350 | + t := z * z; |
| 351 | + t1 := z - t * (1.66666666666666019037E-01 + t * (-2.77777777770155933842E-03 |
| 352 | + + t * (6.61375632143793436117E-05 + t * (-1.65339022054652515390E-06 + t * 4.13813679705723846039E-08)))); |
| 353 | + r := (z * t1) / (t1 - 2.0) - (w + z * w); |
| 354 | + z := 1.0 - (r - z); |
| 355 | + j := high(z) + n shl 20; |
| 356 | + if j < 00100000H then z := scale(z, n); (* a subnormal result *) |
| 357 | + else z := withHigh(z, j); |
| 358 | + end; |
| 359 | + return s * z; |
| 360 | +end; |
| 361 | + |
| 113 | 362 | (* a times b: the low 64 bits of the product, and the high. *) |
| 114 | 363 | proc mul(a, b: U64): (U64, U64); |
| 115 | 364 | code |
| | @@ -258,12 +507,73 @@ |
| 258 | 507 | elsif n = 2 then return -kernelCos(y, yy); |
| 259 | 508 | end; |
| 260 | 509 | return kernelSin(y, yy); |
| 510 | +end; |
| 511 | + |
| 512 | +(* tan(x + y), for x + y at most pi/4 across and y below an ulp of x, |
| 513 | + or, for k -1, -1/tan(x + y): fdlibm's. Past 0.6744, tan(pi/4 - x) |
| 514 | + is found and turned back; -1/w is made exact to its first part. *) |
| 515 | +proc kernelTan(x, y: F64; k: INT): F64; |
| 516 | +var hx, ix: INT; z, r, v, w, s, a, t: F64; |
| 517 | +begin |
| 518 | + hx := high(x); |
| 519 | + ix := hx & 7FFFFFFFH; |
| 520 | + if ix < 3E300000H then (* below 2^-28 *) |
| 521 | + if k = 1 then return x; end; |
| 522 | + if x = 0.0 then return 1.0 / fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); end; |
| 523 | + w := x + y; |
| 524 | + z := noLow(w); |
| 525 | + v := y - (z - x); |
| 526 | + a := -1.0 / w; |
| 527 | + t := noLow(a); |
| 528 | + s := 1.0 + t * z; |
| 529 | + return t + a * (s + t * v); |
| 530 | + end; |
| 531 | + if ix >= 3FE59428H then |
| 532 | + if hx < 0 then x := -x; y := -y; end; |
| 533 | + z := 7.85398163397448278999E-01 - x; (* pi/4, in two parts *) |
| 534 | + w := 3.06161699786838301793E-17 - y; |
| 535 | + x := z + w; |
| 536 | + y := 0.0; |
| 537 | + end; |
| 538 | + z := x * x; |
| 539 | + w := z * z; |
| 540 | + r := 1.33333333333201242699E-01 + w * (2.18694882948595424599E-02 + w * (3.59207910759131235356E-03 |
| 541 | + + w * (5.88041240820264096874E-04 + w * (7.81794442939557092300E-05 - w * 1.85586374855275456654E-05)))); |
| 542 | + v := z * (5.39682539762260521377E-02 + w * (8.86323982359930005737E-03 + w * (1.45620945432529025516E-03 |
| 543 | + + w * (2.46463134818469906812E-04 + w * (7.14072491382608190305E-05 + w * 2.59073051863633712884E-05))))); |
| 544 | + s := z * x; |
| 545 | + r := y + z * (s * (r + v) + y); |
| 546 | + r := r + 3.33333333333334091986E-01 * s; |
| 547 | + w := x + r; |
| 548 | + if ix >= 3FE59428H then |
| 549 | + v := F64(k); |
| 550 | + w := v - 2.0 * (x - (w * w / (w + v) - r)); |
| 551 | + if hx < 0 then return -w; end; |
| 552 | + return w; |
| 553 | + end; |
| 554 | + if k = 1 then return w; end; |
| 555 | + z := noLow(w); |
| 556 | + v := r - (z - x); |
| 557 | + a := -1.0 / w; |
| 558 | + t := noLow(a); |
| 559 | + s := 1.0 + t * z; |
| 560 | + return t + a * (s + t * v); |
| 561 | +end; |
| 562 | + |
| 563 | +(* The tangent of x, in radians; NaN for an infinity or NaN. *) |
| 564 | +proc tan*(x: F64): F64; |
| 565 | +var n: INT; y, yy: F64; |
| 566 | +begin |
| 567 | + if (x = 0.0) or (x # x) then return x; end; |
| 568 | + if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *) |
| 569 | + n, y, yy := reduce(x); |
| 570 | + return kernelTan(y, yy, 1 - (n & 1) * 2); |
| 261 | 571 | end; |
| 262 | 572 | |
| 263 | 573 | (* The arc tangent of x, at least 0: atan(x) = atan(c) + atan(t), t |
| 264 | 574 | small, for c one of 1/2, 1, 3/2 and infinity, as x is nearest; the |
| 265 | 575 | atan(c) in two parts as pi is. *) |
| 266 | | -proc atan(x: F64): F64; |
| 576 | +proc atanAbove(x: F64): F64; |
| 267 | 577 | var hi, lo, z, w, s1, s2: F64; |
| 268 | 578 | begin |
| 269 | 579 | if x < 0.4375 then |
| | @@ -303,15 +613,96 @@ |
| 303 | 613 | else |
| 304 | 614 | a := y / x; |
| 305 | 615 | if a < 0.0 then a := -a; end; |
| 306 | | - a := atan(a); |
| 616 | + a := atanAbove(a); |
| 307 | 617 | end; |
| 308 | 618 | (* pi less a, with what pi lacks of its true value *) |
| 309 | 619 | if negative(x) then a := pi - (a - 1.2246467991473531772E-16); end; |
| 310 | 620 | end; |
| 311 | 621 | if negative(y) then return -a; end; |
| 312 | 622 | return a; |
| 623 | +end; |
| 624 | + |
| 625 | +(* The arc tangent of x, from -pi/2 to pi/2. *) |
| 626 | +proc atan*(x: F64): F64; |
| 627 | +begin |
| 628 | + if x # x then return x; end; |
| 629 | + if negative(x) then return -atanAbove(-x); end; |
| 630 | + return atanAbove(x); |
| 631 | +end; |
| 632 | + |
| 633 | +(* asin(sqrt t)/sqrt t - 1, near enough, for t up to 1/4: fdlibm's |
| 634 | + rational function, for asin and acos. *) |
| 635 | +proc arcSeries(t: F64): F64; |
| 636 | +begin |
| 637 | + return t * (1.66666666666666657415E-01 + t * (-3.25565818622400915405E-01 + t * (2.01212532134862925881E-01 |
| 638 | + + t * (-4.00555345006794114027E-02 + t * (7.91534994289814532176E-04 + t * 3.47933107596021167570E-05))))) |
| 639 | + / (1.0 + t * (-2.40339491173441421878E+00 + t * (2.02094576023350569471E+00 |
| 640 | + + t * (-6.88283971605453293030E-01 + t * 7.70381505559019352791E-02)))); |
| 641 | +end; |
| 642 | + |
| 643 | +(* The arc sine of x, from -pi/2 to pi/2; NaN past -1 and 1. Past |
| 644 | + 1/2, asin x = pi/2 - 2 asin(sqrt((1 - |x|)/2)), the root in two |
| 645 | + parts. *) |
| 646 | +proc asin*(x: F64): F64; |
| 647 | +var ix: INT; a, t, s, w, c, p, q: F64; |
| 648 | +begin |
| 649 | + ix := high(x) & 7FFFFFFFH; |
| 650 | + if ix >= 3FF00000H then |
| 651 | + if (ix = 3FF00000H) and (low(x) = 0) then return x * 1.57079632679489655800 + x * 6.12323399573676603587E-17; end; |
| 652 | + return (x - x) / (x - x); (* NaN *) |
| 653 | + end; |
| 654 | + if ix < 3FE00000H then (* below 1/2 *) |
| 655 | + if ix < 3E400000H then return x; end; (* below 2^-27 *) |
| 656 | + return x + x * arcSeries(x * x); |
| 657 | + end; |
| 658 | + a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); |
| 659 | + t := (1.0 - a) * 0.5; |
| 660 | + s := sqrt(t); |
| 661 | + if ix >= 3FEF3333H then (* past 0.975 *) |
| 662 | + t := 1.57079632679489655800 - (2.0 * (s + s * arcSeries(t)) - 6.12323399573676603587E-17); |
| 663 | + else |
| 664 | + w := noLow(s); |
| 665 | + c := (t - w * w) / (s + w); |
| 666 | + p := 2.0 * s * arcSeries(t) - (6.12323399573676603587E-17 - 2.0 * c); |
| 667 | + q := 7.85398163397448278999E-01 - 2.0 * w; |
| 668 | + t := 7.85398163397448278999E-01 - (p - q); |
| 669 | + end; |
| 670 | + if negative(x) then return -t; end; |
| 671 | + return t; |
| 313 | 672 | end; |
| 314 | 673 | |
| 674 | +(* The arc cosine of x, from 0 to pi; NaN past -1 and 1: pi/2 - asin x |
| 675 | + near 0, and 2 asin(sqrt((1 -+ x)/2)) past 1/2 either way. *) |
| 676 | +proc acos*(x: F64): F64; |
| 677 | +var hx, ix: INT; z, s, w, c, df: F64; |
| 678 | +begin |
| 679 | + hx := high(x); |
| 680 | + ix := hx & 7FFFFFFFH; |
| 681 | + if ix >= 3FF00000H then |
| 682 | + if (ix = 3FF00000H) and (low(x) = 0) then |
| 683 | + if hx > 0 then return 0.0; end; |
| 684 | + return pi + 2.0 * 6.12323399573676603587E-17; |
| 685 | + end; |
| 686 | + return (x - x) / (x - x); (* NaN *) |
| 687 | + end; |
| 688 | + if ix < 3FE00000H then (* below 1/2 *) |
| 689 | + if ix <= 3C600000H then return 1.57079632679489655800 + 6.12323399573676603587E-17; end; |
| 690 | + return 1.57079632679489655800 - (x - (6.12323399573676603587E-17 - x * arcSeries(x * x))); |
| 691 | + end; |
| 692 | + if hx < 0 then |
| 693 | + z := (1.0 + x) * 0.5; |
| 694 | + s := sqrt(z); |
| 695 | + w := arcSeries(z) * s - 6.12323399573676603587E-17; |
| 696 | + return pi - 2.0 * (s + w); |
| 697 | + end; |
| 698 | + z := (1.0 - x) * 0.5; |
| 699 | + s := sqrt(z); |
| 700 | + df := noLow(s); |
| 701 | + c := (z - df * df) / (s + df); |
| 702 | + w := arcSeries(z) * s + c; |
| 703 | + return 2.0 * (df + w); |
| 704 | +end; |
| 705 | + |
| 315 | 706 | (* The module's test: values C's library gives, to within two units in |
| 316 | 707 | the last place, and the edges. *) |
| 317 | 708 | proc near(got, want: F64): BOOL; |
| | @@ -351,6 +742,26 @@ |
| 351 | 742 | or not near(atan2(5.0, 0.1), 1.550798992821746) or not near(atan2(1.0, -1.0E-20), 1.5707963267948966) |
| 352 | 743 | or (atan2(0.0, -1.0) # pi) or not negative(atan2(-0.0, 1.0)) or (atan2(-inf, inf) # -pi / 4.0) |
| 353 | 744 | or not near(atan2(inf, -inf), 2.356194490192345) or (atan2(1.0, 0.0) # pi / 2.0) then Rt.stop("Math.atan2"); end; |
| 745 | + if (ceil(2.5) # 3.0) or (ceil(-2.5) # -2.0) or not negative(ceil(-0.5)) or (ceil(1.0E300) # 1.0E300) |
| 746 | + or (round(2.5) # 3.0) or (round(-2.5) # -3.0) or (round(0.49999999999999994) # 0.0) |
| 747 | + or not negative(round(-0.3)) then Rt.stop("Math.ceil, round"); end; |
| 748 | + if not near(pow(2.5, 3.7), 29.67413253642086) or not near(pow(0.3, -12.5), 3435455.4096325925) |
| 749 | + or not near(pow(1.0000001, 1.0E9), 2.6881038582144647E43) or (pow(-2.0, -3.0) # -0.125) |
| 750 | + 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) |
| 751 | + 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) |
| 752 | + or (pow(-0.0, -1.0) # -inf) or (pow(-2.0, 0.5) = pow(-2.0, 0.5)) then Rt.stop("Math.pow"); end; |
| 753 | + if (log2(8.0) # 3.0) or (log2(0.5) # -1.0) or not near(log2(10.0), 3.321928094887362) |
| 754 | + or not near(log2(3.0E-310), -1028.212746914361) or (log10(1000.0) # 3.0) or (log10(1.0E22) # 22.0) |
| 755 | + or not near(log10(2.0), 0.3010299956639812) or not near(log10(7.0E-300), -299.15490195998575) |
| 756 | + or (log2(0.0) # -inf) or (log10(-1.0) = log10(-1.0)) then Rt.stop("Math.log2, log10"); end; |
| 757 | + if not near(tan(1.0), 1.5574077246549023) or not near(tan(-1.5707963267948966), -1.633123935319537E16) |
| 758 | + or not near(tan(1.0E22), -1.6287782256068988) or not negative(tan(-0.0)) or (tan(inf) = tan(inf)) |
| 759 | + then Rt.stop("Math.tan"); end; |
| 760 | + if not near(asin(0.3), 0.3046926540153975) or not near(asin(-0.99), -1.4292568534704693) |
| 761 | + or (asin(1.0) # pi / 2.0) or (asin(1.5) = asin(1.5)) or not near(acos(0.7), 0.7953988301841436) |
| 762 | + or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi) |
| 763 | + or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965) |
| 764 | + or (atan(inf) # pi / 2.0) or not negative(atan(-0.0)) then Rt.stop("Math.asin, acos, atan"); end; |
| 354 | 765 | end; |
| 355 | 766 | |
| 356 | 767 | end Math. |