Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 2b537db

Browse files
committed
WIP
1 parent a40f6d2 commit 2b537db

7 files changed

+96
-231
lines changed

src/Decimal.mts

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ function _cohort(s: string): "0" | "-0" | Rational {
2626
return "0";
2727
}
2828

29+
if (s.match(/^-?0[eE][+-]?[0-9]+$/)) {
30+
if (s.match(/^-/)) {
31+
return "-0";
32+
}
33+
34+
return "0";
35+
}
36+
2937
return Rational.fromString(s);
3038
}
3139

src/Decimal128.mts

+22-6
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,7 @@ export class Decimal128 {
359359
let p = this._isNegative ? "-" : "";
360360

361361
if (v === "0" || v === "-0") {
362-
if (q < 0) {
363-
return p + v + "." + "0".repeat(0 - q);
364-
}
365-
366-
return v;
362+
return v + "e" + (q < 0 ? "-" : "+") + Math.abs(q);
367363
}
368364

369365
let m = this.mantissa();
@@ -504,7 +500,27 @@ export class Decimal128 {
504500
: POSITIVE_INFINITY;
505501
}
506502

507-
return this.round(n).emitDecimal();
503+
let rounded = this.round(n + 1);
504+
let roundedRendered = rounded.emitDecimal();
505+
506+
if (roundedRendered.match(/[.]/)) {
507+
let [lhs, rhs] = roundedRendered.split(/[.]/);
508+
if (rhs.length < n) {
509+
return lhs + "." + rhs + "0".repeat(n - rhs.length);
510+
}
511+
512+
if (n === 0) {
513+
return lhs;
514+
}
515+
516+
return lhs + "." + rhs.substring(0, n);
517+
}
518+
519+
if (n === 0) {
520+
return roundedRendered;
521+
}
522+
523+
return roundedRendered + "." + "0".repeat(n);
508524
}
509525

510526
toPrecision(opts?: { digits?: number }): string {

tests/Decimal128/exponent.test.js

-43
This file was deleted.

tests/Decimal128/mantissa.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Decimal128 } from "../../src/Decimal128.mjs";
2+
3+
describe("mantissa and exponent", () => {
4+
test("0", () => {
5+
expect(() => new Decimal128("0").mantissa()).toThrow(RangeError);
6+
});
7+
test("0.0", () => {
8+
expect(() => new Decimal128("0.0").mantissa()).toThrow(RangeError);
9+
});
10+
test("-0", () => {
11+
expect(() => new Decimal128("-0").mantissa()).toThrow(RangeError);
12+
});
13+
let data = [
14+
["123.456", "1.23456", 2],
15+
["5", "5", 0],
16+
["1.20", "1.2", 0],
17+
["-123.456", "-1.23456", 2],
18+
["0.0042", "4.2", -3],
19+
["0.00000000000000000000000000000000000001", "1", -38],
20+
["1000", "1", 3],
21+
["-1000", "-1", 3],
22+
["-0.00001", "-1", -5],
23+
["0.5", "5", -1],
24+
["-10", "-1", 1],
25+
["10", "1", 1],
26+
["0.000001", "1", -6],
27+
["0.0000012", "1.2", -6],
28+
];
29+
for (const [n, sigDigits, exponent] of data) {
30+
test(`simple example (${n})`, () => {
31+
let d = new Decimal128(n);
32+
expect(d.mantissa().toString()).toStrictEqual(sigDigits);
33+
expect(d.exponent()).toStrictEqual(exponent);
34+
});
35+
}
36+
});

tests/Decimal128/significand.test.js

-134
This file was deleted.

tests/Decimal128/toexponential.test.js

+22-40
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe("toExponential", function () {
8181
);
8282
});
8383
describe("negative", () => {
84-
let negD = decimalD.neg();
84+
let negD = decimalD.negate();
8585
test("integer part", () => {
8686
expect(negD.toExponential({ digits: 3 }).toString()).toStrictEqual(
8787
"-1.234e+2"
@@ -92,73 +92,55 @@ describe("toExponential", function () {
9292

9393
describe("to exponential string", () => {
9494
test("one", () => {
95-
expect(
96-
new Decimal128("1").toString({ format: "exponential" })
97-
).toStrictEqual("1e+0");
95+
expect(new Decimal128("1").toExponential()).toStrictEqual("1e+0");
9896
});
9997
test("zero", () => {
100-
expect(
101-
new Decimal128("0").toString({ format: "exponential" })
102-
).toStrictEqual("0e+0");
98+
expect(new Decimal128("0").toExponential()).toStrictEqual("0e+0");
10399
});
104100
test("minus zero", () => {
105-
expect(
106-
new Decimal128("-0").toString({ format: "exponential" })
107-
).toStrictEqual("-0e+0");
101+
expect(new Decimal128("-0").toExponential()).toStrictEqual("-0e+0");
108102
});
109103
test("integer", () => {
110-
expect(
111-
new Decimal128("42").toString({ format: "exponential" })
112-
).toStrictEqual("4.2e+1");
104+
expect(new Decimal128("42").toExponential()).toStrictEqual("4.2e+1");
113105
});
114106

115107
test("round trip", () => {
116-
expect(
117-
new Decimal128("4.2E+0").toString({ format: "exponential" })
118-
).toStrictEqual("4.2e+0");
108+
expect(new Decimal128("4.2E+0").toExponential()).toStrictEqual(
109+
"4.2e+0"
110+
);
119111
});
120112

121113
test("significant has one digit", () => {
122-
expect(
123-
new Decimal128("1").toString({ format: "exponential" })
124-
).toStrictEqual("1e+0");
114+
expect(new Decimal128("1").toExponential()).toStrictEqual("1e+0");
125115
});
126116
test("negative exponent", () => {
127-
expect(
128-
new Decimal128("0.1").toString({ format: "exponential" })
129-
).toStrictEqual("1e-1");
117+
expect(new Decimal128("0.1").toExponential()).toStrictEqual("1e-1");
130118
});
131119
test("negative exponent, multiple digits", () => {
132-
expect(
133-
new Decimal128("0.01042").toString({ format: "exponential" })
134-
).toStrictEqual("1.042e-2");
120+
expect(new Decimal128("0.01042").toExponential()).toStrictEqual(
121+
"1.042e-2"
122+
);
135123
});
136124
});
137125

138126
describe("scientific string syntax", () => {
139127
test("1.23E+3", () => {
140-
expect(
141-
new Decimal128("1.23E+3").toString({ format: "decimal" })
142-
).toStrictEqual("1230");
128+
expect(new Decimal128("1.23E+3").toString()).toStrictEqual("1230");
143129
});
144130
test("1.23E+5", () => {
145-
expect(
146-
new Decimal128("1.23E+5").toString({ format: "decimal" })
147-
).toStrictEqual("123000");
131+
expect(new Decimal128("1.23E+5").toString()).toStrictEqual("123000");
148132
});
149133
test("1.23E-8", () => {
150-
expect(
151-
new Decimal128("1.23E-8").toString({ format: "decimal" })
152-
).toStrictEqual("0.0000000123");
134+
expect(new Decimal128("1.23E-8").toString()).toStrictEqual(
135+
"0.0000000123"
136+
);
153137
});
154138
test("-1.23E-10", () => {
155-
expect(
156-
new Decimal128("-1.23E-10").toString({ format: "decimal" })
157-
).toStrictEqual("-0.000000000123");
139+
expect(new Decimal128("-1.23E-10").toString()).toStrictEqual(
140+
"-0.000000000123"
141+
);
158142
});
159143
test("0E+2", () => {
160-
expect(
161-
new Decimal128("0E+2").toString({ format: "exponential" })
162-
).toStrictEqual("0e+2");
144+
expect(new Decimal128("0E+2").toExponential()).toStrictEqual("0e+2");
163145
});
164146
});

0 commit comments

Comments
 (0)