Last active
January 24, 2023 17:35
-
-
Save amrali-eg/31ec3a8b3d22bd840f8e6822e681a3ac to your computer and use it in GitHub Desktop.
Revisions
-
amrali-eg revised this gist
Jan 24, 2023 . 1 changed file with 208 additions and 227 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,233 +10,214 @@ var suite = new Benchmark.Suite; Benchmark.prototype.setup = function () { // Solution 1 var DecimalPrecision = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var shift = function(value, exponent) { value = (value + 'e').split('e'); return +(value[0] + 'e' + (+value[1] + (exponent || 0))); }; var n = shift(num, +decimalPlaces); return shift(Math[type](n), -decimalPlaces); }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); // Solution 2 var DecimalPrecision2 = (function() { if (Number.EPSILON === undefined) { Number.EPSILON = Math.pow(2, -52); } if (Math.sign === undefined) { Math.sign = function(x) { return ((x > 0) - (x < 0)) || +x; }; } return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces || 0); var n = (num * p) * (1 + Number.EPSILON); return Math.round(n) / p; }, // Decimal ceil ceil: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces || 0); var n = (num * p) * (1 - Math.sign(num) * Number.EPSILON); return Math.ceil(n) / p; }, // Decimal floor floor: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces || 0); var n = (num * p) * (1 + Math.sign(num) * Number.EPSILON); return Math.floor(n) / p; }, // Decimal trunc trunc: function(num, decimalPlaces) { return (num < 0 ? this.ceil : this.floor)(num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return this.round(num, decimalPlaces).toFixed(decimalPlaces); } }; })(); // Solution 3 var DecimalPrecision3 = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var powers = [ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 ]; var intpow10 = function(power) { /* Not in lookup table */ if (power < 0 || power > 22) { return Math.pow(10, power); } return powers[power]; }; // Eliminate binary floating-point inaccuracies. var stripError = function(num) { if (Number.isInteger(num)) return num; return parseFloat(num.toPrecision(15)); }; var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var p = intpow10(decimalPlaces || 0); var n = stripError(num * p); return Math[type](n) / p; }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); // Solution 4 var DecimalPrecision4 = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var powers = [ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 ]; var intpow10 = function(power) { /* Not in lookup table */ if (power < 0 || power > 22) { return Math.pow(10, power); } return powers[power]; }; var toPrecision = function(num, significantDigits) { // Return early for ±0, NaN and Infinity. if (!num || !Number.isFinite(num)) return num; // Compute shift of the decimal point (sf - leftSidedDigits). var shift = significantDigits - 1 - Math.floor(Math.log10(Math.abs(num))); // Return if rounding to the same or higher precision. var decimalPlaces = 0; for (var p = 1; num != Math.round(num * p) / p; p *= 10) decimalPlaces++; if (shift >= decimalPlaces) return num; // Round to "shift" fractional digits var scale = intpow10(Math.abs(shift)); return shift > 0 ? Math.round(num * scale) / scale : Math.round(num / scale) * scale; }; // Eliminate binary floating-point inaccuracies. var stripError = function(num) { if (Number.isInteger(num)) return num; return toPrecision(num, 15); }; var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var p = intpow10(decimalPlaces || 0); var n = stripError(num * p); return Math[type](n) / p; }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); }; -
amrali-eg revised this gist
Feb 7, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -193,7 +193,7 @@ var shift = significantDigits - 1 - Math.floor(Math.log10(Math.abs(num))); // Return if rounding to the same or higher precision. var decimalPlaces = 0; for (var p = 1; num != Math.round(num * p) / p; p *= 10) decimalPlaces++; if (shift >= decimalPlaces) return num; // Round to "shift" fractional digits -
amrali-eg revised this gist
Jan 30, 2022 . 1 changed file with 42 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,11 +12,6 @@ Benchmark.prototype.setup = function () { // Solution 1 var DecimalPrecision = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); @@ -66,16 +61,28 @@ return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var powers = [ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 ]; var intpow10 = function(power) { /* Not in lookup table */ if (power < 0 || power > 22) { return Math.pow(10, power); } return powers[power]; }; var isRound = function(num, decimalPlaces) { //return decimalPlaces >= 0 && // +num.toFixed(decimalPlaces) === num; var p = intpow10(decimalPlaces); return Math.round(num * p) / p === num; }; var decimalAdjust = function(type, num, decimalPlaces) { if (type !== 'round' && isRound(num, decimalPlaces || 0)) return num; var p = intpow10(decimalPlaces || 0); var n = (num * p) * (1 + Number.EPSILON); return Math[type](n) / p; }; @@ -105,16 +112,23 @@ // Solution 3 var DecimalPrecision3 = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var powers = [ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 ]; var intpow10 = function(power) { /* Not in lookup table */ if (power < 0 || power > 22) { return Math.pow(10, power); } return powers[power]; }; // Eliminate binary floating-point inaccuracies. var stripError = function(num) { if (Number.isInteger(num)) @@ -124,7 +138,7 @@ var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var p = intpow10(decimalPlaces || 0); var n = stripError(num * p); return Math[type](n) / p; }; @@ -154,16 +168,23 @@ // Solution 4 var DecimalPrecision4 = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var powers = [ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 ]; var intpow10 = function(power) { /* Not in lookup table */ if (power < 0 || power > 22) { return Math.pow(10, power); } return powers[power]; }; var toPrecision = function(num, significantDigits) { // Return early for ±0, NaN and Infinity. if (!num || !Number.isFinite(num)) @@ -176,7 +197,7 @@ if (shift >= decimalPlaces) return num; // Round to "shift" fractional digits var scale = intpow10(Math.abs(shift)); return shift > 0 ? Math.round(num * scale) / scale : Math.round(num / scale) * scale; @@ -190,7 +211,7 @@ var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var p = intpow10(decimalPlaces || 0); var n = stripError(num * p); return Math[type](n) / p; }; -
amrali-eg revised this gist
Jun 2, 2021 . 1 changed file with 19 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,15 +22,15 @@ return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var shift = function(value, exponent) { value = (value + 'e').split('e'); return +(value[0] + 'e' + (+value[1] + (exponent || 0))); }; var n = shift(num, +decimalPlaces); return shift(Math[type](n), -decimalPlaces); }; return { // Decimal round (half away from zero) @@ -76,8 +76,8 @@ if (isRound(num, decimalPlaces || 0)) return num; var p = Math.pow(10, decimalPlaces || 0); var n = (num * p) * (1 + Number.EPSILON); return Math[type](n) / p; }; return { // Decimal round (half away from zero) @@ -121,12 +121,12 @@ return num; return parseFloat(num.toPrecision(15)); }; var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var p = Math.pow(10, decimalPlaces || 0); var n = stripError(num * p); return Math[type](n) / p; }; return { // Decimal round (half away from zero) @@ -168,14 +168,14 @@ // Return early for ±0, NaN and Infinity. if (!num || !Number.isFinite(num)) return num; // Compute shift of the decimal point (sf - leftSidedDigits). var shift = significantDigits - 1 - Math.floor(Math.log10(Math.abs(num))); // Return if rounding to the same or higher precision. var decimalPlaces = 0; for (var p = 1; !Number.isInteger(num * p); p *= 10) decimalPlaces++; if (shift >= decimalPlaces) return num; // Round to "shift" fractional digits var scale = Math.pow(10, Math.abs(shift)); return shift > 0 ? Math.round(num * scale) / scale : @@ -187,12 +187,12 @@ return num; return toPrecision(num, 15); }; var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var p = Math.pow(10, decimalPlaces || 0); var n = stripError(num * p); return Math[type](n) / p; }; return { // Decimal round (half away from zero) -
amrali-eg revised this gist
Oct 19, 2020 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ return +(value[0] + 'e' + (+value[1] + (exponent || 0))); }; var n = type === 'round' ? Math.abs(num) : num; var m = shift(n, +decimalPlaces); var r = shift(Math[type](m), -decimalPlaces); return type === 'round' ? Math.sign(num) * r : r; }; @@ -124,7 +124,7 @@ var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var m = stripError(n * p); var r = Math[type](m) / p; return type === 'round' ? Math.sign(num) * r : r; }; @@ -190,7 +190,7 @@ var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var m = stripError(n * p); var r = Math[type](m) / p; return type === 'round' ? Math.sign(num) * r : r; }; -
amrali-eg revised this gist
Sep 18, 2020 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -168,12 +168,18 @@ // Return early for ±0, NaN and Infinity. if (!num || !Number.isFinite(num)) return num; // Compute shift of the decimal point. var shift = significantDigits - 1 - Math.floor(Math.log10(Math.abs(num))); // Return if rounding to the same or higher precision. var decimalPlaces = 0; for (var p = 1; !Number.isInteger(num * p); p *= 10) decimalPlaces++; if (shift >= decimalPlaces) return num; // Round to sf-1 fractional digits of normalized mantissa x.dddd var scale = Math.pow(10, Math.abs(shift)); return shift > 0 ? Math.round(num * scale) / scale : Math.round(num / scale) * scale; }; // Eliminate binary floating-point inaccuracies. var stripError = function(num) { -
amrali-eg revised this gist
Sep 14, 2020 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -170,9 +170,10 @@ return num; // Compute the base 10 exponent (signed). var e = Math.floor(Math.log10(Math.abs(num))); var d = significantDigits - 1 - e; var p = Math.pow(10, Math.abs(d)); // Round to sf-1 fractional digits of normalized mantissa x.dddd return d > 0 ? Math.round(num * p) / p : Math.round(num / p) * p; }; // Eliminate binary floating-point inaccuracies. var stripError = function(num) { -
amrali-eg revised this gist
Sep 14, 2020 . No changes.There are no files selected for viewing
-
amrali-eg revised this gist
Sep 14, 2020 . 1 changed file with 8 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,8 @@ return +(value[0] + 'e' + (+value[1] + (exponent || 0))); }; var n = type === 'round' ? Math.abs(num) : num; var m = shift(n, +decimalPlaces) var r = shift(Math[type](m), -decimalPlaces); return type === 'round' ? Math.sign(num) * r : r; }; return { @@ -75,8 +76,8 @@ if (isRound(num, decimalPlaces || 0)) return num; var p = Math.pow(10, decimalPlaces || 0); var m = (num * p) * (1 + Number.EPSILON); return Math[type](m) / p; }; return { // Decimal round (half away from zero) @@ -123,7 +124,8 @@ var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var m = stripError(n * p) var r = Math[type](m) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { @@ -181,7 +183,8 @@ var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var m = stripError(n * p) var r = Math[type](m) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { -
amrali-eg revised this gist
Sep 12, 2020 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -115,15 +115,15 @@ }; } // Eliminate binary floating-point inaccuracies. var stripError = function(num) { if (Number.isInteger(num)) return num; return parseFloat(num.toPrecision(15)); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var r = Math[type](stripError(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { @@ -173,15 +173,15 @@ return Math.round(num * p) / p; }; // Eliminate binary floating-point inaccuracies. var stripError = function(num) { if (Number.isInteger(num)) return num; return toPrecision(num, 15); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var r = Math[type](stripError(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { -
amrali-eg revised this gist
Sep 11, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,7 @@ var decimalAdjust = function(type, num, decimalPlaces) { var shift = function(value, exponent) { value = (value + 'e').split('e'); return +(value[0] + 'e' + (+value[1] + (exponent || 0))); }; var n = type === 'round' ? Math.abs(num) : num; var r = shift(Math[type](shift(n, +decimalPlaces)), -decimalPlaces); -
amrali-eg revised this gist
Sep 11, 2020 . 1 changed file with 9 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -72,9 +72,9 @@ return Math.round(num * p) / p === num; }; var decimalAdjust = function(type, num, decimalPlaces) { if (isRound(num, decimalPlaces || 0)) return num; var p = Math.pow(10, decimalPlaces || 0); var e = Number.EPSILON * num * p; return Math[type]((num * p) + e) / p; }; @@ -116,11 +116,13 @@ } // Eliminate binary floating-point inaccuracies. var toDecimal = function(num) { if (Number.isInteger(num)) return num; return parseFloat(num.toPrecision(15)); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var r = Math[type](toDecimal(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; @@ -169,14 +171,16 @@ var p = Math.pow(10, significantDigits - 1 - e); // Round to n-1 fractional digits of normalized mantissa x.dddd return Math.round(num * p) / p; }; // Eliminate binary floating-point inaccuracies. var toDecimal = function(num) { if (Number.isInteger(num)) return num; return toPrecision(num, 15); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces || 0); var r = Math[type](toDecimal(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; -
amrali-eg revised this gist
Sep 10, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -114,7 +114,7 @@ return v < 0 ? Math.ceil(v) : Math.floor(v); }; } // Eliminate binary floating-point inaccuracies. var toDecimal = function(num) { return parseFloat(num.toPrecision(15)); }; @@ -170,7 +170,7 @@ // Round to n-1 fractional digits of normalized mantissa x.dddd return Math.round(num * p) / p; }; // Eliminate binary floating-point inaccuracies. var toDecimal = function(num) { return toPrecision(num, 15); }; -
amrali-eg revised this gist
Sep 9, 2020 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -115,13 +115,13 @@ }; } // Eliminate floating-point inaccuracies. var toDecimal = function(num) { return parseFloat(num.toPrecision(15)); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces); var r = Math[type](toDecimal(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { @@ -171,13 +171,13 @@ return Math.round(num * p) / p; }; // Eliminate floating-point inaccuracies. var toDecimal = function(num) { return toPrecision(num, 15); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces); var r = Math[type](toDecimal(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { -
amrali-eg revised this gist
Sep 8, 2020 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -164,10 +164,12 @@ // Return early for ±0, NaN and Infinity. if (!num || !Number.isFinite(num)) return num; // Compute the base 10 exponent (signed). var e = Math.floor(Math.log10(Math.abs(num))); var p = Math.pow(10, significantDigits - 1 - e); // Round to n-1 fractional digits of normalized mantissa x.dddd return Math.round(num * p) / p; }; // Eliminate floating-point inaccuracies. var strip = function(num) { return toPrecision(num, 15); -
amrali-eg revised this gist
Sep 7, 2020 . 1 changed file with 7 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,14 +23,13 @@ }; } var decimalAdjust = function(type, num, decimalPlaces) { var shift = function(value, exponent) { value = (value + 'e').split('e'); return +(value[0] + 'e' + (+value[1] + exponent)); }; var n = type === 'round' ? Math.abs(num) : num; var r = shift(Math[type](shift(n, +decimalPlaces)), -decimalPlaces); return type === 'round' ? Math.sign(num) * r : r; }; return { // Decimal round (half away from zero) -
amrali-eg revised this gist
Sep 6, 2020 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -48,6 +48,10 @@ // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); @@ -91,6 +95,10 @@ // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); @@ -133,6 +141,10 @@ // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); @@ -183,6 +195,10 @@ // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); -
amrali-eg revised this gist
Sep 4, 2020 . No changes.There are no files selected for viewing
-
amrali-eg revised this gist
Sep 4, 2020 . No changes.There are no files selected for viewing
-
amrali-eg revised this gist
Sep 4, 2020 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -150,6 +150,9 @@ }; } var toPrecision = function(num, significantDigits) { // Return early for ±0, NaN and Infinity. if (!num || !Number.isFinite(num)) return num; var e = Math.floor(Math.log10(Math.abs(num))); var p = Math.pow(10, significantDigits - e - 1); return Math.round(num * p) / p; -
amrali-eg revised this gist
Sep 4, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -63,6 +63,8 @@ }; } var isRound = function(num, decimalPlaces) { //return decimalPlaces >= 0 && // +num.toFixed(decimalPlaces) === num; var p = Math.pow(10, decimalPlaces); return Math.round(num * p) / p === num; }; -
amrali-eg revised this gist
Sep 4, 2020 . 1 changed file with 49 additions and 54 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,34 +22,32 @@ return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var decimalAdjust = function(type, num, decimalPlaces) { var value = type === 'round' ? Math.abs(num) : num; // Shift value = (value + 'e').split('e'); value = Math[type](value[0] + 'e' + (+value[1] + decimalPlaces)); // Shift back value = (value + 'e').split('e'); value = +(value[0] + 'e' + (+value[1] - decimalPlaces)); return type === 'round' ? Math.sign(num) * value : value; }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); } }; })(); @@ -64,38 +62,33 @@ return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var isRound = function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.round(num * p) / p === num; }; var decimalAdjust = function(type, num, decimalPlaces) { if (isRound(num, decimalPlaces)) return num; var p = Math.pow(10, decimalPlaces); var e = Number.EPSILON * num * p; return Math[type]((num * p) + e) / p; }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); } }; })(); @@ -116,27 +109,28 @@ var strip = function(num) { return parseFloat(num.toPrecision(15)); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces); var r = Math[type](strip(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); } }; })(); @@ -162,27 +156,28 @@ var strip = function(num) { return toPrecision(num, 15); }; var decimalAdjust = function(type, num, decimalPlaces) { var n = type === 'round' ? Math.abs(num) : num; var p = Math.pow(10, decimalPlaces); var r = Math[type](strip(n * p)) / p; return type === 'round' ? Math.sign(num) * r : r; }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); } }; })(); -
amrali-eg revised this gist
Sep 3, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -120,7 +120,7 @@ // Decimal round (half away from zero) round: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); var r = Math.round(strip(Math.abs(num * p))) / p; return Math.sign(num) * r; }, // Decimal ceil @@ -166,7 +166,7 @@ // Decimal round (half away from zero) round: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); var r = Math.round(strip(Math.abs(num * p))) / p; return Math.sign(num) * r; }, // Decimal ceil -
amrali-eg revised this gist
Sep 3, 2020 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -114,7 +114,7 @@ } // Eliminate floating-point inaccuracies. var strip = function(num) { return parseFloat(num.toPrecision(15)); }; return { // Decimal round (half away from zero) @@ -223,8 +223,8 @@ DecimalPrecision.round(1262.48, -2); }); suite.add("Solution 2 (Number.EPSILON)", function () { /** Solution 2 (Number.EPSILON) **/ DecimalPrecision2.round(0.5, 0); DecimalPrecision2.round(-0.5, 0); DecimalPrecision2.ceil(1e-8, 2); -
amrali-eg revised this gist
Sep 3, 2020 . 1 changed file with 23 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -143,47 +143,46 @@ // Solution 4 var DecimalPrecision4 = (function() { if (Math.sign === undefined) { Math.sign = function(x) { return ((x > 0) - (x < 0)) || +x; }; } if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var toPrecision = function(num, significantDigits) { var e = Math.floor(Math.log10(Math.abs(num))); var p = Math.pow(10, significantDigits - e - 1); return Math.round(num * p) / p; }; // Eliminate floating-point inaccuracies. var strip = function(num) { return toPrecision(num, 15); }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); var r = Math.round(strip(Math.abs(num) * p)) / p; return Math.sign(num) * r; }, // Decimal ceil ceil: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.ceil(strip(num * p)) / p; }, // Decimal floor floor: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.floor(strip(num * p)) / p; }, // Decimal trunc trunc: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.trunc(strip(num * p)) / p; } }; })(); -
amrali-eg revised this gist
Aug 31, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ // Decimal round (half away from zero) round: function(num, decimalPlaces) { num = (num + 'e').split('e'); num = Math.round(Math.abs(+num[0]) + 'e' + (+num[1] + decimalPlaces)) * Math.sign(+num[0]); num = (num + 'e').split('e'); return +(num[0] + 'e' + (+num[1] - decimalPlaces)); }, -
amrali-eg revised this gist
Aug 31, 2020 . 1 changed file with 42 additions and 42 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -102,6 +102,47 @@ // Solution 3 var DecimalPrecision3 = (function() { if (Math.sign === undefined) { Math.sign = function(x) { return ((x > 0) - (x < 0)) || +x; }; } if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } // Eliminate floating-point inaccuracies. var strip = function(num) { return parseFloat((num).toPrecision(15)); }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); var r = Math.round(strip(Math.abs(num) * p)) / p; return Math.sign(num) * r; }, // Decimal ceil ceil: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.ceil(strip(num * p)) / p; }, // Decimal floor floor: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.floor(strip(num * p)) / p; }, // Decimal trunc trunc: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.trunc(strip(num * p)) / p; } }; })(); // Solution 4 var DecimalPrecision4 = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); @@ -146,47 +187,6 @@ } }; })(); }; @@ -292,7 +292,7 @@ DecimalPrecision3.round(1262.48, -2); }); suite.add("Solution 4 (Double rounding v2)", function () { /** Solution 4 (Double rounding v2) **/ DecimalPrecision4.round(0.5, 0); DecimalPrecision4.round(-0.5, 0); -
amrali-eg revised this gist
Aug 31, 2020 . 2 changed files with 77 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ <html> <head> <meta charset="utf-8"/> <title>Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding vs Double rounding v2 #jsbench</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> <script src="./suite.js"></script> </head> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -146,6 +146,47 @@ } }; })(); // Solution 4 var DecimalPrecision4 = (function() { if (Math.sign === undefined) { Math.sign = function(x) { return ((x > 0) - (x < 0)) || +x; }; } if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } // Eliminate floating-point inaccuracies. var strip = function(num) { return parseFloat((num).toPrecision(15)); }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); var r = Math.round(strip(Math.abs(num) * p)) / p; return Math.sign(num) * r; }, // Decimal ceil ceil: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.ceil(strip(num * p)) / p; }, // Decimal floor floor: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.floor(strip(num * p)) / p; }, // Decimal trunc trunc: function(num, decimalPlaces) { var p = Math.pow(10, decimalPlaces); return Math.trunc(strip(num * p)) / p; } }; })(); }; @@ -251,6 +292,40 @@ DecimalPrecision3.round(1262.48, -2); }); suite.add("Solution 4 (Double rounding v2) **/", function () { /** Solution 4 (Double rounding v2) **/ DecimalPrecision4.round(0.5, 0); DecimalPrecision4.round(-0.5, 0); DecimalPrecision4.ceil(1e-8, 2); DecimalPrecision4.floor(1e-8, 2); DecimalPrecision4.round(5.12, 1); DecimalPrecision4.round(-5.12, 1); DecimalPrecision4.ceil(5.12, 1); DecimalPrecision4.ceil(-5.12, 1); DecimalPrecision4.floor(5.12, 1); DecimalPrecision4.floor(-5.12, 1); DecimalPrecision4.trunc(5.12, 1); DecimalPrecision4.trunc(-5.12, 1); DecimalPrecision4.round(1.005, 2); DecimalPrecision4.round(39.425, 2); DecimalPrecision4.round(-1.005, 2); DecimalPrecision4.round(-39.425, 2); DecimalPrecision4.ceil(9.130, 2); DecimalPrecision4.ceil(65.180, 2); DecimalPrecision4.ceil(-2.260, 2); DecimalPrecision4.ceil(-18.150, 2); DecimalPrecision4.floor(2.260, 2); DecimalPrecision4.floor(18.150, 2); DecimalPrecision4.floor(-9.130, 2); DecimalPrecision4.floor(-65.180, 2); DecimalPrecision4.trunc(2.260, 2); DecimalPrecision4.trunc(18.150, 2); DecimalPrecision4.trunc(-2.260, 2); DecimalPrecision4.trunc(-18.150, 2); DecimalPrecision4.round(1262.48, -1); DecimalPrecision4.round(1262.48, -2); }); suite.on("cycle", function (evt) { console.log(" - " + evt.target); }); @@ -267,7 +342,7 @@ }); }); console.log("Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding vs Double rounding v2 #jsbench"); console.log(new Array(30).join("-")); suite.run(); }); -
amrali-eg revised this gist
Aug 26, 2020 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ <html> <head> <meta charset="utf-8"/> <title>Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding #jsbench</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> <script src="./suite.js"></script> </head> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -267,7 +267,7 @@ }); }); console.log("Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding #jsbench"); console.log(new Array(30).join("-")); suite.run(); }); -
amrali-eg revised this gist
Aug 26, 2020 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ <html> <head> <meta charset="utf-8"/> <title>Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding #jsbench #jsperf</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> <script src="./suite.js"></script> </head> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -267,7 +267,7 @@ }); }); console.log("Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding #jsbench #jsperf"); console.log(new Array(30).join("-")); suite.run(); });
NewerOlder