Skip to content

Commit 081d090

Browse files
committed
handle negative NaNs
1 parent b16b3be commit 081d090

File tree

5 files changed

+1027
-881
lines changed

5 files changed

+1027
-881
lines changed

std/assembly/math.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,9 @@ export namespace NativeMath {
994994

995995
@inline
996996
export function signbit(x: f64): bool {
997-
return reinterpret<u64>(x) >>> 63 != 0;
997+
// In ECMAScript all NaN values are indistinguishable from each other
998+
// so we need handle NaN and negative NaN in similar way
999+
return <bool>(<i32>(reinterpret<u64>(x) >>> 63 != 0) & <i32>(x == x));
9981000
}
9991001

10001002
export function sin(x: f64): f64 { // TODO
@@ -2053,7 +2055,7 @@ export namespace NativeMathf {
20532055

20542056
@inline
20552057
export function signbit(x: f32): bool {
2056-
return <bool>(reinterpret<u32>(x) >>> 31);
2058+
return <bool>((reinterpret<u32>(x) >>> 31) & <i32>(x == x));
20572059
}
20582060

20592061
export function sin(x: f32): f32 { // TODO

std/portable/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ globalScope["fmodf"] = function fmodf(x, y) {
227227

228228
globalScope["JSMath"] = Math;
229229
globalScope["JSMath"].signbit = function signbit(x) {
230-
F64[0] = x; return Boolean(U64[1] >>> 31);
230+
F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x));
231231
}
232232

233233
globalScope["memory"] = (() => {

0 commit comments

Comments
 (0)