| | @@ -5,8 +5,7 @@ |
| 5 | 5 | polynomials' coefficients are fdlibm's. Each is within an ulp, a |
| 6 | 6 | unit in the result's last place. |
| 7 | 7 | |
| 8 | | - Left out: the hyperbolic functions, fmod and hypot, until a program |
| 9 | | - needs them. *) |
| 8 | + Left out: sinh, cosh, fmod and hypot, until a program needs them. *) |
| 10 | 9 | module Math; |
| 11 | 10 | import Check; |
| 12 | 11 | |
| | @@ -69,6 +68,21 @@ |
| 69 | 68 | return t; |
| 70 | 69 | end; |
| 71 | 70 | |
| 71 | +(* The whole number nearest x, a half to the even one, as IEEE 754's |
| 72 | + default and Pascal's Round have it. As for round, the distance to |
| 73 | + floor(|x|) is exact, where x + 0.5 may round up. *) |
| 74 | +proc roundEven*(x: F64): F64; |
| 75 | +var a, t, d: F64; |
| 76 | +begin |
| 77 | + a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); |
| 78 | + if not (a < 4503599627370496.0) then return x; end; (* whole already, or NaN *) |
| 79 | + t := floor(a); |
| 80 | + d := a - t; |
| 81 | + if (d > 0.5) or ((d = 0.5) and (INT(t) & 1 = 1)) then t := t + 1.0; end; |
| 82 | + if negative(x) then return -t; end; |
| 83 | + return t; |
| 84 | +end; |
| 85 | + |
| 72 | 86 | (* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits, |
| 73 | 87 | in two steps where k is past what an F64's exponent holds. *) |
| 74 | 88 | proc scale(x: F64; k: INT): F64; |
| | @@ -98,6 +112,70 @@ |
| 98 | 112 | return scale(1.0 + (r * c / (2.0 - c) - lo + hi), k); |
| 99 | 113 | end; |
| 100 | 114 | |
| 115 | +(* e^x - 1, for x from -45 to 45, as fdlibm's expm1: like exp, but |
| 116 | + the 1 is taken out of the sum, so that no digit is lost near 0. |
| 117 | + With x = k ln 2 + r, e^x - 1 = 2^k (e^r - 1) + 2^k - 1, and c is |
| 118 | + the rounding error of r, put back at the end. *) |
| 119 | +proc expm1(x: F64): F64; |
| 120 | +var a, hi, lo, c, hfx, hxs, r1, t, e, y: F64; k: INT; |
| 121 | +begin |
| 122 | + a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); |
| 123 | + k := 0; |
| 124 | + c := 0.0; |
| 125 | + if a > 0.34657359027997264 then (* more than ln 2 / 2 *) |
| 126 | + if a < 1.0397207708399179 then (* less than 3 ln 2 / 2 *) |
| 127 | + if negative(x) then k := -1; else k := 1; end; |
| 128 | + elsif negative(x) then k := INT(x * 1.44269504088896338700 - 0.5); |
| 129 | + else k := INT(x * 1.44269504088896338700 + 0.5); |
| 130 | + end; |
| 131 | + hi := x - F64(k) * ln2hi; |
| 132 | + lo := F64(k) * ln2lo; |
| 133 | + x := hi - lo; |
| 134 | + c := (hi - x) - lo; |
| 135 | + elsif a < 5.551115123125783E-17 then return x; (* below 2^-54 *) |
| 136 | + end; |
| 137 | + hfx := 0.5 * x; |
| 138 | + hxs := x * hfx; |
| 139 | + r1 := 1.0 + hxs * (-3.33333333333331316428E-02 + hxs * (1.58730158725481460165E-03 |
| 140 | + + hxs * (-7.93650757867487942473E-05 + hxs * (4.00821782732936239552E-06 |
| 141 | + + hxs * (-2.01099218183624371326E-07))))); |
| 142 | + t := 3.0 - r1 * hfx; |
| 143 | + e := hxs * ((r1 - t) / (6.0 - x * t)); |
| 144 | + if k = 0 then return x - (x * e - hxs); end; |
| 145 | + e := (x * (e - c) - c) - hxs; |
| 146 | + if k = -1 then return 0.5 * (x - e) - 0.5; end; |
| 147 | + if k = 1 then |
| 148 | + if x < -0.25 then return -2.0 * (e - (x + 0.5)); end; |
| 149 | + return 1.0 + 2.0 * (x - e); |
| 150 | + end; |
| 151 | + if k <= -2 then return scale(1.0 - (e - x), k) - 1.0; end; |
| 152 | + if k < 20 then y := (1.0 - scale(1.0, -k)) - (e - x); |
| 153 | + else y := (x - (e + scale(1.0, -k))) + 1.0; |
| 154 | + end; |
| 155 | + return scale(y, k); |
| 156 | +end; |
| 157 | + |
| 158 | +(* The hyperbolic tangent of x, from -1 to 1: fdlibm's, from |
| 159 | + expm1(2|x|), which is accurate for a small x where 1 - 2/(e^2x + 1) |
| 160 | + would lose every digit. From 22 on it is 1 to the last bit. *) |
| 161 | +proc tanh*(x: F64): F64; |
| 162 | +var a, t, z: F64; |
| 163 | +begin |
| 164 | + if x # x then return x; end; |
| 165 | + a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); |
| 166 | + if a >= 22.0 then z := 1.0; |
| 167 | + elsif a < 2.7755575615628914E-17 then return x; (* below 2^-55: tanh x = x; keeps -0 *) |
| 168 | + elsif a >= 1.0 then |
| 169 | + t := expm1(2.0 * a); |
| 170 | + z := 1.0 - 2.0 / (t + 2.0); |
| 171 | + else |
| 172 | + t := expm1(-2.0 * a); |
| 173 | + z := -t / (t + 2.0); |
| 174 | + end; |
| 175 | + if negative(x) then return -z; end; |
| 176 | + return z; |
| 177 | +end; |
| 178 | + |
| 101 | 179 | (* ln x = k ln 2 + ln m, with m from sqrt(2)/2 to sqrt(2): ln m is |
| 102 | 180 | found through s = f / (2 + f), f = m - 1, whose series converges |
| 103 | 181 | fast. *) |
| | @@ -999,6 +1077,16 @@ |
| 999 | 1077 | or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi) |
| 1000 | 1078 | or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965) |
| 1001 | 1079 | or (atan(inf) # pi / 2.0) or not negative(atan(-0.0))), "Math.asin, acos, atan"); |
| 1080 | + Check.ok(not (not near(tanh(0.5), 0.46211715726000974) or not near(tanh(1.0), 0.7615941559557649) |
| 1081 | + or not near(tanh(-0.3), -0.2913126124515909) or not near(tanh(3.0), 0.9950547536867305) |
| 1082 | + or not near(tanh(1.0E-10), 1.0E-10) or (tanh(-1.0E-300) # -1.0E-300) or (tanh(20.0) # 1.0) |
| 1083 | + or (tanh(-30.0) # -1.0) or (tanh(inf) # 1.0) or not negative(tanh(-0.0)) or (tanh(nan) = tanh(nan))), |
| 1084 | + "Math.tanh"); |
| 1085 | + 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) |
| 1086 | + or (roundEven(-2.5) # -2.0) or (roundEven(-3.5) # -4.0) or (roundEven(2.6) # 3.0) or (roundEven(-2.4) # -2.0) |
| 1087 | + or not negative(roundEven(-0.5)) or negative(roundEven(0.5)) or (roundEven(0.49999999999999994) # 0.0) |
| 1088 | + or (roundEven(4503599627370495.5) # 4503599627370496.0) or (roundEven(4503599627370497.0) # 4503599627370497.0) |
| 1089 | + or (roundEven(-1.0E300) # -1.0E300) or (roundEven(inf) # inf) or (roundEven(nan) = roundEven(nan))), "Math.roundEven"); |
| 1002 | 1090 | testToReal; |
| 1003 | 1091 | end; |
| 1004 | 1092 | |