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

Commit 05af782

Browse files
committed
Restore isNormal (used in tests)
1 parent d111462 commit 05af782

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/Decimal128.mts

+66
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import { Rational } from "./Rational.mjs";
2323
import { Decimal } from "./Decimal.mjs";
2424

2525
const EXPONENT_MIN = -6176;
26+
const NORMAL_EXPONENT_MIN = -6143;
2627
const EXPONENT_MAX = 6111;
28+
const NORMAL_EXPONENT_MAX = 6144;
2729
const MAX_SIGNIFICANT_DIGITS = 34;
2830

2931
const bigTen = BigInt(10);
@@ -1046,6 +1048,70 @@ export class Decimal128 {
10461048
let q = this.divide(d).round(0, ROUNDING_MODE_TRUNCATE);
10471049
return this.subtract(d.multiply(q));
10481050
}
1051+
1052+
isNormal(): boolean {
1053+
if (this.isNaN()) {
1054+
throw new RangeError("Cannot determine whether NaN is normal");
1055+
}
1056+
1057+
if (!this.isFinite()) {
1058+
throw new RangeError(
1059+
"Only finite numbers can be said to be normal or not"
1060+
);
1061+
}
1062+
1063+
if (this.isZero()) {
1064+
throw new RangeError(
1065+
"Only non-zero numbers can be said to be normal or not"
1066+
);
1067+
}
1068+
1069+
let exp = this.exponent();
1070+
return exp >= NORMAL_EXPONENT_MIN && exp <= NORMAL_EXPONENT_MAX;
1071+
}
1072+
1073+
isSubnormal(): boolean {
1074+
if (this.isNaN()) {
1075+
throw new RangeError("Cannot determine whether NaN is subnormal");
1076+
}
1077+
1078+
if (!this.isFinite()) {
1079+
throw new RangeError(
1080+
"Only finite numbers can be said to be subnormal or not"
1081+
);
1082+
}
1083+
1084+
let exp = this.exponent();
1085+
return exp < NORMAL_EXPONENT_MIN;
1086+
}
1087+
1088+
truncatedExponent(): number {
1089+
if (this.isZero() || this.isSubnormal()) {
1090+
return NORMAL_EXPONENT_MIN;
1091+
}
1092+
1093+
return this.exponent();
1094+
}
1095+
1096+
scaledSignificand(): bigint {
1097+
if (this.isNaN()) {
1098+
throw new RangeError("NaN does not have a scaled significand");
1099+
}
1100+
1101+
if (!this.isFinite()) {
1102+
throw new RangeError("Infinity does not have a scaled significand");
1103+
}
1104+
1105+
if (this.isZero()) {
1106+
return 0n;
1107+
}
1108+
1109+
let v = this.cohort() as Rational;
1110+
let te = this.truncatedExponent();
1111+
let ss = v.scale10(MAX_SIGNIFICANT_DIGITS - 1 - te);
1112+
1113+
return ss.numerator;
1114+
}
10491115
}
10501116

10511117
Decimal128.prototype.valueOf = function () {

0 commit comments

Comments
 (0)