Skip to content

Commit 25efb53

Browse files
authored
make dragonbox opt-in via -d:nimFpRoundtrips (#18504)
* make dragonbox opt-in via -d:nimFpRoundtrips * make tests green again * make tests green again
1 parent 96a7f9b commit 25efb53

File tree

8 files changed

+35
-27
lines changed

8 files changed

+35
-27
lines changed

changelog.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@
7676
- `json` and `jsonutils` now serialize NaN, Inf, -Inf as strings, so that
7777
`%[NaN, -Inf]` is the string `["nan","-inf"]` instead of `[nan,-inf]` which was invalid json.
7878

79-
- `system.addFloat` now uses the "Dragonbox" algorithm, which ensures correct roundtrips of floating point
80-
numbers, that the minimum length representation of a floating point number is used and correct rounding.
81-
Use `-d:nimLegacyAddFloat` for a transition period.
8279

8380
- `strformat` is now part of `include std/prelude`.
8481

@@ -102,6 +99,12 @@
10299
added support for parenthesized expressions.
103100
added support for const string's instead of just string literals
104101

102+
103+
- `system.addFloat` and `system.$` now can produce string representations of floating point numbers
104+
that are minimal in size and that "roundtrip" (via the "Dragonbox" algorithm). This currently has
105+
to be enabled via `-d:nimFpRoundtrips`. It is expected that this behavior becomes the new default
106+
in upcoming versions.
107+
105108
- Fixed buffer overflow bugs in `net`
106109

107110
- Exported `sslHandle` from `net` and `asyncnet`.
@@ -116,8 +119,6 @@
116119
the OpenSSL DLLs (e.g. libssl-1_1-x64.dll, libcrypto-1_1-x64.dll) you
117120
now also need to ship `cacert.pem` with your `.exe` file.
118121

119-
- Make `{.requiresInit.}` pragma to work for `distinct` types.
120-
121122
- `typetraits`:
122123
`distinctBase` now is identity instead of error for non distinct types.
123124
Added `enumLen` to return the number of elements in an enum.
@@ -468,7 +469,6 @@
468469
with `--styleCheck:error` or `--styleCheck:hint`.
469470

470471

471-
472472
## Tool changes
473473

474474
- Latex doc generation is revised: output `.tex` files should be compiled

lib/system/formatfloat.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# distribution, for details about the copyright.
88
#
99

10-
when not defined(nimLegacyAddFloat) and not defined(nimscript) and
10+
when defined(nimFpRoundtrips) and not defined(nimscript) and
1111
not defined(js) and defined(nimHasDragonBox):
1212
import dragonbox
1313

lib/system/strmantle.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ proc nimFloatToStr(f: float): string {.compilerproc.} =
9797
result = newStringOfCap(8)
9898
result.addFloat f
9999

100-
when not defined(nimLegacyAddFloat) and not defined(nimscript) and
100+
when defined(nimFpRoundtrips) and not defined(nimscript) and
101101
not defined(js) and defined(nimHasDragonBox):
102102
import schubfach
103103

tests/errmsgs/treportunused.nim

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ treportunused.nim(30, 5) Hint: 's8' is declared but not used [XDeclaredButNotUse
1313
treportunused.nim(31, 5) Hint: 's9' is declared but not used [XDeclaredButNotUsed]
1414
treportunused.nim(32, 6) Hint: 's10' is declared but not used [XDeclaredButNotUsed]
1515
treportunused.nim(33, 6) Hint: 's11' is declared but not used [XDeclaredButNotUsed]
16-
treportunused.nim(37, 3) Hint: 'v0.99' is declared but not used [XDeclaredButNotUsed]
17-
treportunused.nim(38, 3) Hint: 'v0.99.99' is declared but not used [XDeclaredButNotUsed]
1816
'''
1917
action: compile
2018
"""
2119

20+
#treportunused.nim(37, 3) Hint: 'v0.99' is declared but not used [XDeclaredButNotUsed]
21+
#treportunused.nim(38, 3) Hint: 'v0.99.99' is declared but not used [XDeclaredButNotUsed]
2222
# bug #9764
2323
iterator s1(a:string): int = discard
2424
iterator s2(): int = discard
@@ -32,7 +32,9 @@ var s9: int
3232
type s10 = object
3333
type s11 = type(1.2)
3434

35-
# https://github.com/nim-lang/Nim/issues/14407
36-
let
37-
`v0.99` = "0.99"
38-
`v0.99.99` = "0.99.99"
35+
when false:
36+
# enabled again when Nim bootstraps with -d:nimFpRoundtrips
37+
# https://github.com/nim-lang/Nim/issues/14407
38+
let
39+
`v0.99` = "0.99"
40+
`v0.99.99` = "0.99.99"

tests/float/nim.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-d:nimFpRoundtrips

tests/float/tfloats.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ template main =
6666
var a = 1.1'f32
6767
doAssert $a == "1.1", $a # was failing
6868

69+
proc runtimeOnlyTests =
70+
# enable for 'static' once -d:nimFpRoundtrips became the default
6971
block: # bug #7717
7072
proc test(f: float) =
7173
let f2 = $f
@@ -82,3 +84,5 @@ template main =
8284

8385
static: main()
8486
main()
87+
88+
runtimeOnlyTests()

tests/stdlib/tjson.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ let jsonNode = %*mynode
303303
doAssert $jsonNode == """{"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}"""
304304
doAssert $jsonNode.to(ContentNode) == """(kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])"""
305305

306-
block: # bug #17383
306+
when defined(nimFpRoundtrips): # bug #17383
307307
testRoundtrip(int32.high): "2147483647"
308308
testRoundtrip(uint32.high): "4294967295"
309309
when int.sizeof == 4:
@@ -316,7 +316,7 @@ block: # bug #17383
316316
testRoundtrip(int64.high): "9223372036854775807"
317317
testRoundtrip(uint64.high): "18446744073709551615"
318318

319-
block: # bug #18007
319+
when defined(nimFpRoundtrips): # bug #18007
320320
testRoundtrip([NaN, Inf, -Inf, 0.0, -0.0, 1.0]): """["nan","inf","-inf",0.0,-0.0,1.0]"""
321321
# pending https://github.com/nim-lang/Nim/issues/18025 use:
322322
# testRoundtrip([float32(NaN), Inf, -Inf, 0.0, -0.0, 1.0])
@@ -332,7 +332,7 @@ block: # bug #18007
332332
testRoundtripVal(0.0): "0.0"
333333
testRoundtripVal(-0.0): "-0.0"
334334

335-
block: # bug #15397, bug #13196
335+
when defined(nimFpRoundtrips): # bug #15397, bug #13196
336336
testRoundtripVal(1.0 + epsilon(float64)): "1.0000000000000002"
337337
testRoundtripVal(0.12345678901234567890123456789): "0.12345678901234568"
338338

tests/stdlib/tjsonutils.nim

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ proc `$`(a: MyEnum): string =
4444
if a == me2: "me2Modif"
4545
else: system.`$`(a)
4646

47-
template fn() =
47+
template fn() =
4848
block: # toJson, jsonTo
4949
type Foo = distinct float
5050
testRoundtrip('x', """120""")
@@ -161,15 +161,16 @@ template fn() =
161161
doAssert b[2].signbit
162162
doAssert not b[3].signbit
163163

164-
block: # bug #15397, bug #13196
165-
let a = 0.1
166-
let x = 0.12345678901234567890123456789
167-
let b = (a + 0.2, 0.3, x)
168-
testRoundtripVal(b): "[0.30000000000000004,0.3,0.12345678901234568]"
164+
when defined(nimFpRoundtrips):
165+
block: # bug #15397, bug #13196
166+
let a = 0.1
167+
let x = 0.12345678901234567890123456789
168+
let b = (a + 0.2, 0.3, x)
169+
testRoundtripVal(b): "[0.30000000000000004,0.3,0.12345678901234568]"
169170

170-
testRoundtripVal(0.12345678901234567890123456789): "0.12345678901234568"
171-
testRoundtripVal(epsilon(float64)): "2.220446049250313e-16"
172-
testRoundtripVal(1.0 + epsilon(float64)): "1.0000000000000002"
171+
testRoundtripVal(0.12345678901234567890123456789): "0.12345678901234568"
172+
testRoundtripVal(epsilon(float64)): "2.220446049250313e-16"
173+
testRoundtripVal(1.0 + epsilon(float64)): "1.0000000000000002"
173174

174175
block: # case object
175176
type Foo = object
@@ -433,7 +434,7 @@ template fn() =
433434
"""{"b": true, "bt": false, "btf": "test"}"""
434435
testRoundtrip(Variant(b: true, bt: true, btt: 'c')):
435436
"""{"b": true, "bt": true, "btt": "c"}"""
436-
437+
437438
# TODO: Add additional tests with missing and extra JSON keys, both when
438439
# allowed and forbidden analogous to the tests for the not nested
439440
# variant objects.

0 commit comments

Comments
 (0)