plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


322d1fa — Peter John 2026-11-10T22:01:09+05:30
math: exp and ln are fdlibm's, under 1 ulp
Files changed (3) hide show
  1. docs/std.md +1 -1
  2. std/math/math.plum +83 -97
  3. tests/core/float_bits.plum +40 -20
docs/std.md CHANGED
@@ -39,7 +39,7 @@ fun main() =
39
39
  - `Float`: `abs`, `sqrt`, `pow`, `floor`, `ceil`, `round`, `trunc`, `isNaN`, `compare`, `toStr`, `Float.parse`, `Float.Pi`, `Float.Tau`, `Float.E`, and `toBits` / `Float.fromBits` between a Float and its 64 IEEE 754 bits as an Int
40
40
  - `math`: `math.sqrt`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `exp`, `ln`, `hypot`, `cbrt`
41
41
 
42
- Where wasm has the operation, std uses its instruction, compiled in line at each call: `math.sqrt` and `Float.sqrt` are `f64.sqrt`, correctly rounded; `abs`, `floor`, `ceil` and `trunc` are `f64.abs`, `f64.floor`, `f64.ceil` and `f64.trunc`; `leadingZeros`, `trailingZeros` and `popCount` are `i64.clz`, `i64.ctz` and `i64.popcnt`; `x.toBits()` and `Float.fromBits(b)` are `i64.reinterpret_f64` and `f64.reinterpret_i64`, so a NaN keeps its payload and `{-0.0}.toBits()` is `Int.Min`. These are exact for every Float: NaN and the infinities pass through, a value past `Int`'s range comes back whole as it was, and a zero keeps its sign (`{-0.5}.floor()` is `-1.0`, `{-0.5}.ceil()` is `-0.0`, `{-0.0}.abs()` is `0.0`). `round` is half away from zero and exact too (`0.49999999999999994.round()` is `0.0`). `math.hypot(a, b)` is `sqrt(a * a + b * b)` unless that sum overflows or underflows, then it scales: `hypot(3e200, 4e200)` is about `5e200`, and an infinite side gives Infinity even next to a NaN. `min` and `max` stay comparisons: `x.min(y)` is `if x < y then x else y`, so with a NaN it returns `y`. `sin`, `cos`, `exp`, `ln` and the rest have no wasm instruction and are Plum code made of exact IEEE operations, so they give the same bits on every host. Their series and polynomials are written out term by term (no loop), and `ln` reads the power of two of its argument from the exponent bits instead of halving or doubling it, as `exp` does for the scaling into [-0.5, 0.5]; `benchmarks/math_bits/check.plum` checks that these give the same bits as the loop versions did.
42
+ Where wasm has the operation, std uses its instruction, compiled in line at each call: `math.sqrt` and `Float.sqrt` are `f64.sqrt`, correctly rounded; `abs`, `floor`, `ceil` and `trunc` are `f64.abs`, `f64.floor`, `f64.ceil` and `f64.trunc`; `leadingZeros`, `trailingZeros` and `popCount` are `i64.clz`, `i64.ctz` and `i64.popcnt`; `x.toBits()` and `Float.fromBits(b)` are `i64.reinterpret_f64` and `f64.reinterpret_i64`, so a NaN keeps its payload and `{-0.0}.toBits()` is `Int.Min`. These are exact for every Float: NaN and the infinities pass through, a value past `Int`'s range comes back whole as it was, and a zero keeps its sign (`{-0.5}.floor()` is `-1.0`, `{-0.5}.ceil()` is `-0.0`, `{-0.0}.abs()` is `0.0`). `round` is half away from zero and exact too (`0.49999999999999994.round()` is `0.0`). `math.hypot(a, b)` is `sqrt(a * a + b * b)` unless that sum overflows or underflows, then it scales: `hypot(3e200, 4e200)` is about `5e200`, and an infinite side gives Infinity even next to a NaN. `min` and `max` stay comparisons: `x.min(y)` is `if x < y then x else y`, so with a NaN it returns `y`. `sin`, `cos`, `exp`, `ln` and the rest have no wasm instruction and are Plum code made of exact IEEE operations, so they give the same bits on every host. `math.exp` and `math.ln` are fdlibm's (FreeBSD's `e_exp.c` and `e_log.c`), under 1 ulp (0.81 and 0.73 at worst against mpmath over 22k sampled inputs, `exp` over its whole range): `exp` reduces `x` to `k * ln2 + r` and is finite up to 709.78 and subnormal down to -745.13 (`exp(1.0)` is `2.7182818284590455`, fdlibm's result, one ulp above `Float.E`), and `ln` splits `x` into `2^k * (1 + f)` with `1 + f` in [sqrt(2)/2, sqrt(2)), so `ln(0.99999999999)` keeps its digits (the old Taylor versions were up to 8700 ulp off for `exp` and 7e10 ulp for `ln` next to 1). `x.pow(y)` for a `y` that isn't whole is `exp(y * ln(x))`, so the rounding of `y * ln(x)` grows with it: `10.0.pow(0.5)` is 1.4 ulp off, `10.0.pow(10.5)` 23 ulp and `10.0.pow(299.5)` 323 ulp (fdlibm's `e_pow.c`, which carries `ln(x)` in extra precision, is not ported yet).
43
43
 
44
44
  At the edges they follow IEEE 754. `x.logb()` is the exponent of `|x|`, floor(log2(|x|)) exactly, read from the bits (a subnormal is scaled by 2^54 first): `7.999999999999999.logb()` is `2.0`, `Float.Min.logb()` is `-1074.0`, `logb` of a zero is -Infinity, of either infinity Infinity, of NaN NaN, and the sign is ignored (`Int.logb` agrees: `Int.Min.logb()` is `63.0`). `math.cbrt` is correctly rounded (no miss in a million sampled inputs over every exponent) for every Float, subnormals and `1e300` included (`cbrt(1e300)` is `1e100`): `|x|` is split into `m * 2^(3k)` from the bits, FreeBSD's `s_cbrt.c` guess gives a 23-bit root `t` of `m`, and `t * (1 + u)^(1/3)` with `u = (m - t^3) / t^3` (the residual exact but for one rounding, by Dekker's product; the binomial series to u^4) rounds to the nearest float unless the root is within about 2^-22 ulp of halfway. It keeps the sign (`cbrt(-0.0)` is `-0.0`), and NaN and the infinities pass through. `math.sin`, `math.cos` and `math.tan` are fdlibm's (FreeBSD's `s_sin.c`, `s_cos.c`, `s_tan.c`), under 1 ulp for every finite Float: against mpmath over 89k sampled inputs (every exponent, multiples of Pi/2 and the floats next to them, subnormals) at worst 0.72, 0.74 and 0.81 ulp, with 1.4%, 1.5% and 1.7% not the nearest float (the old Taylor series after a plain subtraction of multiples of 2*Pi was up to 10^22 ulp off next to the zeros, and for |x| <= 4 36%, 68% and 68% not the nearest). `x` is reduced to `n * Pi/2 + r` with `|r| <= Pi/4`: below 2^20 * Pi/2 (about 1.6e6) by Cody and Waite's subtraction with Pi/2 in three 33-bit parts, above it by multiplying the 53-bit significand by 240 bits of 2/Pi in integers (Payne and Hanek), so `sin(1e22)` is `-0.8522008497671888` and `sin(Float.Max)` is right too; then fdlibm's kernel polynomials for sin, cos and tan on `r`. A tiny `x` (below 2^-26 for `sin`, 2^-27 for `tan`) is its own `sin` and `tan`, a subnormal included, and its `cos` is `1.0`; the infinities and NaN give NaN. `math.atan` is fdlibm's `s_atan.c` bit for bit, under 1 ulp (0.70 at worst over 40k sampled inputs against mpmath): breakpoints at 7/16, 11/16, 19/16 and 39/16, then a degree-22 odd polynomial; from 2^66 up, and at the infinities, it is `±Pi/2`. `math.atan2` is fdlibm's `e_atan2.c` (1.36 ulp at worst) with C99's special cases: `atan2(±0, x)` is `±0` for `x` above zero or `+0` and `±Pi` for `x` below zero or `-0`, `atan2(y, ±0)` for a nonzero `y` is `±Pi/2`, an infinite `x` gives `±0` or `±Pi` for a finite `y` and `±Pi/4` or `±3*Pi/4` for an infinite one, an infinite `y` gives `±Pi/2`, and a NaN on either side gives NaN. `math.asin` is `atan(x / sqrt(1 - x*x))` with `(1 - x) * (1 + x)` from `|x| = 0.5` up, and `math.acos` is `2 * atan(sqrt((1 - x) / (1 + x)))`, both within 1.8 ulp (`acos` was `Pi/2 - asin(x)`, 231 ulp off near 1). `sin(-0.0)`, `tan(-0.0)`, `asin(-0.0)` and `atan(-0.0)` are `-0.0`.
45
45
 
std/math/math.plum CHANGED
@@ -32,66 +32,59 @@ fun powLoop(result: Float, b: Float, m: Int) -> Float =
32
32
  result
33
33
 
34
34
 
35
- # Natural exponential, e^x, via range reduction (x / 2^k with the least k that puts it in
35
+ # Natural exponential, e^x: fdlibm's `e_exp.c` (FreeBSD's, under 1 ulp). `x` is reduced to
36
- # [-0.5, 0.5], an exact scaling read from x's exponent bits), the Taylor series there, where
36
+ # `k * ln2 + r` with `|r| <= ln2 / 2` (ln2 in a high and a low part, so `r` keeps its digits),
37
+ # e^r comes from a degree-5 rational approximation, and `k` goes into the exponent bits. It is
38
+ # finite up to 709.78 (`exp(709.0)` is about 8.2e307) and a subnormal down to -745.13; past
37
- # it converges fast, then the result squared back up k times.
39
+ # those it is Infinity and `0.0`. NaN gives NaN, `exp(-Infinity)` is `0.0`.
38
40
  fun exp(x: Float) -> Float =
39
- if x != x
41
+ bits := x.toBits()
42
+ hx := {bits >> 32} & 0x7FFF_FFFF
40
- x
43
+ neg := bits < 0
44
+ if hx >= 0x4086_2E42 && {x != x || x > 7.09782712893383973096e+02 || x < -7.45133219101941108420e+02}
45
+ if x != x then x else if x > 0.0f then 1.0f / 0.0f else 0.0f
41
- else if x > 700.0f
46
+ else if hx > 0x3FD6_2E42
42
- 1.0f / 0.0f
47
+ # |x| > ln2 / 2: k is x / ln2 rounded, 1 or -1 below 1.5 * ln2
43
- else if x < -700.0f
48
+ k := if hx < 0x3FF0_A2B2
44
- 0.0f
49
+ if neg then -1 else 1
45
- else
50
+ else
46
- expReduce(x)
47
-
48
- # `|v| <= 700`. Halving a v above 0.5 is exact, so v / 2^k is v times the power of two
49
- # 2^-k: k is e + 1 for |v| = 2^e, else e + 2 for 2^e < |v| < 2^(e+1).
51
+ Int(1.44269504088896338700e+00 * x + {if neg then -0.5f else 0.5f})
50
- fun expReduce(v: Float) -> Float =
51
- if v > 0.5f || v < -0.5f
52
+ hi := x - Float(k) * 6.93147180369123816490e-01
52
- b := v.toBits() & 0x7FFF_FFFF_FFFF_FFFF
53
+ lo := Float(k) * 1.90821492927058770002e-10
54
+ expScaled(hi, lo, k)
53
- e := {b >> 52} - 1023
55
+ else if hx < 0x3E30_0000
54
- n := if {b & 0x000F_FFFF_FFFF_FFFF} == 0 then e + 1 else e + 2
56
+ # |x| < 2^-28: e^x rounds to 1 + x
55
- squareTimes(expSeries(v * Float.fromBits({1023 - n} << 52)), n)
57
+ 1.0f + x
56
58
  else
57
- expSeries(v)
59
+ expScaled(x, 0.0f, 0)
58
-
60
+
59
- # The Taylor series of e^v to the v^24 term, in order: term n is term n-1 times v over n.
61
+ # e^(hi - lo) * 2^k, for |hi - lo| <= ln2 / 2 (fdlibm's tail of `exp`).
60
- fun expSeries(v: Float) -> Float =
61
- t1 := v
62
- t2 := t1 * v / 2.0f
63
- t3 := t2 * v / 3.0f
64
- t4 := t3 * v / 4.0f
65
- t5 := t4 * v / 5.0f
66
- t6 := t5 * v / 6.0f
67
- t7 := t6 * v / 7.0f
68
- t8 := t7 * v / 8.0f
69
- t9 := t8 * v / 9.0f
70
- t10 := t9 * v / 10.0f
71
- t11 := t10 * v / 11.0f
72
- t12 := t11 * v / 12.0f
73
- t13 := t12 * v / 13.0f
74
- t14 := t13 * v / 14.0f
75
- t15 := t14 * v / 15.0f
76
- t16 := t15 * v / 16.0f
77
- t17 := t16 * v / 17.0f
78
- t18 := t17 * v / 18.0f
79
- t19 := t18 * v / 19.0f
80
- t20 := t19 * v / 20.0f
81
- t21 := t20 * v / 21.0f
82
- t22 := t21 * v / 22.0f
83
- t23 := t22 * v / 23.0f
84
- t24 := t23 * v / 24.0f
85
- 1.0f + t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t15 + t16 + t17 + t18 + t19 + t20 + t21 + t22 + t23 + t24
86
-
87
- fun squareTimes(result: Float, k: Int) -> Float =
62
+ fun expScaled(hi: Float, lo: Float, k: Int) -> Float =
63
+ r := hi - lo
64
+ c := expSeries(r)
88
- if k > 0
65
+ if k == 0
89
- squareTimes(result * result, k - 1)
66
+ 1.0f - {{r * c} / {c - 2.0f} - r}
90
67
  else
68
+ y := 1.0f - {{lo - {r * c} / {2.0f - c}} - hi}
69
+ if k == 1024
70
+ # 2^1024 is past Float's range: scale by 2 and 2^1023
71
+ y * 2.0f * 8.98846567431157953865e+307
72
+ else if k >= -1021
73
+ Float.fromBits(y.toBits() + {k << 52})
91
- result
74
+ else
92
-
75
+ # the result is subnormal: scale into range, then round once by 2^-1000
76
+ Float.fromBits(y.toBits() + {{k + 1000} << 52}) * 9.33263618503218878990e-302
77
+
78
+ # fdlibm's `r - r^2 * P(r^2)`, P of degree 4 (Remez): e^r is `1 + r + r * c / (2 - c)` for it.
79
+ fun expSeries(r: Float) -> Float =
80
+ t := r * r
81
+ r - t * {1.66666666666666019037e-01 + t * {-2.77777777770155933842e-03 + t * {6.61375632143793436117e-05 + t * {-1.65339022054652515390e-06 + t * 4.13813679705723846039e-08}}}}
82
+
93
- # Natural logarithm, via range reduction to x = v * 2^k with v in [1, 2) (k and v read from
83
+ # Natural logarithm: fdlibm's `e_log.c` (FreeBSD's, under 1 ulp). `x` is split from its bits
84
+ # into `2^k * (1 + f)` with `1 + f` in [sqrt(2)/2, sqrt(2)) (so `ln(0.999999)` keeps its
94
- # x's bits) plus the fast-converging series ln(v) = 2*atanh((v-1)/(v+1)).
85
+ # digits), and `ln(1 + f)` is `f - f^2/2 + s * (f^2/2 + R(s^2))` with `s = f / (2 + f)`.
86
+ # `ln(1.0)` is `0.0`, `ln(±0.0)` is -Infinity, a negative `x` or NaN gives NaN, and
87
+ # `ln(Infinity)` is Infinity.
95
88
  fun ln(x: Float) -> Float =
96
89
  if x != x || x < 0.0f
97
90
  0.0f / 0.0f
@@ -107,46 +100,39 @@ fun ln(x: Float) -> Float =
107
100
  else
108
101
  lnBits(b, 0)
109
102
 
110
- # ln(f) + k * ln 2, f the positive normal float with bits `b`: f is v * 2^(e - 1023), e its
111
- # exponent field and v in [1, 2) its fraction under exponent field 1023.
112
- fun lnBits(b: Int, k: Int) -> Float =
113
- v := Float.fromBits({b & 0x000F_FFFF_FFFF_FFFF} | 0x3FF0_0000_0000_0000)
114
- t := {v - 1.0f} / {v + 1.0f}
115
- t2 := t * t
116
- Float(k + {b >> 52} - 1023) * Float.Ln2 + 2.0f * lnSeries(t2, t)
117
-
118
- # t + t^3/3 + t^5/5 + ... + t^59/59, in order: each power is the one before times t2.
119
- fun lnSeries(t2: Float, t: Float) -> Float =
120
- p3 := t * t2
121
- p5 := p3 * t2
122
- p7 := p5 * t2
123
- p9 := p7 * t2
124
- p11 := p9 * t2
125
- p13 := p11 * t2
126
- p15 := p13 * t2
127
- p17 := p15 * t2
128
- p19 := p17 * t2
129
- p21 := p19 * t2
130
- p23 := p21 * t2
131
- p25 := p23 * t2
132
- p27 := p25 * t2
133
- p29 := p27 * t2
134
- p31 := p29 * t2
135
- p33 := p31 * t2
136
- p35 := p33 * t2
137
- p37 := p35 * t2
138
- p39 := p37 * t2
139
- p41 := p39 * t2
140
- p43 := p41 * t2
141
- p45 := p43 * t2
142
- p47 := p45 * t2
143
- p49 := p47 * t2
144
- p51 := p49 * t2
145
- p53 := p51 * t2
146
- p55 := p53 * t2
147
- p57 := p55 * t2
148
- p59 := p57 * t2
149
- t + p3 / 3.0f + p5 / 5.0f + p7 / 7.0f + p9 / 9.0f + p11 / 11.0f + p13 / 13.0f + p15 / 15.0f + p17 / 17.0f + p19 / 19.0f + p21 / 21.0f + p23 / 23.0f + p25 / 25.0f + p27 / 27.0f + p29 / 29.0f + p31 / 31.0f + p33 / 33.0f + p35 / 35.0f + p37 / 37.0f + p39 / 39.0f + p41 / 41.0f + p43 / 43.0f + p45 / 45.0f + p47 / 47.0f + p49 / 49.0f + p51 / 51.0f + p53 / 53.0f + p55 / 55.0f + p57 / 57.0f + p59 / 59.0f
103
+ # ln of the positive normal float with bits `b`, plus `k0 * ln2`.
104
+ fun lnBits(b: Int, k0: Int) -> Float =
105
+ hx0 := b >> 32
106
+ hm := hx0 & 0x000F_FFFF
107
+ # i is 2^20 when the fraction is at least sqrt(2) - 1: then the float is halved
108
+ i := {hm + 0x9_5F64} & 0x10_0000
109
+ k := k0 + {hx0 >> 20} - 1023 + {i >> 20}
110
+ v := Float.fromBits({{hm | {i ^ 0x3FF0_0000}} << 32} | {b & 0xFFFF_FFFF})
111
+ f := v - 1.0f
112
+ dk := Float(k)
113
+ if {0x000F_FFFF & {2 + hm}} < 3
114
+ # |f| < 2^-20
115
+ if f == 0.0f
116
+ if k == 0 then 0.0f else dk * 6.93147180369123816490e-01 + dk * 1.90821492927058770002e-10
117
+ else
118
+ r := f * f * {0.5f - 0.33333333333333333f * f}
119
+ if k == 0 then f - r else dk * 6.93147180369123816490e-01 - {{r - dk * 1.90821492927058770002e-10} - f}
120
+ else
121
+ s := f / {2.0f + f}
122
+ r := lnSeries(s * s)
123
+ if {{hm - 0x6_147A} | {0x6_B851 - hm}} > 0
124
+ hfsq := 0.5f * f * f
125
+ if k == 0 then f - {hfsq - s * {hfsq + r}} else dk * 6.93147180369123816490e-01 - {{hfsq - {s * {hfsq + r} + dk * 1.90821492927058770002e-10}} - f}
126
+ else
127
+ if k == 0 then f - s * {f - r} else dk * 6.93147180369123816490e-01 - {{s * {f - r} - dk * 1.90821492927058770002e-10} - f}
128
+
129
+ # fdlibm's `R(z)` for `z = s^2`, `ln(1 + f) = 2s + s * R(z)`: the polynomial of degree 7 in
130
+ # z (Remez), split into the even and odd powers of `w = z^2` as fdlibm has it.
131
+ fun lnSeries(z: Float) -> Float =
132
+ w := z * z
133
+ t1 := w * {3.999999999940941908e-01 + w * {2.222219843214978396e-01 + w * 1.531383769920937332e-01}}
134
+ t2 := z * {6.666666666666735130e-01 + w * {2.857142874366239149e-01 + w * {1.818357216161805012e-01 + w * 1.479819860511658591e-01}}}
135
+ t2 + t1
150
136
 
151
137
  # Square root: wasm's `f64.sqrt`, correctly rounded (IEEE 754). `sqrt(-0.0)` is `-0.0`, a
152
138
  # negative `x` or NaN gives NaN, `sqrt(Infinity)` is Infinity.
tests/core/float_bits.plum CHANGED
@@ -1,8 +1,6 @@
1
1
  # `Float.toBits` / `Float.fromBits` (wasm's `i64.reinterpret_f64` / `f64.reinterpret_i64`),
2
- # std math's unrolled series (exp, ln) and bit-read range reduction (exp, ln): the expected bits
3
- # are the ones the loop versions gave (see benchmarks/math_bits/check.plum, which compares the
4
- # two over millions of inputs); and fdlibm's sin, cos, tan and atan, whose bits were checked
2
+ # and std math's fdlibm ports (exp, ln, sin, cos, tan, atan), whose bits were checked against
5
- # against mpmath.
3
+ # mpmath.
6
4
  import std/Float
7
5
  import std/Int
8
6
  import std/math/math
@@ -89,33 +87,54 @@ test "sin, cos and tan keep their bits"
89
87
  assert math.sin(Inf).isNaN()
90
88
  assert math.cos(-Inf).isNaN()
91
89
 
92
- test "exp keeps its bits, at the reduction boundaries too"
90
+ test "exp is fdlibm's: the nearest Float but for under 1 ulp, finite up to 709.78, subnormal down to -745.13"
93
91
  assert math.exp(0.0f) == 1.0f
94
92
  assert math.exp(-0.0f) == 1.0f
95
93
  assert math.exp(Float.Min) == 1.0f
94
+ assert math.exp(-1e-300) == 1.0f
95
+ # 2.7182818284590455, one ulp above the nearest Float (fdlibm's own result, 0.67 ulp off)
96
- assert math.exp(1.0f).toBits() == 4613303445314885478
96
+ assert math.exp(1.0f).toBits() == 4613303445314885482
97
- assert math.exp(0.5f).toBits() == 4610103999673009818
97
+ assert math.exp(0.5f).toBits() == 4610103999673009820
98
- assert math.exp(-0.5f).toBits() == 4603638362051417609
98
+ assert math.exp(-0.5f).toBits() == 4603638362051417610
99
- assert math.exp(0.75f).toBits() == 4611949479042997497
99
+ assert math.exp(0.75f).toBits() == 4611949479042997496
100
- assert math.exp(2.0f).toBits() == 4620005356000828837
100
+ assert math.exp(2.0f).toBits() == 4620005356000828846
101
- assert math.exp(-3.3f).toBits() == 4585476245642657404
101
+ assert math.exp(-3.3f).toBits() == 4585476245642657413
102
+ assert math.exp(1e-9).toBits() == 4607182418804521008
103
+ assert math.exp(0.34657359027997264f) == 1.414213562373095
102
- assert math.exp(700.0f).toBits() == 9155136748776911380
104
+ assert math.exp(700.0f).toBits() == 9155136748776909966
103
- assert math.exp(-700.0f).toBits() == 58915316498510369
105
+ assert math.exp(-700.0f).toBits() == 58915316498509951
106
+ # past 700 (the old cut-off) it is still finite, and below -708 subnormal
107
+ assert math.exp(701.0f).toBits() == 9161476081910164278
108
+ assert math.exp(709.0f) == 8.218407461554972e+307
109
+ assert math.exp(709.782712893384f) == 1.7976931348622732e+308
104
- assert math.exp(701.0f) == Inf
110
+ assert math.exp(709.79f) == Inf
111
+ assert math.exp(-701.0f).toBits() == 52205408832852198
112
+ assert math.exp(-708.0f) == 3.307553003638408e-308
113
+ assert math.exp(-740.0f) == 4.2e-322
105
- assert posZero(math.exp(-701.0f))
114
+ assert math.exp(-745.1f) == 5e-324
115
+ assert posZero(math.exp(-745.2f)) && posZero(math.exp(-Inf))
116
+ assert math.exp(Inf) == Inf
106
117
  assert math.exp(NaN).isNaN()
107
118
 
108
- test "ln keeps its bits, for subnormals too"
119
+ test "ln is fdlibm's: the nearest Float here, next to 1 and for subnormals too"
109
120
  assert posZero(math.ln(1.0f))
110
121
  assert math.ln(2.0f).toBits() == 4604418534313441775
111
122
  assert math.ln(Float.E) == 1.0f
112
123
  assert math.ln(0.1f).toBits() == -4611004657371359979
124
+ assert math.ln(1.5f) == 0.4054651081081644
125
+ assert math.ln(0.7071067811865476f) == -0.3465735902799726
113
126
  assert math.ln(1.0e300).toBits() == 4649287341619838901
114
127
  assert math.ln(Float.Max).toBits() == 4649454530587146735
115
128
  assert math.ln(Float.Min).toBits() == -4573612656913714749
116
- assert math.ln(Float.fromBits(0x000F_FFFF_FFFF_FFFF)).toBits() == -4573929700241785645
129
+ assert math.ln(Float.fromBits(0x000F_FFFF_FFFF_FFFF)).toBits() == -4573929700241785646
117
130
  assert math.ln(Float.fromBits(0x0010_0000_0000_0000)).toBits() == -4573929700241785646
118
- assert math.ln(1.9999999999999998f).toBits() == 4604418534313441772
131
+ assert math.ln(1.9999999999999998f).toBits() == 4604418534313441774
132
+ # next to 1 every digit counts: the old ln(0.99999999999) was 7e10 ulp off
133
+ assert math.ln(0.9999999f) == -1.0000000494736474e-07
134
+ assert math.ln(1.0000001f) == 9.999999505838704e-08
135
+ assert math.ln(0.99999999999f) == -1.000000082745371e-11
136
+ assert math.ln(1.0000000000000002f) == 2.2204460492503128e-16
137
+ assert math.ln(0.9999999999999999f) == -1.1102230246251565e-16
119
138
  assert math.ln(0.0f) == -Inf
120
139
  assert math.ln(-0.0f) == -Inf
121
140
  assert math.ln(Inf) == Inf
@@ -128,5 +147,6 @@ test "atan and pow keep their bits"
128
147
  assert math.atan(1.0f).toBits() == 4605249457297304856
129
148
  assert math.atan(-7.0f).toBits() == -4614258027452340739
130
149
  assert math.atan(NaN).isNaN()
150
+ # pow is exp(y * ln(x)): within an ulp or two (the nearest are ...685 and ...866)
131
- assert 2.0.pow(0.5f).toBits() == 4609047870845172686
151
+ assert 2.0.pow(0.5f).toBits() == 4609047870845172684
132
- assert 10.0.pow(-1.5f).toBits() == 4584718143433226873
152
+ assert 10.0.pow(-1.5f).toBits() == 4584718143433226865