Skip to content

Instantly share code, notes, and snippets.

@amrali-eg
Last active January 24, 2023 17:35
Show Gist options
  • Select an option

  • Save amrali-eg/31ec3a8b3d22bd840f8e6822e681a3ac to your computer and use it in GitHub Desktop.

Select an option

Save amrali-eg/31ec3a8b3d22bd840f8e6822e681a3ac to your computer and use it in GitHub Desktop.

Revisions

  1. amrali-eg revised this gist Jan 24, 2023. 1 changed file with 208 additions and 227 deletions.
    435 changes: 208 additions & 227 deletions suite.js
    Original 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.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 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;
    };
    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 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);
    }
    };
    // 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);
    }
    };
    })();
    };

  2. amrali-eg revised this gist Feb 7, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion suite.js
    Original 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; !Number.isInteger(num * p); p *= 10) decimalPlaces++;
    for (var p = 1; num != Math.round(num * p) / p; p *= 10) decimalPlaces++;
    if (shift >= decimalPlaces)
    return num;
    // Round to "shift" fractional digits
  3. amrali-eg revised this gist Jan 30, 2022. 1 changed file with 42 additions and 21 deletions.
    63 changes: 42 additions & 21 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -12,11 +12,6 @@
    Benchmark.prototype.setup = function () {
    // Solution 1
    var DecimalPrecision = (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);
    @@ -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 = Math.pow(10, decimalPlaces);
    var p = intpow10(decimalPlaces);
    return Math.round(num * p) / p === num;
    };
    var decimalAdjust = function(type, num, decimalPlaces) {
    if (isRound(num, decimalPlaces || 0))
    if (type !== 'round' && isRound(num, decimalPlaces || 0))
    return num;
    var p = Math.pow(10, decimalPlaces || 0);
    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.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 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 = Math.pow(10, decimalPlaces || 0);
    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.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 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 = Math.pow(10, Math.abs(shift));
    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 = Math.pow(10, decimalPlaces || 0);
    var p = intpow10(decimalPlaces || 0);
    var n = stripError(num * p);
    return Math[type](n) / p;
    };
  4. amrali-eg revised this gist Jun 2, 2021. 1 changed file with 19 additions and 19 deletions.
    38 changes: 19 additions & 19 deletions suite.js
    Original 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(type, num, decimalPlaces) {
    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 = 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;
    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 m = (num * p) * (1 + Number.EPSILON);
    return Math[type](m) / p;
    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(type, num, decimalPlaces) {
    var n = type === 'round' ? Math.abs(num) : num;
    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 m = stripError(n * p);
    var r = Math[type](m) / p;
    return type === 'round' ? Math.sign(num) * r : r;
    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.
    // 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 sf-1 fractional digits of normalized mantissa x.dddd
    // 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(type, num, decimalPlaces) {
    var n = type === 'round' ? Math.abs(num) : num;
    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 m = stripError(n * p);
    var r = Math[type](m) / p;
    return type === 'round' ? Math.sign(num) * r : r;
    var n = stripError(num * p);
    return Math[type](n) / p;
    };
    return {
    // Decimal round (half away from zero)
  5. amrali-eg revised this gist Oct 19, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions suite.js
    Original 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 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 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 m = stripError(n * p);
    var r = Math[type](m) / p;
    return type === 'round' ? Math.sign(num) * r : r;
    };
  6. amrali-eg revised this gist Sep 18, 2020. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions suite.js
    Original 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 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));
    // 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
    return d > 0 ? Math.round(num * p) / p : Math.round(num / p) * p;
    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) {
  7. amrali-eg revised this gist Sep 14, 2020. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions suite.js
    Original 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 p = Math.pow(10, significantDigits - 1 - e);
    // Round to n-1 fractional digits of normalized mantissa x.dddd
    return Math.round(num * p) / p;
    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) {
  8. amrali-eg revised this gist Sep 14, 2020. No changes.
  9. amrali-eg revised this gist Sep 14, 2020. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions suite.js
    Original 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 r = shift(Math[type](shift(n, +decimalPlaces)), -decimalPlaces);
    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 e = Number.EPSILON * num * p;
    return Math[type]((num * p) + e) / p;
    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 r = Math[type](stripError(n * p)) / p;
    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 r = Math[type](stripError(n * p)) / p;
    var m = stripError(n * p)
    var r = Math[type](m) / p;
    return type === 'round' ? Math.sign(num) * r : r;
    };
    return {
  10. amrali-eg revised this gist Sep 12, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -115,15 +115,15 @@
    };
    }
    // Eliminate binary floating-point inaccuracies.
    var toDecimal = function(num) {
    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](toDecimal(n * p)) / p;
    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 toDecimal = function(num) {
    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](toDecimal(n * p)) / p;
    var r = Math[type](stripError(n * p)) / p;
    return type === 'round' ? Math.sign(num) * r : r;
    };
    return {
  11. amrali-eg revised this gist Sep 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion suite.js
    Original 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));
    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);
  12. amrali-eg revised this gist Sep 11, 2020. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions suite.js
    Original 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))
    if (isRound(num, decimalPlaces || 0))
    return num;
    var p = Math.pow(10, decimalPlaces);
    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);
    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);
    var p = Math.pow(10, decimalPlaces || 0);
    var r = Math[type](toDecimal(n * p)) / p;
    return type === 'round' ? Math.sign(num) * r : r;
    };
  13. amrali-eg revised this gist Sep 10, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -114,7 +114,7 @@
    return v < 0 ? Math.ceil(v) : Math.floor(v);
    };
    }
    // Eliminate floating-point inaccuracies.
    // 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 floating-point inaccuracies.
    // Eliminate binary floating-point inaccuracies.
    var toDecimal = function(num) {
    return toPrecision(num, 15);
    };
  14. amrali-eg revised this gist Sep 9, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -115,13 +115,13 @@
    };
    }
    // Eliminate floating-point inaccuracies.
    var strip = function(num) {
    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](strip(n * p)) / p;
    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 strip = function(num) {
    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](strip(n * p)) / p;
    var r = Math[type](toDecimal(n * p)) / p;
    return type === 'round' ? Math.sign(num) * r : r;
    };
    return {
  15. amrali-eg revised this gist Sep 8, 2020. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions suite.js
    Original 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 - e - 1);
    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);
  16. amrali-eg revised this gist Sep 7, 2020. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -23,14 +23,13 @@
    };
    }
    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;
    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)
  17. amrali-eg revised this gist Sep 6, 2020. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions suite.js
    Original 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);
    }
    };
    })();
  18. amrali-eg revised this gist Sep 4, 2020. No changes.
  19. amrali-eg revised this gist Sep 4, 2020. No changes.
  20. amrali-eg revised this gist Sep 4, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions suite.js
    Original 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;
  21. amrali-eg revised this gist Sep 4, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions suite.js
    Original 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;
    };
  22. amrali-eg revised this gist Sep 4, 2020. 1 changed file with 49 additions and 54 deletions.
    103 changes: 49 additions & 54 deletions suite.js
    Original 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) {
    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));
    return decimalAdjust('round', num, decimalPlaces);
    },
    // Decimal ceil
    ceil: function(num, decimalPlaces) {
    num = (num + 'e').split('e');
    num = Math.ceil(num[0] + 'e' + (+num[1] + decimalPlaces));
    num = (num + 'e').split('e');
    return +(num[0] + 'e' + (+num[1] - decimalPlaces));
    return decimalAdjust('ceil', num, decimalPlaces);
    },
    // Decimal floor
    floor: function(num, decimalPlaces) {
    num = (num + 'e').split('e');
    num = Math.floor(num[0] + 'e' + (+num[1] + decimalPlaces));
    num = (num + 'e').split('e');
    return +(num[0] + 'e' + (+num[1] - decimalPlaces));
    return decimalAdjust('floor', num, decimalPlaces);
    },
    // Decimal trunc
    trunc: function(num, decimalPlaces) {
    num = (num + 'e').split('e');
    num = Math.trunc(num[0] + 'e' + (+num[1] + decimalPlaces));
    num = (num + 'e').split('e');
    return +(num[0] + 'e' + (+num[1] - 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 {
    isRound: function(num, decimalPlaces) {
    //return this.round(num, decimalPlaces) === num;
    return decimalPlaces >= 0 &&
    +num.toFixed(decimalPlaces) === num;
    },
    // Decimal round (half away from zero)
    round: function(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    var e = Number.EPSILON * num * p;
    return Math.round((num * p) + e) / p;
    return decimalAdjust('round', num, decimalPlaces);
    },
    // Decimal ceil
    ceil: function(num, decimalPlaces) {
    if (this.isRound(num, decimalPlaces))
    return num;
    var p = Math.pow(10, decimalPlaces);
    return Math.ceil(num * p) / p;
    return decimalAdjust('ceil', num, decimalPlaces);
    },
    // Decimal floor
    floor: function(num, decimalPlaces) {
    if (this.isRound(num, decimalPlaces))
    return num;
    var p = Math.pow(10, decimalPlaces);
    return Math.floor(num * p) / p;
    return decimalAdjust('floor', num, decimalPlaces);
    },
    // Decimal trunc
    trunc: function(num, decimalPlaces) {
    if (this.isRound(num, decimalPlaces))
    return num;
    var p = Math.pow(10, decimalPlaces);
    return Math.trunc(num * p) / p;
    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) {
    var p = Math.pow(10, decimalPlaces);
    var r = Math.round(strip(Math.abs(num * p))) / p;
    return Math.sign(num) * r;
    return decimalAdjust('round', num, decimalPlaces);
    },
    // Decimal ceil
    ceil: function(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    return Math.ceil(strip(num * p)) / p;
    return decimalAdjust('ceil', num, decimalPlaces);
    },
    // Decimal floor
    floor: function(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    return Math.floor(strip(num * p)) / p;
    return decimalAdjust('floor', num, decimalPlaces);
    },
    // Decimal trunc
    trunc: function(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    return Math.trunc(strip(num * p)) / p;
    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) {
    var p = Math.pow(10, decimalPlaces);
    var r = Math.round(strip(Math.abs(num * p))) / p;
    return Math.sign(num) * r;
    return decimalAdjust('round', num, decimalPlaces);
    },
    // Decimal ceil
    ceil: function(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    return Math.ceil(strip(num * p)) / p;
    return decimalAdjust('ceil', num, decimalPlaces);
    },
    // Decimal floor
    floor: function(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    return Math.floor(strip(num * p)) / p;
    return decimalAdjust('floor', num, decimalPlaces);
    },
    // Decimal trunc
    trunc: function(num, decimalPlaces) {
    var p = Math.pow(10, decimalPlaces);
    return Math.trunc(strip(num * p)) / p;
    return decimalAdjust('trunc', num, decimalPlaces);
    }
    };
    })();
  23. amrali-eg revised this gist Sep 3, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions suite.js
    Original 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;
    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;
    var r = Math.round(strip(Math.abs(num * p))) / p;
    return Math.sign(num) * r;
    },
    // Decimal ceil
  24. amrali-eg revised this gist Sep 3, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions suite.js
    Original 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 parseFloat(num.toPrecision(15));
    };
    return {
    // Decimal round (half away from zero)
    @@ -223,8 +223,8 @@
    DecimalPrecision.round(1262.48, -2);
    });

    suite.add("Solution 2 (Epsilon correction)", function () {
    /** Solution 2 (Epsilon correction) **/
    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);
  25. amrali-eg revised this gist Sep 3, 2020. 1 changed file with 23 additions and 24 deletions.
    47 changes: 23 additions & 24 deletions suite.js
    Original 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 d = decimalPlaces || 0;
    var m = Math.pow(10, d);
    var n = +(d ? num * m : num).toFixed(8);
    var i = Math.floor(n), f = n - i;
    var e = 1e-8; // Allow for rounding errors in f
    var r = (f > 0.5 - e && f < 0.5 + e) ?
    ((n < 0) ? i : i + 1) :
    Math.round(n);
    return d ? r / m : r;
    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 d = decimalPlaces || 0;
    var m = Math.pow(10, d);
    var n = +(d ? num * m : num).toFixed(8);
    var i = Math.ceil(n);
    return d ? i / m : i;
    var p = Math.pow(10, decimalPlaces);
    return Math.ceil(strip(num * p)) / p;
    },
    // Decimal floor
    floor: function(num, decimalPlaces) {
    var d = decimalPlaces || 0;
    var m = Math.pow(10, d);
    var n = +(d ? num * m : num).toFixed(8);
    var i = Math.floor(n);
    return d ? i / m : i;
    var p = Math.pow(10, decimalPlaces);
    return Math.floor(strip(num * p)) / p;
    },
    // Decimal trunc
    trunc: function(num, decimalPlaces) {
    var d = decimalPlaces || 0;
    var m = Math.pow(10, d);
    var n = +(d ? num * m : num).toFixed(8);
    var i = Math.trunc(n);
    return d ? i / m : i;
    var p = Math.pow(10, decimalPlaces);
    return Math.trunc(strip(num * p)) / p;
    }
    };
    })();
  26. amrali-eg revised this gist Aug 31, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion suite.js
    Original 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.sign(+num[0]) * Math.round(Math.abs(+num[0]) + 'e' + (+num[1] + decimalPlaces));
    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));
    },
  27. amrali-eg revised this gist Aug 31, 2020. 1 changed file with 42 additions and 42 deletions.
    84 changes: 42 additions & 42 deletions suite.js
    Original 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 @@
    }
    };
    })();

    // 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;
    }
    };
    })();
    };


    @@ -292,7 +292,7 @@
    DecimalPrecision3.round(1262.48, -2);
    });

    suite.add("Solution 4 (Double rounding v2) **/", function () {
    suite.add("Solution 4 (Double rounding v2)", function () {
    /** Solution 4 (Double rounding v2) **/
    DecimalPrecision4.round(0.5, 0);
    DecimalPrecision4.round(-0.5, 0);
  28. amrali-eg revised this gist Aug 31, 2020. 2 changed files with 77 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion index.html
    Original 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>
    <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>
    77 changes: 76 additions & 1 deletion suite.js
    Original 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 #jsbench");
    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();
    });
  29. amrali-eg revised this gist Aug 26, 2020. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion index.html
    Original 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>
    <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>
    2 changes: 1 addition & 1 deletion suite.js
    Original 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("Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding #jsbench");
    console.log(new Array(30).join("-"));
    suite.run();
    });
  30. amrali-eg revised this gist Aug 26, 2020. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion index.html
    Original 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>
    <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>
    2 changes: 1 addition & 1 deletion suite.js
    Original 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("Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding #jsbench #jsperf");
    console.log(new Array(30).join("-"));
    suite.run();
    });