plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
ceb4483
— Peter John
2026-11-10T22:01:09+05:30
Decimal: digits past the 800th only count as a sticky digit
- docs/std.md +1 -1
- std/Decimal.plum +17 -8
- tests/std/Decimal.plum +15 -0
docs/std.md
CHANGED
|
@@ -211,7 +211,7 @@ fun main() =
|
|
|
211
211
|
```
|
|
212
212
|
|
|
213
213
|
- **`Decimal.parse(text)`**: `[+-]digits[.digits][(e|E)[+-]digits]` read exactly, an `Option<Decimal>`. Trailing zeros are kept (`1.50` is 150 * 10^-2).
|
|
214
|
-
- **`d.toFloat()`**: the Float nearest `d`, ties to even, as IEEE 754 and every correct `strtod` give; past Float's range an infinity, below it a zero. `Decimal.parseFloat(text)` is `parse` then `toFloat` without the `Decimal` in between (an `Option<Float>`); `Float.parse` and `Json` read numbers with it.
|
|
214
|
+
- **`d.toFloat()`**: the Float nearest `d`, ties to even, as IEEE 754 and every correct `strtod` give; past Float's range an infinity, below it a zero. Digits past the 800th only count as whether a nonzero one follows (a halfway point between two Floats has at most 767), so a million-digit text converts quickly and still exactly. `Decimal.parseFloat(text)` is `parse` then `toFloat` without the `Decimal` in between (an `Option<Float>`); `Float.parse` and `Json` read numbers with it.
|
|
215
215
|
- **`Decimal.ofFloat(x)`**: the shortest digits that convert back to `x`. **`Decimal.exact(x)`**: the value `x` holds, digit for digit (`Decimal.exact(1e23)` is 99999999999999991611392). Both need a finite `x`; a NaN or an infinity stops the program.
|
|
216
216
|
- **`d.toStr()`**: the way JavaScript writes a number: plain digits from 1e-6 up to 1e21, exponent notation outside (`1e+21`, `1.5e-7`), `-0` for minus zero. `d.isZero()`.
|
|
217
217
|
|
std/Decimal.plum
CHANGED
|
@@ -219,15 +219,24 @@ struct Decimal(digits: Str, exp: Int, neg: Bool) =
|
|
|
219
219
|
else if n <= 15 && e >= -22 && e <= 22
|
|
220
220
|
m := Float(digitsValue(ds, 0, n, 0))
|
|
221
221
|
if e >= 0 then m * pow10Exact(e) else m / pow10Exact(0 - e)
|
|
222
|
+
else if n > 800
|
|
223
|
+
# A halfway point between two Floats has at most 767 significant digits, so past
|
|
224
|
+
# 800 only whether a nonzero digit follows matters: 800 digits and a sticky 1 round
|
|
225
|
+
# the same way, and the big-integer work stays bounded for a text of any length.
|
|
226
|
+
nearestOf("{ds.sub(0, 800)}1", e + n - 801, 801)
|
|
222
227
|
else
|
|
228
|
+
nearestOf(ds, e, n)
|
|
229
|
+
|
|
230
|
+
# The Float nearest `ds * 10^e`, for `n` digits `ds` with no zero at either end.
|
|
231
|
+
fun nearestOf(ds: Str, e: Int, n: Int) -> Float =
|
|
223
|
-
|
|
232
|
+
k := if n < 18 then n else 18
|
|
224
|
-
|
|
233
|
+
# An estimate that overflowed starts at Float.Max: the value may still round to it
|
|
225
|
-
|
|
234
|
+
# (`1.7976931348623158e308`), and `nearest` steps up to Infinity when it doesn't.
|
|
226
|
-
|
|
235
|
+
rough := scaleApprox(Float(digitsValue(ds, 0, k, 0)), e + n - k)
|
|
227
|
-
|
|
236
|
+
guess := if rough > Float.Max then Float.Max else rough
|
|
228
|
-
|
|
237
|
+
big := bigOfDigits(ds, 0, n, Slice<Int>())
|
|
229
|
-
|
|
238
|
+
left := if e >= 0 then bigMulPow10(big, e) else big
|
|
230
|
-
|
|
239
|
+
nearest(left, if e >= 0 then 0 else e, guess)
|
|
231
240
|
|
|
232
241
|
fun firstNonZero(s: Str, i: Int) -> Int =
|
|
233
242
|
if i < s.length() && s.byteAtUnchecked(i) == 48 then firstNonZero(s, i + 1) else i
|
tests/std/Decimal.plum
CHANGED
|
@@ -139,3 +139,18 @@ test "ofFloat: when two last digits are equally near, the even one"
|
|
|
139
139
|
assert shortText(26363981746409.312) == "26363981746409.312"
|
|
140
140
|
assert shortText(-108868734838530.12) == "-108868734838530.12"
|
|
141
141
|
assert shortText(1125899906842624.8) == "1125899906842624.8"
|
|
142
|
+
|
|
143
|
+
test "toFloat of a million digits: past 800 only a sticky digit counts, so it is quick and still exact"
|
|
144
|
+
# the halfway point between 0.0 and 5e-324 (2^-1075, 751 significant digits) rounds to even
|
|
145
|
+
hd := "24703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125"
|
|
146
|
+
half := "{hd.sub(0, 1)}.{hd.sub(1, hd.length())}e-324"
|
|
147
|
+
assert parsed(half) == 0.0
|
|
148
|
+
# a nonzero digit 1,000 places past the halfway point decides it, though only 800 are kept
|
|
149
|
+
above := "{hd.sub(0, 1)}.{hd.sub(1, hd.length())}{"0".repeat(1000)}1e-324"
|
|
150
|
+
assert parsed(above) == 5e-324
|
|
151
|
+
below := "{hd.sub(0, 1)}.{hd.sub(1, hd.length() - 1)}4{"9".repeat(1000)}e-324"
|
|
152
|
+
assert parsed(below) == 0.0
|
|
153
|
+
assert parsed("1{"0".repeat(1000000)}e-1000000") == 1.0
|
|
154
|
+
assert parsed("0.{"3".repeat(1000000)}") == 0.3333333333333333
|
|
155
|
+
assert parsed("9007199254740993{"0".repeat(2000)}e-2000") == 9007199254740992.0
|
|
156
|
+
assert parsed("9007199254740993{"0".repeat(2000)}1e-2001") == 9007199254740994.0
|