@hestia-earth/utils 0.17.3 → 0.17.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/number.js +13 -7
  2. package/package.json +1 -1
package/dist/number.js CHANGED
@@ -38,13 +38,19 @@ exports.isNumber = isNumber;
38
38
  */
39
39
  var toPrecision = function (n, precision) {
40
40
  if (precision === void 0) { precision = 3; }
41
- var mult = Math.pow(10, precision - Math.floor(Math.log(n < 0 ? -n : n) / Math.LN10) - 1);
42
- var multiplier = Math.max(
43
- // handle floating errors like 0.00009999999999999999
44
- +"".concat(mult).replace(/[0]([1-9])\1+/g, function (v) { return "".concat(+v.substring(0, 1) + 1); }),
45
- // dividing by 0.00001 will give floating point error
46
- 0.0001);
47
- return n === 0 ? 0 : Math.round(n * multiplier) / multiplier;
41
+ // Use Math.log10 for better accuracy
42
+ var logN = Math.log10(Math.abs(n));
43
+ // Use a small epsilon to handle floating point errors in log10
44
+ var floorLogN = Math.floor(logN + 1e-13);
45
+ var mult = Math.pow(10, precision - floorLogN - 1);
46
+ var cappedMultiplied = 0.0001;
47
+ return n === 0
48
+ ? 0
49
+ : !(0, exports.isNumber)(n)
50
+ ? undefined
51
+ : mult < cappedMultiplied
52
+ ? Math.round(n * cappedMultiplied) / cappedMultiplied
53
+ : Number(n.toPrecision(precision));
48
54
  };
49
55
  exports.toPrecision = toPrecision;
50
56
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/utils",
3
- "version": "0.17.3",
3
+ "version": "0.17.5",
4
4
  "description": "HESTIA Utils library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",