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

Commit a40f6d2

Browse files
committed
WIP
1 parent 7e314c4 commit a40f6d2

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

src/Decimal128.mts

+4
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,10 @@ export class Decimal128 {
11341134
return this.clone();
11351135
}
11361136

1137+
if (d.isZero()) {
1138+
return new Decimal128(NAN);
1139+
}
1140+
11371141
if (this.cmp(d) === -1) {
11381142
return this.clone();
11391143
}

src/Rational.mts

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
countFractionalDigits,
33
countSignificantDigits,
44
Digit,
5+
ROUNDING_MODE_TRUNCATE,
56
RoundingMode,
67
} from "./common.mjs";
78

@@ -423,24 +424,19 @@ export class Rational {
423424
);
424425
}
425426

426-
if (this.isNegative) {
427-
return this.negate().round(numFractionalDigits, mode).negate();
428-
}
429-
430427
if (numFractionalDigits > 1) {
431428
return this.scale10(1)
432429
.round(numFractionalDigits - 1, mode)
433430
.scale10(-1);
434431
}
435-
436-
if (mode !== "halfEven") {
437-
throw new RangeError("Only half-even rounding mode is supported");
438-
}
439-
440432
let s = this.toFixed(1);
441433

442434
let [integerPart, fractionalPart] = s.split(".");
443435

436+
if (mode === ROUNDING_MODE_TRUNCATE) {
437+
return Rational.fromString(integerPart);
438+
}
439+
444440
let penultimateDigit = parseInt(
445441
integerPart.charAt(integerPart.length - 1)
446442
) as Digit;

src/common.mts

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ export function countFractionalDigits(s: string): number {
4242
export type Digit = -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; // -1 signals that we're moving from the integer part to the decimal part of a decimal number
4343
export type DigitOrTen = Digit | 10;
4444

45-
const ROUNDING_MODE_CEILING = "ceil";
46-
const ROUNDING_MODE_FLOOR = "floor";
47-
const ROUNDING_MODE_TRUNCATE = "trunc";
48-
const ROUNDING_MODE_HALF_EVEN = "halfEven";
49-
const ROUNDING_MODE_HALF_EXPAND = "halfExpand";
45+
export const ROUNDING_MODE_CEILING = "ceil";
46+
export const ROUNDING_MODE_FLOOR = "floor";
47+
export const ROUNDING_MODE_TRUNCATE = "trunc";
48+
export const ROUNDING_MODE_HALF_EVEN = "halfEven";
49+
export const ROUNDING_MODE_HALF_EXPAND = "halfExpand";
5050

5151
export type RoundingMode =
5252
| "ceil"

tests/Decimal128/remainder.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe("examples from the General Decimal Arithmetic Specification", () => {
123123
expect(
124124
new Decimal128("3.6")
125125
.remainder(new Decimal128("1.3"))
126-
.toString({ normalize: false })
126+
.toString({ preserveTrailingZeroes: true })
127127
).toStrictEqual("1.0");
128128
});
129129
});

tests/Rational/rational.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ describe("toString", () => {
3131
describe("toFixed", () => {
3232
test("zero", () => {
3333
let d = new Rational(0n, 1n);
34-
expect(d.toFixed(1)).toStrictEqual("0");
35-
expect(d.toFixed(5)).toStrictEqual("0");
34+
expect(d.toFixed(1)).toStrictEqual("0.0");
35+
expect(d.toFixed(5)).toStrictEqual("0.00000");
3636
});
3737
test("exactly representable", () => {
3838
expect(new Rational(3n, 2n).toFixed(1)).toStrictEqual("1.5");

0 commit comments

Comments
 (0)