summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2020-12-29 08:50:22 -0600
committerGitHub <noreply@github.com>2020-12-29 15:50:22 +0100
commit89a2390f8bcf8615466aea9fd2653f57829404c4 (patch)
tree6301919cafcf751d9e0ef15a49487fe7c250c0d1
parent732419ae907208b0b484911b996893097402a6c7 (diff)
downloadNim-89a2390f8bcf8615466aea9fd2653f57829404c4.tar.gz
fix printing negative zero in JS backend (#16505)
-rw-r--r--lib/system/jssys.nim4
-rw-r--r--tests/misc/tnegativezero.nim30
2 files changed, 33 insertions, 1 deletions
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()