Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Decimal rounding
(version: 0)
Round a number to a specific
Comparing performance of:
String vs Arithmetic
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var DecimalPrecision = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } // Decimal round (half away from zero) this.round = function(num, digits = 0) { num = (num + 'e').split('e'); num = Math.sign(+num[0]) * Math.round(Math.abs(+num[0]) + 'e' + (+num[1] + digits)); num = (num + 'e').split('e'); return +(num[0] + 'e' + (+num[1] - digits)); }; // Decimal ceil this.ceil = function(num, digits = 0) { num = (num + 'e').split('e'); num = Math.ceil(num[0] + 'e' + (+num[1] + digits)); num = (num + 'e').split('e'); return +(num[0] + 'e' + (+num[1] - digits)); }; // Decimal floor this.floor = function(num, digits = 0) { num = (num + 'e').split('e'); num = Math.floor(num[0] + 'e' + (+num[1] + digits)); num = (num + 'e').split('e'); return +(num[0] + 'e' + (+num[1] - digits)); }; // Decimal trunc this.trunc = function(num, digits = 0) { num = (num + 'e').split('e'); num = Math.trunc(num[0] + 'e' + (+num[1] + digits)); num = (num + 'e').split('e'); return +(num[0] + 'e' + (+num[1] - digits)); }; return this; })(); 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); }; } this.isRound = function(num, digits = 0) { //return Number(num.toFixed(digits)) === num; return this.round(num, digits) === num; }; // Decimal round (half away from zero) this.round = function(num, digits = 0) { var p = Math.pow(10, digits); var e = 0.51 * Number.EPSILON * num; return Math.round((num + e) * p) / p; }; // Decimal ceil this.ceil = function(num, digits = 0) { if (this.isRound(num, digits)) return num; var p = Math.pow(10, digits); var e = 0.51 * Number.EPSILON * num; return Math.ceil((num + e) * p) / p; }; // Decimal floor this.floor = function(num, digits = 0) { if (this.isRound(num, digits)) return num; var p = Math.pow(10, digits); var e = 0.51 * Number.EPSILON * num; return Math.floor((num + e) * p) / p; }; // Decimal trunc this.trunc = function(num, digits = 0) { if (this.isRound(num, digits)) return num; var p = Math.pow(10, digits); var e = 0.51 * Number.EPSILON * num; return Math.trunc((num + e) * p) / p; }; return this; })();
Tests:
String
// test rounding of half DecimalPrecision.round(0.5); // 1 DecimalPrecision.round(-0.5); // -1 // testing very small numbers DecimalPrecision.ceil(1e-8, 2); // 0.01 DecimalPrecision.floor(1e-8, 2); // 0 // testing simple cases DecimalPrecision.round(5.12, 1); // 5.1 DecimalPrecision.round(-5.12, 1); // -5.1 DecimalPrecision.ceil(5.12, 1); // 5.2 DecimalPrecision.ceil(-5.12, 1); // -5.1 DecimalPrecision.floor(5.12, 1); // 5.1 DecimalPrecision.floor(-5.12, 1); // -5.2 DecimalPrecision.trunc(5.12, 1); // 5.1 DecimalPrecision.trunc(-5.12, 1); // -5.1 // testing edge cases for round DecimalPrecision.round(1.005, 2); // 1.01 DecimalPrecision.round(39.425, 2); // 39.43 DecimalPrecision.round(-1.005, 2); // -1.01 DecimalPrecision.round(-39.425, 2); // -39.43 // testing edge cases for ceil DecimalPrecision.ceil(9.130, 2); // 9.13 DecimalPrecision.ceil(65.180, 2); // 65.18 DecimalPrecision.ceil(-2.260, 2); // -2.26 DecimalPrecision.ceil(-18.150, 2); // -18.15 // testing edge cases for floor DecimalPrecision.floor(2.260, 2); // 2.26 DecimalPrecision.floor(18.150, 2); // 18.15 DecimalPrecision.floor(-9.130, 2); // -9.13 DecimalPrecision.floor(-65.180, 2); // -65.18 // testing edge cases for trunc DecimalPrecision.trunc(2.260, 2); // 2.26 DecimalPrecision.trunc(18.150, 2); // 18.15 DecimalPrecision.trunc(-2.260, 2); // -2.26 DecimalPrecision.trunc(-18.150, 2); // -18.15 // testing round to tens and hundreds DecimalPrecision.round(1262.48, -1); // 1260 DecimalPrecision.round(1262.48, -2); // 1300
Arithmetic
// test rounding of half DecimalPrecision2.round(0.5); // 1 DecimalPrecision2.round(-0.5); // -1 // testing very small numbers DecimalPrecision2.ceil(1e-8, 2); // 0.01 DecimalPrecision2.floor(1e-8, 2); // 0 // testing simple cases DecimalPrecision2.round(5.12, 1); // 5.1 DecimalPrecision2.round(-5.12, 1); // -5.1 DecimalPrecision2.ceil(5.12, 1); // 5.2 DecimalPrecision2.ceil(-5.12, 1); // -5.1 DecimalPrecision2.floor(5.12, 1); // 5.1 DecimalPrecision2.floor(-5.12, 1); // -5.2 DecimalPrecision2.trunc(5.12, 1); // 5.1 DecimalPrecision2.trunc(-5.12, 1); // -5.1 // testing edge cases for round DecimalPrecision2.round(1.005, 2); // 1.01 DecimalPrecision2.round(39.425, 2); // 39.43 DecimalPrecision2.round(-1.005, 2); // -1.01 DecimalPrecision2.round(-39.425, 2); // -39.43 // testing edge cases for ceil DecimalPrecision2.ceil(9.130, 2); // 9.13 DecimalPrecision2.ceil(65.180, 2); // 65.18 DecimalPrecision2.ceil(-2.260, 2); // -2.26 DecimalPrecision2.ceil(-18.150, 2); // -18.15 // testing edge cases for floor DecimalPrecision2.floor(2.260, 2); // 2.26 DecimalPrecision2.floor(18.150, 2); // 18.15 DecimalPrecision2.floor(-9.130, 2); // -9.13 DecimalPrecision2.floor(-65.180, 2); // -65.18 // testing edge cases for trunc DecimalPrecision2.trunc(2.260, 2); // 2.26 DecimalPrecision2.trunc(18.150, 2); // 18.15 DecimalPrecision2.trunc(-2.260, 2); // -2.26 DecimalPrecision2.trunc(-18.150, 2); // -18.15 // testing round to tens and hundreds DecimalPrecision2.round(1262.48, -1); // 1260 DecimalPrecision2.round(1262.48, -2); // 1300
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String
Arithmetic
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
A massive benchmark result! To summarize, we have two test results: **Arithmetic** 1. Browser: Chrome 98 2. Executions Per Second: 43135.40625 (very high performance) 3. Raw UA String: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36` 4. Device Platform: Desktop 5. Operating System: Windows **String** 1. Browser: Chrome 98 2. Executions Per Second: 42381.40234375 (still very high performance) 3. Raw UA String: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36` 4. Device Platform: Desktop 5. Operating System: Windows Both tests show excellent performance, with the Arithmetic test performing slightly better than the String test. However, to answer your implied question: **What is causing this high-performance result?** Without more context or information about the specific benchmarking tool, algorithm, and workload being used, it's difficult to pinpoint a single cause for this exceptional performance. However, here are some possible factors that could contribute to such high performance: 1. **Optimized JavaScript engine**: Chrome 98 is known for its improved JavaScript performance, which might be contributing to the high-executions-per-second numbers. 2. **Fast hardware**: The device platform (Desktop) and operating system (Windows) used by the benchmarking tool are likely fast hardware configurations, further enhancing performance. 3. **Efficient algorithm or testing framework**: The specific benchmarking tool and algorithm used for these tests might be highly optimized for performance, which is contributing to the exceptional results. Keep in mind that this is a general analysis, and without more information about the specific test setup, it's challenging to provide a more detailed explanation.
Related benchmarks:
decimal.js versus native precision
roundDecimal() with Math.pow vs. Unary and toPrecision()
toFixed vs mathjs round
Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding vs Double rounding v2
Comments
Confirm delete:
Do you really want to delete benchmark?