diff --git a/std/portable/index.js b/std/portable/index.js index d027d8d967..2400d61746 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -136,12 +136,12 @@ if (typeof globalScope.ASC_TARGET === "undefined") { globalScope["floor"] = Math.floor; - // Adopt code from https://github.com/rfk/wasm-polyfill globalScope["nearest"] = function nearest(value) { - if (Math.abs(value - Math.trunc(value)) === 0.5) { - return 2.0 * Math.round(value * 0.5); - } - return Math.round(value); + const INV_EPS64 = 4503599627370496.0; + const y = Math.abs(value); + return y < INV_EPS64 + ? Math.abs(y + INV_EPS64 - INV_EPS64) * Math.sign(value) + : value; }; globalScope["select"] = function select(ifTrue, ifFalse, condition) {