plum

#treesitter#compiler#wasm

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

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


78ea999 — Peter John 2026-11-10T09:57:26+05:30
std math edges: logb and cbrt from the bits, atan past 2^60, Int.Min.toStr, big Float.toStr, Float.hash on the bits
benchmarks/math_bits/check.plum CHANGED
@@ -1,7 +1,11 @@
1
1
  # Bit-identity check for std math's unrolled series and bit-based range reduction: runs the
2
2
  # loop versions std had before (kept here only, as `old*`) and std's current ones over the
3
3
  # same deterministic inputs and counts results whose 64 bits differ (NaN payloads and the
4
+ # sign of zero included). The edge fixes made since (`sin` and `atan` keep a zero's sign,
5
+ # `atan` past 2^60 is `Pi/2 - atan(1/|x|)`) are copied into the old versions too, so only the
6
+ # unrolling is compared. `logb` now reads the exponent from the bits; it is checked against an
7
+ # exact halve-or-double reference instead (the old `floor(ln(|x|) * Log2E)` was wrong for
4
- # sign of zero included).
8
+ # about 40% of the floats just below a power of two).
5
9
  #
6
10
  # target/release/plum run benchmarks/math_bits/check.plum (also with --no-fbip, --no-fusion)
7
11
  #
@@ -92,6 +96,8 @@ fun oldReduceToPi(x: Float) -> Float =
92
96
  fun oldSin(x: Float) -> Float =
93
97
  if x != x || x > Float.Max || x < -Float.Max
94
98
  0.0f / 0.0f
99
+ else if x == 0.0f
100
+ x
95
101
  else
96
102
  r := oldReduceToPi(x)
97
103
  oldSinSeries(r * r, r, r, 1)
@@ -113,12 +119,12 @@ fun oldTan(x: Float) -> Float =
113
119
  oldSin(x) / oldCos(x)
114
120
 
115
121
  fun oldAtan(x: Float) -> Float =
116
- if x != x
122
+ if x != x || x == 0.0f
117
123
  x
118
124
  else
119
125
  neg := x < 0.0f
120
126
  v := if neg then -x else x
121
- result := oldAtanReduce(v, 0)
127
+ result := if v > 1152921504606846976.0f then Float.HalfPi - oldAtanReduce(1.0f / v, 0) else oldAtanReduce(v, 0)
122
128
  if neg then -result else result
123
129
 
124
130
  fun oldAtanReduce(v: Float, k: Int) -> Float =
@@ -151,11 +157,25 @@ fun oldPow(base: Float, y: Float) -> Float =
151
157
  else
152
158
  oldExp(y * oldLn(base))
153
159
 
160
+ # An exact logb by repeated halving or doubling into [1, 2) (every step exact): std's
161
+ # `logb` reads the exponent from the bits and must agree everywhere.
154
- fun oldLogb(x: Float) -> Float =
162
+ fun refLogb(x: Float) -> Float =
163
+ if x != x
164
+ x
155
- if x == 0.0f
165
+ else if x == 0.0f
156
166
  -1.0f / 0.0f
167
+ else if x > Float.Max || x < -Float.Max
168
+ 1.0f / 0.0f
169
+ else
170
+ Float(refLogbFrom(if x < 0.0f then -x else x, 0))
171
+
172
+ fun refLogbFrom(v: Float, k: Int) -> Int =
173
+ if v >= 2.0f
174
+ refLogbFrom(v / 2.0f, k + 1)
175
+ else if v < 1.0f
176
+ refLogbFrom(v * 2.0f, k - 1)
157
177
  else
158
- {oldLn(if x < 0.0f then -x else x) * Float.Log2E}.floor()
178
+ k
159
179
 
160
180
  # --- inputs ---
161
181
 
@@ -308,6 +328,6 @@ fun main(io: Io) =
308
328
  b6 := report(io, "ln", n, sweep1(io, |x| oldLn(x), |x| math.ln(x), 0, n, 0))
309
329
  b7 := report(io, "ln near every 2^e", 101 * 2047, sweepGen(io, |i| lnEdge(i), |x| oldLn(x), |x| math.ln(x), 0, 101 * 2047, 0))
310
330
  b8 := report(io, "atan", n, sweep1(io, |x| oldAtan(x), |x| math.atan(x), 0, n, 0))
311
- b9 := report(io, "logb", n, sweep1(io, |x| oldLogb(x), |x| x.logb(), 0, n, 0))
331
+ b9 := report(io, "logb (against an exact reference)", n, sweep1(io, |x| refLogb(x), |x| x.logb(), 0, n, 0))
312
332
  b10 := report(io, "pow", n, sweep2(io, |x, y| oldPow(x, y), |x, y| x.pow(y), 0, n, 0))
313
333
  io.printLn("mismatches {b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9 + b10}")
docs/std.md CHANGED
@@ -39,6 +39,10 @@ fun main() =
39
39
  - `math`: `math.sqrt`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `exp`, `ln`, `hypot`, `cbrt`
40
40
 
41
41
  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 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
+
43
+ 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` starts from a guess read from the bits (the exponent divided by 3) and is faithfully rounded, under 0.667 ulp, for every Float, subnormals and `1e300` included (`cbrt(1e300)` is `1e100`); it keeps the sign (`cbrt(-0.0)` is `-0.0`), and NaN and the infinities pass through. `math.atan` past 2^60 is `Pi/2 - atan(1/|x|)` with the sign of `x`, so `atan(1e300)` and `atan(Infinity)` are `Pi/2`. `sin(-0.0)`, `tan(-0.0)` and `atan(-0.0)` are `-0.0`. `math.atan2` gives NaN for a NaN on either side, and `±Pi/4` or `±3*Pi/4` when both sides are infinite.
44
+
45
+ `Int.toStr` prints every Int, `Int.Min` as `-9223372036854775808`. `Float.toStr` rounds to 6 fractional digits below 2^63; from 2^63 up (past Int's range, where every Float is whole) it prints the float's exact decimal value, so `1.0e20.toStr()` is `100000000000000000000.0` and `Float.Max` prints all 309 digits. `Float.hash` is the bits (`toBits`), so values in (-1, 1) no longer collide in a `Map`; `0.0` and `-0.0`, which are `==`, both hash to 0, and every NaN hashes the same.
42
46
  - `Bool`: `toStr`, `Bool.parse`
43
47
 
44
48
  ## Text
examples/numbers.plum CHANGED
@@ -102,9 +102,10 @@ test "sign returns -1/0/1 (or the matching Float) for both Int and Float"
102
102
  assert 0.sign() == 0
103
103
  assert {-2.5}.sign() == -1.0f
104
104
 
105
- test "hash is identity for Int and truncating for Float"
105
+ test "hash is identity for Int and the bits for Float"
106
106
  assert 5.hash() == 5
107
- assert 5.9.hash() == 5
107
+ assert 5.9.hash() == 5.9.toBits()
108
+ assert 0.0.hash() == {-0.0}.hash()
108
109
 
109
110
  test "trunc/floor/ceil/round agree with Int passthrough and real Float math"
110
111
  assert 5.trunc() == 5.0f
std/Float.plum CHANGED
@@ -5,6 +5,7 @@ import std/Int
5
5
  import std/math
6
6
  import std/Option
7
7
  import std/Result
8
+ import std/Array
8
9
 
9
10
  # The API of the built-in `Float` type: associated constants, `Float.parse`
10
11
  # and the numeric methods (`x.abs()`, `x.sqrt()`, ...).
@@ -98,10 +99,17 @@ primitive Float =
98
99
  else
99
100
  self
100
101
 
102
+ # The bits (`toBits`), so `0.1` and `0.2` hash apart; `Map` and `FHamt` avalanche them as
103
+ # they do an Int's. Floats that are `==` hash the same: `0.0` and `-0.0` both hash to 0.
104
+ # Every NaN hashes the same (to the bits of `0.0 / 0.0`, as wasm makes it), though a NaN is
101
- # A `Float` hashes via a truncating conversion (no bit-level float hashing
105
+ # never `==` to anything, so it never finds itself as a key.
102
- # attempted).
103
106
  fun hash(self) -> Int =
104
- Int(self)
107
+ if self == 0.0f
108
+ 0
109
+ else if self != self
110
+ 0x7FF8_0000_0000_0000
111
+ else
112
+ self.toBits()
105
113
 
106
114
  # `trunc`, `floor` and `ceil` are wasm's `f64.trunc`, `f64.floor` and `f64.ceil`: exact
107
115
  # for every Float, a whole number (or NaN, or an infinity) comes back as it was, and the
@@ -134,12 +142,22 @@ primitive Float =
134
142
  fun log10(self) -> Float =
135
143
  math.ln(self) * Float.Log10E
136
144
 
137
- # floor(log2(|self|)).
145
+ # IEEE 754 logb: the exponent of `|self|`, floor(log2(|self|)) exactly, read from the bits
146
+ # (a subnormal is first scaled by 2^54, exact). `logb(±0.0)` is -Infinity,
147
+ # `logb(±Infinity)` is Infinity, NaN gives NaN, the sign is ignored.
138
148
  fun logb(self) -> Float =
139
- if self == 0.0f
149
+ if self != self
150
+ self
151
+ else if self == 0.0f
140
152
  -1.0f / 0.0f
153
+ else if self > Float.Max || self < -Float.Max
154
+ 1.0f / 0.0f
141
155
  else
156
+ e := {self.toBits() >> 52} & 0x7FF
157
+ if e == 0
142
- {math.ln(if self < 0.0f then -self else self) * Float.Log2E}.floor()
158
+ Float({{{self * 18014398509481984.0f}.toBits() >> 52} & 0x7FF} - 1077)
159
+ else
160
+ Float(e - 1023)
143
161
 
144
162
  # wasm's `f64.sqrt`, like `math.sqrt`.
145
163
  fun sqrt(self) -> Float =
@@ -195,12 +213,18 @@ primitive Float =
195
213
  0.5f * math.ln({1.0f + self} / {1.0f - self})
196
214
 
197
215
  # Rounded to 6 fractional digits, with trailing zeros trimmed (but always
198
- # at least one digit after the point, e.g. `3.0` not `3.`).
216
+ # at least one digit after the point, e.g. `3.0` not `3.`). From 2^63 up (past Int's
217
+ # range, and always whole) it is the exact decimal value of the float: `1.0e20` is
218
+ # `100000000000000000000.0`, `1.0e300` the 301 digits of the double nearest 10^300.
199
219
  fun toStr(self) -> Str =
200
220
  if self != self
201
221
  "NaN"
202
222
  else if self > Float.Max || self < -Float.Max
203
223
  if self < 0.0f then "-Infinity" else "Infinity"
224
+ else if self >= 9223372036854775808.0f || self <= -9223372036854775808.0f
225
+ b := self.toBits()
226
+ s := bigWholeToStr({b & 0x000F_FFFF_FFFF_FFFF} | 0x0010_0000_0000_0000, {{b >> 52} & 0x7FF} - 1075) + ".0"
227
+ if self < 0.0f then "-" + s else s
204
228
  else
205
229
  neg := self < 0.0f
206
230
  abs_val := if neg then -self else self
@@ -212,3 +236,39 @@ primitive Float =
212
236
  frac := if carry then 0 else scaled
213
237
  s := Int.intToStr(whole_part) + "." + Int.trimmedFracDigits(frac, 6)
214
238
  if neg then "-" + s else s
239
+
240
+ # The decimal digits of `m * 2^s` (0 < m < 2^53, s >= 0), exactly: base-10^9 limbs, least
241
+ # significant first (Float.Max needs 35), multiplied up by 2^29 at a time.
242
+ fun bigWholeToStr(m: Int, s: Int) -> Str =
243
+ limbs := Array.filled(36, 0)
244
+ limbs.set(0, m % 1000000000)
245
+ limbs.set(1, m / 1000000000)
246
+ used := bigShift(limbs, if m >= 1000000000 then 2 else 1, s)
247
+ Int.intToStr(limbs.get(used - 1)) + bigLimbsToStr(limbs, used - 2)
248
+
249
+ # Multiplies the limbs by 2^s; returns how many are used.
250
+ fun bigShift(limbs: Array[Int], used: Int, s: Int) -> Int =
251
+ if s <= 0
252
+ used
253
+ else
254
+ k := if s > 29 then 29 else s
255
+ bigShift(limbs, bigMul(limbs, 0, used, 1 << k, 0), s - k)
256
+
257
+ # limbs[i..used) times f (at most 2^29) plus `carry`; returns the new used count.
258
+ fun bigMul(limbs: Array[Int], i: Int, used: Int, f: Int, carry: Int) -> Int =
259
+ if i < used
260
+ p := limbs.get(i) * f + carry
261
+ limbs.set(i, p % 1000000000)
262
+ bigMul(limbs, i + 1, used, f, p / 1000000000)
263
+ else if carry > 0
264
+ limbs.set(used, carry)
265
+ used + 1
266
+ else
267
+ used
268
+
269
+ # Limbs i down to 0, nine digits each.
270
+ fun bigLimbsToStr(limbs: Array[Int], i: Int) -> Str =
271
+ if i < 0
272
+ ""
273
+ else
274
+ Int.fixedFracDigits(limbs.get(i), 100000000) + bigLimbsToStr(limbs, i - 1)
std/Int.plum CHANGED
@@ -163,14 +163,15 @@ primitive Int =
163
163
  # Plain decimal rendering of an `Int` — factored out of `Float.toStr` so the
164
164
  # `Float` branch can reuse it (via `intToStr(whole_part)`) without
165
165
  # re-dispatching back through `Float.toStr` itself.
166
+ # A negative `n` peels its last digit off before negating, so `Int.Min` (whose negation
167
+ # overflows back to itself) prints as `-9223372036854775808`.
166
168
  fun intToStr(n: Int) -> Str =
167
169
  if n == 0
168
170
  "0"
171
+ else if n < 0
172
+ "-" + digitsToStr(-{n / 10}) + digitChar(-{n % 10})
169
173
  else
170
- neg := n < 0
171
- v := if neg then -n else n
172
- s := digitsToStr(v)
174
+ digitsToStr(n)
173
- if neg then "-" + s else s
174
175
 
175
176
  # Builds the decimal digits of a positive Int, most significant first, by
176
177
  # peeling off one digit at a time from the least significant end (so the
std/math.plum CHANGED
@@ -163,9 +163,12 @@ fun reduceToPi(x: Float) -> Float =
163
163
  k := {x / Float.Tau + {if x >= 0.0f then 0.5f else -0.5f}}.trunc()
164
164
  x - k * Float.Tau
165
165
 
166
+ # `sin(-0.0)` is `-0.0`, as IEEE 754 has it.
166
167
  fun sin(x: Float) -> Float =
167
168
  if x != x || x > Float.Max || x < -Float.Max
168
169
  0.0f / 0.0f
170
+ else if x == 0.0f
171
+ x
169
172
  else
170
173
  r := reduceToPi(x)
171
174
  sinSeries(r, -{r * r})
@@ -195,14 +198,16 @@ fun tan(x: Float) -> Float =
195
198
 
196
199
  # Arctangent, via repeated argument-halving (`atan(x) = 2*atan(x / (1 +
197
200
  # sqrt(1+x^2)))`) until `|x| <= 0.5` (where the Taylor series below converges
198
- # quickly), then doubling the result back up the same number of times.
201
+ # quickly), then doubling the result back up the same number of times. Past 2^60 (where
202
+ # `x * x` would overflow before long) it is `Pi/2 - atan(1/|x|)` with the sign of `x`, so
203
+ # `atan(Infinity)` is `Pi/2`; `atan(-0.0)` is `-0.0`.
199
204
  fun atan(x: Float) -> Float =
200
- if x != x
205
+ if x != x || x == 0.0f
201
206
  x
202
207
  else
203
208
  neg := x < 0.0f
204
209
  v := if neg then -x else x
205
- result := atanReduce(v, 0)
210
+ result := if v > 1152921504606846976.0f then Float.HalfPi - atanReduce(1.0f / v, 0) else atanReduce(v, 0)
206
211
  if neg then -result else result
207
212
 
208
213
  fun atanReduce(v: Float, k: Int) -> Float =
@@ -251,9 +256,15 @@ fun acos(x: Float) -> Float =
251
256
 
252
257
  # Angle (in radians) of the point `(x, y)` from the origin, in the correct
253
258
  # quadrant for any sign combination of `x`/`y` (unlike plain `atan(y/x)`,
254
- # which can't distinguish opposite quadrants).
259
+ # which can't distinguish opposite quadrants). A NaN gives NaN; both sides infinite give
260
+ # `±Pi/4` or `±3*Pi/4`.
255
261
  fun atan2(y: Float, x: Float) -> Float =
262
+ if x != x || y != y
263
+ x + y
264
+ else if {x > Float.Max || x < -Float.Max} && {y > Float.Max || y < -Float.Max}
265
+ q := if x > 0.0f then 0.7853981633974483f else 2.356194490192345f
266
+ if y > 0.0f then q else -q
256
- if x > 0.0f
267
+ else if x > 0.0f
257
268
  atan(y / x)
258
269
  else if x < 0.0f && y >= 0.0f
259
270
  atan(y / x) + Float.Pi
@@ -291,20 +302,25 @@ fun hypotScaled(x: Float, y: Float) -> Float =
291
302
  r := small / big
292
303
  big * sqrt(1.0f + r * r)
293
304
 
294
- # Cube root via Newton's method (fixed iteration count, unlike `sqrt`'s
305
+ # Cube root, faithfully rounded (under 0.667 ulp of the true root, the method of FreeBSD's
295
- # converge-to-a-fixed-point loop, since the cubic update step doesn't reach an
306
+ # `s_cbrt.c`): a first guess from the bits (the exponent divided by 3), a polynomial step to
296
- # exact fixed point in float precision as reliably as the quadratic one does).
307
+ # 23 bits, rounded to 23 bits so it cubes exactly, then one Newton step to 53 bits. A
308
+ # subnormal is first scaled by 2^54 (exact), its root then by 2^-18. The sign is kept:
309
+ # `cbrt(-8.0)` is `-2.0`, `cbrt(-0.0)` is `-0.0`; NaN and the infinities pass through.
297
310
  fun cbrt(x: Float) -> Float =
298
- if x == 0.0f || x != x
311
+ if x == 0.0f || x != x || x > Float.Max || x < -Float.Max
299
312
  x
300
313
  else
301
- neg := x < 0.0f
314
+ b := x.toBits()
302
- v := if neg then -x else x
315
+ a := b & 0x7FFF_FFFF_FFFF_FFFF
316
+ r := if {a >> 52} == 0 then cbrtNormal(Float.fromBits(a) * 18014398509481984.0f) * 0.000003814697265625f else cbrtNormal(Float.fromBits(a))
303
- guess := cbrtFrom(v, v, 0)
317
+ Float.fromBits(r.toBits() | {b & Int.Min})
304
- if neg then -guess else guess
305
318
 
319
+ # The cube root of a positive normal float `v`.
306
- fun cbrtFrom(v: Float, guess: Float, i: Int) -> Float =
320
+ fun cbrtNormal(v: Float) -> Float =
307
- if i < 60
308
- cbrtFrom(v, {2.0f * guess + v / {guess * guess}} / 3.0f, i + 1)
321
+ t0 := Float.fromBits({{v.toBits() >> 32} / 3 + 715094163} << 32)
309
- else
322
+ r := t0 * t0 * {t0 / v}
323
+ t1 := t0 * {{1.87595182427177009643f + r * {-1.88497979543377169875f + r * 1.621429720105354466140f}} + r * r * r * {-0.758397934778766047437f + r * 0.145996192886612446982f}}
324
+ t := Float.fromBits({t1.toBits() + 0x8000_0000} & -0x4000_0000)
310
- guess
325
+ q := v / {t * t}
326
+ t + t * {{q - t} / {t + t + q}}
tests/core/float_bits.plum CHANGED
@@ -71,7 +71,7 @@ test "toBits and fromBits in closures"
71
71
 
72
72
  test "sin, cos and tan keep their bits"
73
73
  assert posZero(math.sin(0.0f))
74
- assert posZero(math.sin(-0.0f))
74
+ assert negZero(math.sin(-0.0f))
75
75
  assert math.sin(1.0f).toBits() == 4605754516372524270
76
76
  assert math.sin(-2.5f).toBits() == -4619806259459182587
77
77
  assert math.sin(Float.Pi).toBits() == 4468182615269985440
tests/core/math_edges.plum ADDED
@@ -0,0 +1,153 @@
1
+ # std math at its edges: `Float.logb` read from the bits, `math.cbrt` from a bit-level guess
2
+ # (faithfully rounded), `math.atan` past 2^60 and at the infinities, the sign of zero through
3
+ # `sin` and `atan`, `atan2` with NaNs and infinities, `Int.Min.toStr()`, `Float.toStr` past
4
+ # Int's range, and `Float.hash` on the bits. Expected roots and angles are the correctly
5
+ # rounded doubles (checked against mpmath at 300 bits).
6
+ import std/Float
7
+ import std/Int
8
+ import std/Str
9
+ import std/Map
10
+ import std/Option
11
+ import std/math
12
+
13
+ Inf = 1.0f / 0.0f
14
+ NaN = 0.0f / 0.0f
15
+
16
+ fun negZero(x: Float) -> Bool =
17
+ x == 0.0f && 1.0f / x < 0.0f
18
+
19
+ fun posZero(x: Float) -> Bool =
20
+ x == 0.0f && 1.0f / x > 0.0f
21
+
22
+ test "logb is the exponent, just below a power of two too"
23
+ assert 1.0f.logb() == 0.0f
24
+ assert 8.0f.logb() == 3.0f
25
+ assert 7.999999999999999.logb() == 2.0f
26
+ assert Float.fromBits(0x4000_0000_0000_0000 - 1).logb() == 0.0f
27
+ assert Float.fromBits(0x7FE0_0000_0000_0000 - 1).logb() == 1022.0f
28
+ assert 0.5f.logb() == -1.0f
29
+ assert 0.49999999999999994.logb() == -2.0f
30
+ assert {-1024.0f}.logb() == 10.0f
31
+ assert {-1023.9999999999999}.logb() == 9.0f
32
+ assert Float.Max.logb() == 1023.0f
33
+ assert 1.0e300.logb() == 996.0f
34
+ assert 1.0e-300.logb() == -997.0f
35
+
36
+ test "logb of subnormals, zeros, infinities and NaN"
37
+ assert Float.Min.logb() == -1074.0f
38
+ assert {-Float.Min}.logb() == -1074.0f
39
+ assert Float.fromBits(0x000F_FFFF_FFFF_FFFF).logb() == -1023.0f
40
+ assert Float.fromBits(0x0010_0000_0000_0000).logb() == -1022.0f
41
+ assert Float.fromBits(0x0008_0000_0000_0000).logb() == -1023.0f
42
+ assert Float.fromBits(0x0007_FFFF_FFFF_FFFF).logb() == -1024.0f
43
+ assert 0.0f.logb() == -Inf
44
+ assert {-0.0f}.logb() == -Inf
45
+ assert Inf.logb() == Inf
46
+ assert {-Inf}.logb() == Inf
47
+ assert NaN.logb().isNaN()
48
+
49
+ test "Int.logb agrees with Float.logb"
50
+ assert Int.Min.logb() == 63.0f
51
+ assert Int.Min.logb() == Float(Int.Min).logb()
52
+ assert Int.Max.logb() == 62.0f
53
+ assert 1023.logb() == Float(1023).logb()
54
+ assert {-8}.logb() == {-8.0f}.logb()
55
+
56
+ test "cbrt of exact cubes and ordinary values"
57
+ assert math.cbrt(27.0f) == 3.0f
58
+ assert math.cbrt(-8.0f) == -2.0f
59
+ assert math.cbrt(1.0f) == 1.0f
60
+ assert math.cbrt(0.001f) == 0.1f
61
+ assert math.cbrt(2.0f) == 1.2599210498948732f
62
+ assert math.cbrt(1000000.0f) == 100.0f
63
+
64
+ test "cbrt of huge and tiny values"
65
+ assert math.cbrt(1.0e300) == 1.0e100
66
+ assert math.cbrt(-1.0e300) == -1.0e100
67
+ assert math.cbrt(1.0e-300) == 1.0e-100
68
+ assert math.cbrt(Float.Max) == 5.643803094122362e+102
69
+ assert math.cbrt(Float.Min) == 1.7031839360032603e-108
70
+ assert math.cbrt(-1.0e-310) == -4.641588833612774e-104
71
+
72
+ test "cbrt of zeros, infinities and NaN"
73
+ assert posZero(math.cbrt(0.0f))
74
+ assert negZero(math.cbrt(-0.0f))
75
+ assert math.cbrt(Inf) == Inf
76
+ assert math.cbrt(-Inf) == -Inf
77
+ assert math.cbrt(NaN).isNaN()
78
+
79
+ test "atan of huge values and the infinities"
80
+ assert math.atan(1.0e300) == Float.HalfPi
81
+ assert math.atan(-1.0e200) == -Float.HalfPi
82
+ assert math.atan(1.0e155) == Float.HalfPi
83
+ assert math.atan(2305843009213693952.0f) == Float.HalfPi
84
+ assert math.atan(Float.Max) == Float.HalfPi
85
+ assert math.atan(Inf) == Float.HalfPi
86
+ assert math.atan(-Inf) == -Float.HalfPi
87
+
88
+ test "sin, tan and atan keep the sign of zero"
89
+ assert negZero(math.sin(-0.0f))
90
+ assert posZero(math.sin(0.0f))
91
+ assert negZero(math.tan(-0.0f))
92
+ assert negZero(math.atan(-0.0f))
93
+ assert posZero(math.atan(0.0f))
94
+ assert math.cos(-0.0f) == 1.0f
95
+
96
+ test "atan2 with huge ratios, NaNs and infinities"
97
+ assert math.atan2(1.0e300, 1.0e-10) == Float.HalfPi
98
+ assert math.atan2(-1.0e300, 1.0e-10) == -Float.HalfPi
99
+ assert math.atan2(1.0f, Inf) == 0.0f
100
+ assert math.atan2(Inf, 1.0f) == Float.HalfPi
101
+ assert math.atan2(Inf, Inf) == 0.7853981633974483f
102
+ assert math.atan2(Inf, -Inf) == 2.356194490192345f
103
+ assert math.atan2(-Inf, Inf) == -0.7853981633974483f
104
+ assert math.atan2(-Inf, -Inf) == -2.356194490192345f
105
+ assert math.atan2(NaN, 1.0f).isNaN()
106
+ assert math.atan2(1.0f, NaN).isNaN()
107
+ assert math.atan2(0.0f, NaN).isNaN()
108
+
109
+ test "Int.Min and every Int prints its digits"
110
+ assert Int.Min.toStr() == "-9223372036854775808"
111
+ assert Int.Max.toStr() == "9223372036854775807"
112
+ assert {Int.Min + 1}.toStr() == "-9223372036854775807"
113
+ assert {-5}.toStr() == "-5"
114
+ assert {-10}.toStr() == "-10"
115
+ assert 0.toStr() == "0"
116
+ assert "{Int.Min}" == "-9223372036854775808"
117
+ assert Int.parse(Int.Min.toStr()).unwrapOr(0) == Int.Min
118
+
119
+ test "Float.toStr past Int's range is the exact whole value"
120
+ assert 1.0e20.toStr() == "100000000000000000000.0"
121
+ assert {-1.0e20}.toStr() == "-100000000000000000000.0"
122
+ assert 9.2e18.toStr() == "9200000000000000000.0"
123
+ assert 9223372036854775808.0f.toStr() == "9223372036854775808.0"
124
+ assert {-9223372036854775808.0f}.toStr() == "-9223372036854775808.0"
125
+ assert 1.0e300.toStr() == "1000000000000000052504760255204420248704468581108159154915854115511802457988908195786371375080447864043704443832883878176942523235360430575644792184786706982848387200926575803737830233794788090059368953234970799945081119038967640880074652742780142494579258788820056842838115669472196386865459400540160.0"
126
+ assert Float.Max.toStr() == "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0"
127
+ assert {-Float.Max}.toStr().length() == Float.Max.toStr().length() + 1
128
+ assert {-Float.Max}.toStr().byteAt(0) == 45
129
+ assert 1.5.toStr() == "1.5"
130
+ assert {-2.25}.toStr() == "-2.25"
131
+
132
+ test "Float.hash uses the bits, and Floats that are == hash the same"
133
+ assert 0.1.hash() != 0.2.hash()
134
+ assert 0.1.hash() != {-0.1}.hash()
135
+ assert 0.5.hash() != 0.25.hash()
136
+ assert 0.0f.hash() == {-0.0f}.hash()
137
+ assert 1.0f.hash() == 1.0f.toBits()
138
+ assert NaN.hash() == {-NaN}.hash()
139
+ assert NaN.hash() == Float.fromBits(0x7FF8_0000_DEAD_BEEF).hash()
140
+ assert Inf.hash() != {-Inf}.hash()
141
+
142
+ test "Map with Float keys in (-1, 1)"
143
+ m: Map[Float, Int] := Map.empty()
144
+ m.set(0.1f, 1)
145
+ m.set(0.2f, 2)
146
+ m.set(-0.5f, 3)
147
+ m.set(0.0f, 4)
148
+ assert m.length() == 4
149
+ assert m.get(0.1f).unwrapOr(0) == 1
150
+ assert m.get(0.2f).unwrapOr(0) == 2
151
+ assert m.get(-0.5f).unwrapOr(0) == 3
152
+ assert m.get(-0.0f).unwrapOr(0) == 4
153
+ assert m.get(0.3f).isNone()