summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2021-01-11 04:39:38 -0600
committerGitHub <noreply@github.com>2021-01-11 11:39:38 +0100
commitbe6e8916faa51d227d51e1291d1f24751385b010 (patch)
tree4f4e7d5e843b3b850f5cfb4dfdd3030b73310976 /compiler
parent5897ed9d3d5ca5f84423e87a70addc8c6764923e (diff)
downloadNim-be6e8916faa51d227d51e1291d1f24751385b010.tar.gz
fix negative nan (#16628)
Diffstat (limited to 'compiler')
-rw-r--r--compiler/jsgen.nim5
-rw-r--r--compiler/rodutils.nim13
2 files changed, 15 insertions, 3 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index f588f9555..10a33423e 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -2485,7 +2485,10 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
     let f = n.floatVal
     case classify(f)
     of fcNan:
-      r.res = rope"NaN"
+      if signbit(f):
+        r.res = rope"-NaN"
+      else:
+        r.res = rope"NaN"
     of fcNegZero:
       r.res = rope"-0.0"
     of fcZero:
diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim
index 7070e6c3f..353992fca 100644
--- a/compiler/rodutils.nim
+++ b/compiler/rodutils.nim
@@ -8,7 +8,7 @@
 #
 
 ## Serialization utilities for the compiler.
-import strutils, math
+import std/[strutils, math]
 
 # bcc on windows doesn't have C99 functions
 when defined(windows) and defined(bcc):
@@ -33,10 +33,19 @@ when defined(windows) and defined(bcc):
 
 proc c_snprintf(s: cstring; n:uint; frmt: cstring): cint {.importc: "snprintf", header: "<stdio.h>", nodecl, varargs.}
 
+
+when not declared(signbit):
+  proc c_signbit(x: SomeFloat): cint {.importc: "signbit", header: "<math.h>".}
+  proc signbit*(x: SomeFloat): bool {.inline.} =
+    result = c_signbit(x) != 0
+
 proc toStrMaxPrecision*(f: BiggestFloat, literalPostfix = ""): string =
   case classify(f)
   of fcNan:
-    result = "NAN"
+    if signbit(f):
+      result = "-NAN"
+    else:
+      result = "NAN"
   of fcNegZero:
     result = "-0.0" & literalPostfix
   of fcZero: