Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-d:nimPreviewFloatRoundtrip becomes the default #24217

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## Changes affecting backward compatibility

- `-d:nimPreviewFloatRoundtrip` becomes the default. `system.addFloat` and `system.$` now can produce string representations of
floating point numbers that are minimal in size and possess round-trip and correct
rounding guarantees (via the
[Dragonbox](https://raw.githubusercontent.com/jk-jeon/dragonbox/master/other_files/Dragonbox.pdf) algorithm).

## Standard library additions and changes

Expand Down
1 change: 0 additions & 1 deletion compiler/nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ hint[XDeclaredButNotUsed]:off

define:booting
define:nimcore
define:nimPreviewFloatRoundtrip
define:nimPreviewSlimSystem
define:nimPreviewCstringConversion
define:nimPreviewProcConversion
Expand Down
14 changes: 3 additions & 11 deletions lib/std/formatfloat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ proc writeFloatToBufferSprintf*(buf: var array[65, char]; value: BiggestFloat):
result = 3

proc writeFloatToBuffer*(buf: var array[65, char]; value: BiggestFloat | float32): int {.inline.} =
when defined(nimPreviewFloatRoundtrip) or defined(nimPreviewSlimSystem):
writeFloatToBufferRoundtrip(buf, value)
else:
writeFloatToBufferSprintf(buf, value)
writeFloatToBufferRoundtrip(buf, value)

proc addFloatRoundtrip*(result: var string; x: float | float32) =
when nimvm:
Expand Down Expand Up @@ -126,16 +123,11 @@ proc addFloat*(result: var string; x: float | float32) {.inline.} =
b = 45.67
s.addFloat(45.67)
assert s == "foo:45.67"
template impl =
when defined(nimPreviewFloatRoundtrip) or defined(nimPreviewSlimSystem):
addFloatRoundtrip(result, x)
else:
addFloatSprintf(result, x)
when defined(js):
when nimvm: impl()
when nimvm: addFloatRoundtrip(result, x)
else:
result.add nimFloatToString(x)
else: impl()
else: addFloatRoundtrip(result, x)

when defined(nimPreviewSlimSystem):
func `$`*(x: float | float32): string =
Expand Down
1 change: 0 additions & 1 deletion tests/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ hint("Processing", off)
switch("define", "nimExperimentalLinenoiseExtra")

# preview APIs are expected to be the new default in upcoming versions
switch("define", "nimPreviewFloatRoundtrip")
#switch("define", "nimPreviewDotLikeOps") # deprecated?
switch("define", "nimPreviewJsonutilsHoleyEnum")
switch("define", "nimPreviewHashRef")
Expand Down
22 changes: 5 additions & 17 deletions tests/float/tfloats.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
discard """
matrix: "-d:nimPreviewFloatRoundtrip; -u:nimPreviewFloatRoundtrip"
targets: "c cpp js"
"""

Expand Down Expand Up @@ -66,29 +65,18 @@ template main =
block: # example 1
let a = 0.1+0.2
doAssert a != 0.3
when defined(nimPreviewFloatRoundtrip):
doAssert $a == "0.30000000000000004"
else:
whenRuntimeJs: discard
do: doAssert $a == "0.3"
doAssert $a == "0.30000000000000004"
block: # example 2
const a = 0.1+0.2
when defined(nimPreviewFloatRoundtrip):
doAssert $($a, a) == """("0.30000000000000004", 0.30000000000000004)"""
else:
whenRuntimeJs: discard
do: doAssert $($a, a) == """("0.3", 0.3)"""
doAssert $($a, a) == """("0.30000000000000004", 0.30000000000000004)"""
block: # example 3
const a1 = 0.1+0.2
let a2 = a1
doAssert a1 != 0.3
when defined(nimPreviewFloatRoundtrip):
doAssert $[$a1, $a2] == """["0.30000000000000004", "0.30000000000000004"]"""
else:
whenRuntimeJs: discard
do: doAssert $[$a1, $a2] == """["0.3", "0.3"]"""
doAssert $[$a1, $a2] == """["0.30000000000000004", "0.30000000000000004"]"""


when defined(nimPreviewFloatRoundtrip):
when true:
block: # bug #18148
var a = 1.1'f32
doAssert $a == "1.1", $a # was failing
Expand Down
Loading