Skip to content

Commit 1f51873

Browse files
committed
Large numbers (5e25)
1 parent 8fa78b2 commit 1f51873

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

src/__tests__/FloatToolkit/operations/addAndSubtract.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe("FloatToolkit.add()", () => {
1616

1717
expect(ft.add([2022.1, 2022.3])).toBe(4044.4);
1818
expect(ft.add([0.123, 0.456, 0.1])).toBe(0.679);
19+
expect(ft.add([5e30, 7e30])).toBe(12e30);
1920
});
2021

2122
it("should round to the default precision if and only if specified in the options", () => {
@@ -55,6 +56,7 @@ describe("FloatToolkit.subtract()", () => {
5556

5657
expect(ft.subtract([2022.2, 0.1])).toBe(2022.1);
5758
expect(ft.subtract([0.679, 0.1])).toBe(0.579);
59+
expect(ft.subtract([12e30, 7e30])).toBe(5e30);
5860
});
5961

6062
it("should round to the default precision if and only if specified in the options", () => {

src/__tests__/FloatToolkit/operations/multiplyAndDivide.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe("FloatToolkit.multiply()", () => {
1515
expect(ft.multiply([0.1, 0.9])).toBe(0.09);
1616

1717
expect(ft.multiply([2022.1, 0.7])).toBe(1415.47);
18+
expect(ft.multiply([2.9e31, 3.1e31])).toBe(8.99e62);
1819
});
1920

2021
it("should round to the default precision if and only if specified in the options", () => {
@@ -52,6 +53,7 @@ describe("FloatToolkit.divide()", () => {
5253
expect(ft.divide([0.09, 0.9])).toBe(0.1);
5354

5455
expect(ft.divide([1415.47, 0.7])).toBe(2022.1);
56+
expect(ft.divide([8.99e62, 3.1e31])).toBe(2.9e31);
5557

5658
ft.defaultPrecision = 17;
5759
expect(ft.divide([0.09, 0.9])).toBe(0.09999999999999999);

src/eval/isLargeNumber.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @internal
3+
*/
4+
export const isLargeNumber = (n: number) => n.toString().includes("e");

src/functions/round.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { precisionRange } from "../precisionRange.js";
33

44
import { isNumber } from "../eval/isNumber.js";
55
import { isValidPrecision } from "../eval/isValidPrecision.js";
6+
import { isLargeNumber } from "../eval/isLargeNumber.js";
67

78
/**
89
* @internal
@@ -14,5 +15,5 @@ export function round(n?: number, precision?: FloatToolkit.Precision): number {
1415
`Argument for 'precision' must be an integer between ${precisionRange.min} and ${precisionRange.max - 1}.`
1516
);
1617

17-
return Number(n!.toFixed(precision));
18+
return Number(isLargeNumber(n!) ? n!.toPrecision(precision! + 1) : n!.toFixed(precision));
1819
}

src/get-precision/getPrecision.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import FloatToolkit from "../index.js";
22

3+
import { isLargeNumber } from "../eval/isLargeNumber.js";
4+
35
/**
46
* @internal
57
*/
68
export function getPrecision(n: number): FloatToolkit.Precision {
79
const nString = n.toString();
8-
const pointPosition = nString.indexOf(".") + 1;
910

11+
const pointPosition = nString.indexOf(".") + 1;
1012
if (pointPosition === 0) return 1;
11-
return (nString.length - pointPosition) as FloatToolkit.Precision;
13+
14+
const ePosition = isLargeNumber(n) ? nString.indexOf("e") : nString.length;
15+
return (ePosition - pointPosition) as FloatToolkit.Precision;
1216
}

0 commit comments

Comments
 (0)