Skip to content

Commit

Permalink
Restore isNormal (used in tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessealama committed Nov 15, 2024
1 parent d111462 commit 05af782
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import { Rational } from "./Rational.mjs";
import { Decimal } from "./Decimal.mjs";

const EXPONENT_MIN = -6176;
const NORMAL_EXPONENT_MIN = -6143;
const EXPONENT_MAX = 6111;
const NORMAL_EXPONENT_MAX = 6144;
const MAX_SIGNIFICANT_DIGITS = 34;

const bigTen = BigInt(10);
Expand Down Expand Up @@ -1046,6 +1048,70 @@ export class Decimal128 {
let q = this.divide(d).round(0, ROUNDING_MODE_TRUNCATE);
return this.subtract(d.multiply(q));
}

isNormal(): boolean {
if (this.isNaN()) {
throw new RangeError("Cannot determine whether NaN is normal");
}

if (!this.isFinite()) {
throw new RangeError(
"Only finite numbers can be said to be normal or not"
);
}

if (this.isZero()) {
throw new RangeError(
"Only non-zero numbers can be said to be normal or not"
);
}

let exp = this.exponent();
return exp >= NORMAL_EXPONENT_MIN && exp <= NORMAL_EXPONENT_MAX;
}

isSubnormal(): boolean {
if (this.isNaN()) {
throw new RangeError("Cannot determine whether NaN is subnormal");
}

if (!this.isFinite()) {
throw new RangeError(
"Only finite numbers can be said to be subnormal or not"
);
}

let exp = this.exponent();
return exp < NORMAL_EXPONENT_MIN;
}

truncatedExponent(): number {
if (this.isZero() || this.isSubnormal()) {
return NORMAL_EXPONENT_MIN;
}

return this.exponent();
}

scaledSignificand(): bigint {
if (this.isNaN()) {
throw new RangeError("NaN does not have a scaled significand");
}

if (!this.isFinite()) {
throw new RangeError("Infinity does not have a scaled significand");
}

if (this.isZero()) {
return 0n;
}

let v = this.cohort() as Rational;
let te = this.truncatedExponent();
let ss = v.scale10(MAX_SIGNIFICANT_DIGITS - 1 - te);

return ss.numerator;
}
}

Decimal128.prototype.valueOf = function () {
Expand Down

0 comments on commit 05af782

Please sign in to comment.