Skip to content

Commit 5b30828

Browse files
committed
Use the actual NaN value in the math package.
Apparently, under V8 division by zero literal and by zero assigned to a variable yields bitwise-different values of NaN, which broke some tests in `internal/fuzz`. Using an actual NaN value from the JavaScript runtime fixes the issue. Isolated example: https://jsfiddle.net/j3rtaz0o/1/. Note that Firefox doesn't seem to exhibit this issue.
1 parent 8cc14eb commit 5b30828

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

compiler/natives/src/math/math.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ var math = js.Global.Get("Math")
1111
var _zero float64 = 0
1212
var posInf = 1 / _zero
1313
var negInf = -1 / _zero
14-
var nan = 0 / _zero
14+
15+
// Usually, NaN can be obtained in JavaScript with `0 / 0` operation. However,
16+
// in V8, `0 / _zero` yields a bitwise-different value of NaN compared to the
17+
// default NaN or `0 / 0`. Unfortunately, Go compiler forbids division by zero,
18+
// so we have to get this value from prelude.
19+
var nan = js.Global.Get("$NaN").Float()
1520

1621
func Acos(x float64) float64 {
1722
return math.Call("acos", x).Float()

compiler/prelude/prelude.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Prelude = prelude + numeric + types + goroutines + jsmapping
77

88
const prelude = `Error.stackTraceLimit = Infinity;
99
10+
var $NaN = NaN;
1011
var $global, $module;
1112
if (typeof window !== "undefined") { /* web page */
1213
$global = window;

0 commit comments

Comments
 (0)