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

Commit 7585371

Browse files
committed
Ensure consistent use of bigints
1 parent d01fa53 commit 7585371

File tree

1 file changed

+7
-37
lines changed

1 file changed

+7
-37
lines changed

src/decimal128.mts

+7-37
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,7 @@ function adjustNonInteger(
605605
case ROUNDING_MODE_HALF_TRUNCATE:
606606
return roundHalfTrunc(x);
607607
default:
608-
throw new Error(
609-
`Unsupported rounding mode "${options.roundingMode}"`
610-
);
608+
return roundHalfEven(x);
611609
}
612610
}
613611

@@ -677,7 +675,7 @@ function handleDecimalNotation(
677675
}
678676

679677
if ("-." === withoutUnderscores) {
680-
throw new SyntaxError("Lone minus sign and period not permitted");
678+
throw new SyntaxError("Lone minus sign and period anot permitted");
681679
}
682680

683681
let isNegative = !!withoutUnderscores.match(/^-/);
@@ -993,12 +991,8 @@ function ensureFullySpecifiedCmpOptions(
993991
return opts;
994992
}
995993

996-
function toRational(isNegative: boolean, sg: string, exp: number): Rational {
997-
if (sg === NAN || sg === POSITIVE_INFINITY) {
998-
return new Rational(0n, 1n);
999-
}
1000-
1001-
if ("1" === sg) {
994+
function toRational(isNegative: boolean, sg: bigint, exp: number): Rational {
995+
if (1n === sg) {
1002996
// power of ten
1003997
if (exp < 0) {
1004998
return new Rational(
@@ -1069,24 +1063,13 @@ export class Decimal128 {
10691063

10701064
this.rat =
10711065
this.isFinite && !this.isNaN
1072-
? toRational(
1073-
this.isNegative,
1074-
this.significand.toString(),
1075-
this.exponent
1076-
)
1066+
? toRational(this.isNegative, this.significand, this.exponent)
10771067
: new Rational(0n, 1n);
10781068
}
10791069

10801070
private emitExponential(): string {
10811071
let prefix = this.isNegative ? "-" : "";
1082-
let originalSg = this.significand;
1083-
1084-
if (typeof originalSg == "undefined") {
1085-
throw new Error("Significand is undefined");
1086-
}
1087-
1088-
let sg = originalSg.toString();
1089-
1072+
let sg = this.significand.toString();
10901073
let exp = this.exponent;
10911074

10921075
let adjustedExponent = exp + sg.length - 1;
@@ -1108,13 +1091,7 @@ export class Decimal128 {
11081091

11091092
private emitDecimal(options: FullySpecifiedToStringOptions): string {
11101093
let prefix = this.isNegative ? "-" : "";
1111-
let originalSg = this.significand;
1112-
1113-
if (typeof originalSg == "undefined") {
1114-
throw new Error("Significand is undefined");
1115-
}
1116-
1117-
let sg = originalSg.toString();
1094+
let sg = this.significand.toString();
11181095
let exp = this.exponent;
11191096
let isZ = this.isZero();
11201097

@@ -1129,13 +1106,6 @@ export class Decimal128 {
11291106

11301107
let numFractionalDigits = options.numDecimalDigits;
11311108

1132-
if (
1133-
"number" === typeof numFractionalDigits &&
1134-
numFractionalDigits < 0
1135-
) {
1136-
numFractionalDigits = undefined;
1137-
}
1138-
11391109
let renderedRat = this.rat.toDecimalPlaces(
11401110
numFractionalDigits ?? MAX_SIGNIFICANT_DIGITS
11411111
);

0 commit comments

Comments
 (0)