raz / commit 31c6ebe1

commit
31c6ebe1935ae9200eb7824093d74bb3a950830d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
7849597f
Math: tanh, over fdlibm's expm1, and roundEven, a half to the even one

patch · log

2 files changed, +94 -3

doc/raz-lib.md (history) modified +4 -1

@@ -746,6 +746,7 @@
746746 proc floor*(x: F64): F64;
747747 proc ceil*(x: F64): F64;
748748 proc round*(x: F64): F64; (* to the nearest whole number; a half away from 0 *)
749+proc roundEven*(x: F64): F64; (* to the nearest whole number; a half to the even one *)
749750 proc exp*(x: F64): F64;
750751 proc ln*(x: F64): F64;
751752 proc log2*(x: F64): F64;
@@ -753,6 +754,7 @@
753754 proc sin*(x: F64): F64;
754755 proc cos*(x: F64): F64;
755756 proc tan*(x: F64): F64;
757+proc tanh*(x: F64): F64; (* from -1 to 1 *)
756758 proc asin*(x: F64): F64; (* from -pi/2 to pi/2 *)
757759 proc acos*(x: F64): F64; (* from 0 to pi *)
758760 proc atan*(x: F64): F64; (* from -pi/2 to pi/2 *)
@@ -882,7 +884,8 @@
882884 `Sort.sort` puts `s` in the order `less` gives, in place, without
883885 memory of its own, in time `n log n` at most; two elements equal by
884886 `less` may change places. `Math`'s procedures are as C's `math.h`
885-has them, to within the last bit or so. `Fmt.real(x, places)` writes
887+has them, to within the last bit or so; `roundEven` is IEEE 754's
888+default rounding, as Pascal's `Round`, where `round` is C's. `Fmt.real(x, places)` writes
886889 `x` with `places` digits after the point, at most 9, rounded to the
887890 nearest; from 1E15 on, with an exponent, as `1.50E17`; and `nan`,
888891 `inf` and `-inf` as themselves. `hex` writes capitals and `lowHex`

lib/Math.raz (history) modified +90 -2

@@ -5,8 +5,7 @@
55 polynomials' coefficients are fdlibm's. Each is within an ulp, a
66 unit in the result's last place.
77
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. *)
109 module Math;
1110 import Check;
1211
@@ -69,6 +68,21 @@
6968 return t;
7069 end;
7170
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+
7286 (* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits,
7387 in two steps where k is past what an F64's exponent holds. *)
7488 proc scale(x: F64; k: INT): F64;
@@ -98,6 +112,70 @@
98112 return scale(1.0 + (r * c / (2.0 - c) - lo + hi), k);
99113 end;
100114
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+
101179 (* ln x = k ln 2 + ln m, with m from sqrt(2)/2 to sqrt(2): ln m is
102180 found through s = f / (2 + f), f = m - 1, whose series converges
103181 fast. *)
@@ -999,6 +1077,16 @@
9991077 or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi)
10001078 or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965)
10011079 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");
10021090 testToReal;
10031091 end;
10041092