plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
df97001
— Peter John
2026-11-10T21:41:08+05:30
Decimal: an estimate past Float.Max still rounds to it, and a tie between two last digits takes the even one
- std/Decimal.plum +7 -2
- tests/std/Decimal.plum +14 -0
std/Decimal.plum
CHANGED
|
@@ -221,7 +221,10 @@ struct Decimal(digits: Str, exp: Int, neg: Bool) =
|
|
|
221
221
|
if e >= 0 then m * pow10Exact(e) else m / pow10Exact(0 - e)
|
|
222
222
|
else
|
|
223
223
|
k := if n < 18 then n else 18
|
|
224
|
+
# An estimate that overflowed starts at Float.Max: the value may still round to it
|
|
225
|
+
# (`1.7976931348623158e308`), and `nearest` steps up to Infinity when it doesn't.
|
|
224
|
-
|
|
226
|
+
rough := scaleApprox(Float(digitsValue(ds, 0, k, 0)), e + n - k)
|
|
227
|
+
guess := if rough > Float.Max then Float.Max else rough
|
|
225
228
|
big := bigOfDigits(ds, 0, n, Slice<Int>())
|
|
226
229
|
left := if e >= 0 then bigMulPow10(big, e) else big
|
|
227
230
|
nearest(left, if e >= 0 then 0 else e, guess)
|
|
@@ -327,8 +330,10 @@ struct Decimal(digits: Str, exp: Int, neg: Bool) =
|
|
|
327
330
|
else if !tc1 && tc2
|
|
328
331
|
out.writeByte(Byte(49 + d))
|
|
329
332
|
else
|
|
333
|
+
# Both `d` and `d + 1` read back as `x`: the nearer one, and at a tie the even one
|
|
334
|
+
# (as JavaScript and every shortest-digits printer choose).
|
|
330
335
|
half := bigCmp(bigShl(rest, 1), s)
|
|
331
|
-
out.writeByte(Byte(if half < 0 then 48 + d else 49 + d))
|
|
336
|
+
out.writeByte(Byte(if half < 0 || half == 0 && d % 2 == 0 then 48 + d else 49 + d))
|
|
332
337
|
|
|
333
338
|
# r / s as a digit 0..9 and the remainder, by subtracting (r < 10 s).
|
|
334
339
|
fun quotientDigit(r: Slice<Int>, s: Slice<Int>, d: Int) -> Pair<Int, Slice<Int>> =
|
tests/std/Decimal.plum
CHANGED
|
@@ -125,3 +125,17 @@ test "6000 random bit patterns (and their negatives) round-trip through ofFloat,
|
|
|
125
125
|
Pair(first: x3, second: if roundTrips(x3) && roundTrips(x3 >> 12) then acc.second else acc.second + 1)
|
|
126
126
|
)
|
|
127
127
|
assert bad.second == 0
|
|
128
|
+
|
|
129
|
+
test "toFloat next to Float.Max: an estimate that overflows still rounds down to it"
|
|
130
|
+
assert parsed("1.7976931348623158e308") == Float.Max
|
|
131
|
+
assert parsed("1.797693134862315807e308") == Float.Max
|
|
132
|
+
assert parsed("1.797693134862315808e308") > Float.Max
|
|
133
|
+
assert parsed("1.7976931348623159e308") > Float.Max && parsed("-1.7976931348623158e308") == 0.0 - Float.Max
|
|
134
|
+
assert parsed("17976931348623158{"0".repeat(292)}") == Float.Max
|
|
135
|
+
|
|
136
|
+
test "ofFloat: when two last digits are equally near, the even one"
|
|
137
|
+
assert shortText(0.000000029802322387695312) == "2.9802322387695312e-8"
|
|
138
|
+
assert shortText(1125899906842624.2) == "1125899906842624.2"
|
|
139
|
+
assert shortText(26363981746409.312) == "26363981746409.312"
|
|
140
|
+
assert shortText(-108868734838530.12) == "-108868734838530.12"
|
|
141
|
+
assert shortText(1125899906842624.8) == "1125899906842624.8"
|