Skip to content

Commit

Permalink
fix printing negative zero in JS backend (#16505)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Dec 29, 2020
1 parent 732419a commit 89a2390
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/system/jssys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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`)){
Expand Down
30 changes: 30 additions & 0 deletions tests/misc/tnegativezero.nim
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 89a2390

Please sign in to comment.