diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2023-08-18 00:52:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-17 18:52:38 +0200 |
commit | 7fababd583ee5e3c113c0d83a04c07f2ee0ef06d (patch) | |
tree | 1adb0cab8dcb34c57a3709953dbe79fa2d6d644a | |
parent | 98c39e8e571b95e7d9351c7115f897ce9af1a218 (diff) | |
download | Nim-7fababd583ee5e3c113c0d83a04c07f2ee0ef06d.tar.gz |
make float32 literals stringifying behave in JS the same as in C (#22500)
-rw-r--r-- | compiler/jsgen.nim | 9 | ||||
-rw-r--r-- | tests/float/tfloats.nim | 5 |
2 files changed, 9 insertions, 5 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index c566a718a..1720de17f 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -32,7 +32,7 @@ import ast, trees, magicsys, options, nversion, msgs, idents, types, ropes, ccgutils, wordrecg, renderer, - cgmeth, lowerings, sighashes, modulegraphs, lineinfos, rodutils, + cgmeth, lowerings, sighashes, modulegraphs, lineinfos, transf, injectdestructors, sourcemap, astmsgs, backendpragmas import pipelineutils @@ -43,6 +43,7 @@ import strutils except addf when defined(nimPreviewSlimSystem): import std/[assertions, syncio] +import std/formatfloat type TJSGen = object of PPassContext @@ -2900,7 +2901,11 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) = r.res = rope"Infinity" of fcNegInf: r.res = rope"-Infinity" - else: r.res = rope(f.toStrMaxPrecision) + else: + if n.typ.skipTypes(abstractVarRange).kind == tyFloat32: + r.res.addFloatRoundtrip(f.float32) + else: + r.res.addFloatRoundtrip(f) r.kind = resExpr of nkCallKinds: if isEmptyType(n.typ): diff --git a/tests/float/tfloats.nim b/tests/float/tfloats.nim index 967605c53..aaed2d615 100644 --- a/tests/float/tfloats.nim +++ b/tests/float/tfloats.nim @@ -156,9 +156,8 @@ template main = when nimvm: discard # xxx, refs #12884 else: - when not defined(js): - doAssert x == 1.2345679'f32 - doAssert $x == "1.2345679" + doAssert x == 1.2345679'f32 + doAssert $x == "1.2345679" static: main() main() |