From 89a2390f8bcf8615466aea9fd2653f57829404c4 Mon Sep 17 00:00:00 2001 From: flywind <43030857+xflywind@users.noreply.github.com> Date: Tue, 29 Dec 2020 08:50:22 -0600 Subject: fix printing negative zero in JS backend (#16505) --- lib/system/jssys.nim | 4 +++- tests/misc/tnegativezero.nim | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/misc/tnegativezero.nim diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 5f18f01cb..64c766482 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -495,11 +495,13 @@ proc negInt64(a: int64): int64 {.compilerproc.} = proc nimFloatToString(a: float): cstring {.compilerproc.} = ## ensures the result doesn't print like an integer, i.e. return 2.0, not 2 + # print `-0.0` properly asm """ function nimOnlyDigitsOrMinus(n) { return n.toString().match(/^-?\d+$/); } - if (Number.isSafeInteger(`a`)) `result` = `a`+".0" + if (Number.isSafeInteger(`a`)) + `result` = `a` === 0 && 1 / `a` < 0 ? "-0.0" : `a`+".0" else { `result` = `a`+"" if(nimOnlyDigitsOrMinus(`result`)){ diff --git a/tests/misc/tnegativezero.nim b/tests/misc/tnegativezero.nim new file mode 100644 index 000000000..a443e40cf --- /dev/null +++ b/tests/misc/tnegativezero.nim @@ -0,0 +1,30 @@ +discard """ + targets: "c cpp js" +""" + +proc main()= + block: + let a = -0.0 + doAssert $a == "-0.0" + doAssert $(-0.0) == "-0.0" + + block: + let a = 0.0 + when nimvm: discard ## TODO VM print wrong -0.0 + else: + doAssert $a == "0.0" + doAssert $(0.0) == "0.0" + + block: + let b = -0 + doAssert $b == "0" + doAssert $(-0) == "0" + + block: + let b = 0 + doAssert $b == "0" + doAssert $(0) == "0" + + +static: main() +main() -- cgit 1.4.1-2-gfad0