Skip to content

Commit 9ffab6a

Browse files
authored
Impove nearest operation portable polyfill (#2105)
1 parent a6d934a commit 9ffab6a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

std/portable/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ if (typeof globalScope.ASC_TARGET === "undefined") {
136136

137137
globalScope["floor"] = Math.floor;
138138

139-
// Adopt code from https://github.com/rfk/wasm-polyfill
140139
globalScope["nearest"] = function nearest(value) {
141-
if (Math.abs(value - Math.trunc(value)) === 0.5) {
142-
return 2.0 * Math.round(value * 0.5);
143-
}
144-
return Math.round(value);
140+
const INV_EPS64 = 4503599627370496.0;
141+
const y = Math.abs(value);
142+
return y < INV_EPS64
143+
? Math.abs(y + INV_EPS64 - INV_EPS64) * Math.sign(value)
144+
: value;
145145
};
146146

147147
globalScope["select"] = function select(ifTrue, ifFalse, condition) {

0 commit comments

Comments
 (0)