Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 93c4235

Browse files
committed
New log10 floor method for more precise approach
Fixes #62
1 parent e285cec commit 93c4235

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Intl.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ function ToRawPrecision (x, minPrecision, maxPrecision) {
16671667
// possible. If there are two such sets of e and n, pick the e and n for
16681668
// which n × 10ᵉ⁻ᵖ⁺¹ is larger.
16691669
var
1670-
e = Math.floor(Math.log(Math.abs(x)) / Math.LN10),
1670+
e = log10Floor(Math.abs(x)),
16711671

16721672
// Easier to get to m from here
16731673
f = Math.round(Math.exp((Math.abs(e - p + 1)) * Math.LN10)),
@@ -2841,6 +2841,20 @@ function addLocaleData (data, tag) {
28412841
// Helper functions
28422842
// ================
28432843

2844+
/**
2845+
* A function to deal with the inaccuracy of calculating log10 in pre-ES6
2846+
* JavaScript environments. Math.log(num) / Math.LN10 was responsible for
2847+
* causing issue #62.
2848+
*/
2849+
function log10Floor (n) {
2850+
// ES6 provides the more accurate Math.log10
2851+
if (typeof Math.log10 === 'function')
2852+
return Math.floor(Math.log10(n));
2853+
2854+
var x = Math.round(Math.log(n) * Math.LOG10E);
2855+
return x - (Number('1e' + x) > n);
2856+
}
2857+
28442858
/**
28452859
* A merge of the Intl.{Constructor}.supportedLocalesOf functions
28462860
* To make life easier, the function should be bound to the constructor's internal

0 commit comments

Comments
 (0)