fix printing negative zero in JS backend (#16505)
This commit is contained in:
parent
732419ae90
commit
89a2390f8b
2 changed files with 33 additions and 1 deletions
|
|
@ -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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue