From 33967994143b351f3908a9f9d859009f67e79762 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 24 Oct 2021 17:53:35 +0300 Subject: [PATCH 1/2] impove nearest js portable polyfill --- std/portable/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index d027d8d967..2fb9e1f5c5 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 TO_INT64 = 4503599627370496.0; + const y = Math.abs(value); + return y < TO_INT64 + ? Math.abs(y + TO_INT64 - TO_INT64) * Math.sign(value) + : value; }; globalScope["select"] = function select(ifTrue, ifFalse, condition) { From b3f460815fb6631dca1e893732b97392a995586f Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 24 Oct 2021 18:51:30 +0300 Subject: [PATCH 2/2] refactor --- std/portable/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index 2fb9e1f5c5..2400d61746 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -137,10 +137,10 @@ if (typeof globalScope.ASC_TARGET === "undefined") { globalScope["floor"] = Math.floor; globalScope["nearest"] = function nearest(value) { - const TO_INT64 = 4503599627370496.0; + const INV_EPS64 = 4503599627370496.0; const y = Math.abs(value); - return y < TO_INT64 - ? Math.abs(y + TO_INT64 - TO_INT64) * Math.sign(value) + return y < INV_EPS64 + ? Math.abs(y + INV_EPS64 - INV_EPS64) * Math.sign(value) : value; };