mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-20 07:51:32 +00:00
fix printing negative zero in JS backend (#16505)
This commit is contained in:
@@ -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`)){
|
||||
|
||||
30
tests/misc/tnegativezero.nim
Normal file
30
tests/misc/tnegativezero.nim
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user