From 569e881a4e76072a170dd3555dc47e2b0b43a5ef Mon Sep 17 00:00:00 2001 From: Jesse Alama Date: Tue, 25 Jun 2024 14:28:03 +0200 Subject: [PATCH] WIP --- src/Decimal128.mts | 20 +-- tests/Decimal128/abs.test.js | 6 - tests/Decimal128/cmp.test.js | 198 ++++++++++++++++++++++ tests/Decimal128/equals.test.js | 272 ------------------------------ tests/Decimal128/lessthan.test.js | 248 --------------------------- 5 files changed, 208 insertions(+), 536 deletions(-) create mode 100644 tests/Decimal128/cmp.test.js delete mode 100644 tests/Decimal128/equals.test.js delete mode 100644 tests/Decimal128/lessthan.test.js diff --git a/src/Decimal128.mts b/src/Decimal128.mts index e02b874..3b5385e 100644 --- a/src/Decimal128.mts +++ b/src/Decimal128.mts @@ -13,7 +13,7 @@ * @author Jesse Alama */ -import { Digit, DigitOrTen, RoundingMode, ROUNDING_MODES } from "./common.mjs"; +import { RoundingMode, ROUNDING_MODES } from "./common.mjs"; import { Rational } from "./Rational.mjs"; import { Decimal } from "./Decimal.mjs"; @@ -596,7 +596,7 @@ export class Decimal128 { * * @param x */ - private cmp(x: Decimal128): number { + cmp(x: Decimal128): number { if (this.isNaN() || x.isNaN()) { return NaN; } @@ -621,20 +621,20 @@ export class Decimal128 { return x.isNegative() ? 1 : -1; } + if (this.isZero()) { + if (x.isZero()) { + return 0; + } + + return x.isNegative() ? 1 : -1; + } + let ourCohort = this.cohort() as Rational; let theirCohort = x.cohort() as Rational; return ourCohort.cmp(theirCohort); } - lessThan(x: Decimal128): boolean { - return this.cmp(x) === -1; - } - - equals(x: Decimal128): boolean { - return this.cmp(x) === 0; - } - abs(): Decimal128 { if (this.isNaN()) { return new Decimal128(NAN); diff --git a/tests/Decimal128/abs.test.js b/tests/Decimal128/abs.test.js index 1ed3b0f..a99611d 100644 --- a/tests/Decimal128/abs.test.js +++ b/tests/Decimal128/abs.test.js @@ -3,12 +3,6 @@ import { Decimal128 } from "../../src/Decimal128.mjs"; const MAX_SIGNIFICANT_DIGITS = 34; const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS); -const zero = new Decimal128("0"); -const minusZero = new Decimal128("-0"); -const one = new Decimal128("1"); -const minusOne = new Decimal128("-1"); -const two = new Decimal128("2"); - describe("abs", () => { test("minus zero", () => { expect(new Decimal128("-0").abs().toString()).toStrictEqual("0"); diff --git a/tests/Decimal128/cmp.test.js b/tests/Decimal128/cmp.test.js new file mode 100644 index 0000000..860bc0c --- /dev/null +++ b/tests/Decimal128/cmp.test.js @@ -0,0 +1,198 @@ +import { Decimal128 } from "../../src/Decimal128.mjs"; + +const MAX_SIGNIFICANT_DIGITS = 34; +const nan = new Decimal128("NaN"); +const zero = new Decimal128("0"); +const negZero = new Decimal128("-0"); +const one = new Decimal128("1"); + +describe("equals", () => { + let d1 = new Decimal128("987.123"); + let d2 = new Decimal128("123.456789"); + test("simple example", () => { + expect(d1.cmp(d1)).toStrictEqual(0); + }); + test("non-example", () => { + expect(d1.cmp(d2)).toStrictEqual(1); + }); + test("negative numbers", () => { + let a = new Decimal128("-123.456"); + expect(a.cmp(a)).toStrictEqual(0); + }); + test("integer part is the same, decimal part is not", () => { + let a = new Decimal128("42.678"); + let b = new Decimal128("42.6789"); + expect(a.cmp(b)).toStrictEqual(-1); + }); + test("negative and positive are different", () => { + expect( + new Decimal128("-123.456").cmp(new Decimal128("123.456")) + ).toStrictEqual(-1); + }); + test("limit of significant digits", () => { + expect( + new Decimal128("0.4166666666666666666666666666666667").cmp( + new Decimal128("0.41666666666666666666666666666666666") + ) + ).toStrictEqual(0); + }); + test("beyond limit of significant digits", () => { + expect( + new Decimal128("0.41666666666666666666666666666666667").cmp( + new Decimal128("0.41666666666666666666666666666666666") + ) + ).toStrictEqual(0); + }); + test("non-example", () => { + expect( + new Decimal128("0.037").cmp(new Decimal128("0.037037037037")) + ).toStrictEqual(-1); + }); + describe("examples from a presentation", () => { + let a = new Decimal128("1.00"); + let b = new Decimal128("1.0000"); + let c = new Decimal128("1.0001"); + let d = new Decimal128("0.9999"); + test("use mathematical equality by default", () => { + expect(a.cmp(b)).toStrictEqual(0); + }); + test("mathematically distinct", () => { + expect(a.cmp(c)).toStrictEqual(-1); + }); + test("mathematically distinct, again", () => { + expect(b.cmp(d)).toStrictEqual(1); + }); + test("mathematically distinct, once more", () => { + expect(a.cmp(d)).toStrictEqual(1); + }); + }); +}); + +describe("many digits", () => { + test("non-integers get rounded", () => { + expect( + new Decimal128("0." + "4".repeat(MAX_SIGNIFICANT_DIGITS + 50)).cmp( + new Decimal128("0." + "4".repeat(MAX_SIGNIFICANT_DIGITS)) + ) + ).toStrictEqual(0); + }); + test("non-equality within limits", () => { + expect( + new Decimal128("0." + "4".repeat(33)).cmp( + new Decimal128("0." + "4".repeat(MAX_SIGNIFICANT_DIGITS)) + ) + ).toStrictEqual(-1); + }); + describe("NaN", () => { + test("NaN equals NaN throws", () => { + expect(nan.cmp(nan)).toStrictEqual(NaN); + }); + test("number equals NaN throws", () => { + expect(one.cmp(nan)).toStrictEqual(NaN); + }); + test("NaN equals number throws", () => { + expect(nan.cmp(one)).toStrictEqual(NaN); + }); + }); + describe("minus zero", () => { + test("left hand", () => { + expect(negZero.cmp(zero)).toStrictEqual(0); + }); + test("right hand", () => { + expect(zero.cmp(negZero)).toStrictEqual(0); + }); + test("both arguments", () => { + expect(negZero.cmp(negZero)).toStrictEqual(0); + }); + }); + describe("infinity", () => { + let posInf = new Decimal128("Infinity"); + let negInf = new Decimal128("-Infinity"); + test("positive infinity vs number", () => { + expect(posInf.cmp(one)).toStrictEqual(1); + }); + test("negative infinity vs number", () => { + expect(negInf.cmp(one)).toStrictEqual(-1); + }); + test("negative infintity vs positive infinity", () => { + expect(negInf.cmp(posInf)).toStrictEqual(-1); + }); + test("positive infinity vs negative infinity", () => { + expect(posInf.cmp(negInf)).toStrictEqual(1); + }); + test("positive infinity both arguments", () => { + expect(posInf.cmp(posInf)).toStrictEqual(0); + }); + test("negative infinity both arguments", () => { + expect(negInf.cmp(negInf)).toStrictEqual(0); + }); + test("compare number to positive infinity", () => { + expect(one.cmp(posInf)).toStrictEqual(-1); + }); + test("compare number to negative infinity", () => { + expect(one.cmp(negInf)).toStrictEqual(1); + }); + }); +}); + +describe("zero", () => { + test("positive zero", () => { + expect(zero.cmp(zero)).toStrictEqual(0); + }); + test("negative zero", () => { + expect(negZero.cmp(negZero)).toStrictEqual(0); + }); + test("negative zero vs zero", () => { + expect(negZero.cmp(zero)).toStrictEqual(0); + }); +}); + +describe("normalization", () => { + let d1 = new Decimal128("1.2"); + let d2 = new Decimal128("1.20"); + let d3 = new Decimal128("1.200"); + test("compare normalized to normalized", () => { + expect(d1.cmp(d2)).toStrictEqual(0); + }); + test("compare normalized to normalized", () => { + expect(d2.cmp(d3)).toStrictEqual(0); + }); + test("compare normalized to normalized", () => { + expect(d1.cmp(d3)).toStrictEqual(0); + }); +}); + +describe("examples from the General Decimal Arithmetic specification", () => { + describe("compare", () => { + test("example one", () => { + expect( + new Decimal128("2.1").cmp(new Decimal128("3")) + ).toStrictEqual(-1); + }); + test("example two", () => { + expect( + new Decimal128("2.1").cmp(new Decimal128("2.1")) + ).toStrictEqual(0); + }); + test("example three", () => { + expect( + new Decimal128("2.1").cmp(new Decimal128("2.10")) + ).toStrictEqual(0); + }); + test("example four", () => { + expect( + new Decimal128("3").cmp(new Decimal128("2.1")) + ).toStrictEqual(1); + }); + test("example five", () => { + expect( + new Decimal128("2.1").cmp(new Decimal128("-3")) + ).toStrictEqual(1); + }); + test("example six", () => { + expect( + new Decimal128("-3").cmp(new Decimal128("2.1")) + ).toStrictEqual(-1); + }); + }); +}); diff --git a/tests/Decimal128/equals.test.js b/tests/Decimal128/equals.test.js deleted file mode 100644 index eb64316..0000000 --- a/tests/Decimal128/equals.test.js +++ /dev/null @@ -1,272 +0,0 @@ -import { Decimal128 } from "../../src/Decimal128.mjs"; - -const MAX_SIGNIFICANT_DIGITS = 34; -const nan = new Decimal128("NaN"); -const zero = new Decimal128("0"); -const negZero = new Decimal128("-0"); -const one = new Decimal128("1"); - -describe("equals", () => { - let d1 = new Decimal128("987.123"); - let d2 = new Decimal128("123.456789"); - test("simple example", () => { - expect(d1.equals(d1)).toStrictEqual(true); - }); - test("non-example", () => { - expect(d1.equals(d2)).toStrictEqual(false); - }); - test("negative numbers", () => { - let a = new Decimal128("-123.456"); - expect(a.equals(a)).toStrictEqual(true); - }); - test("integer part is the same, decimal part is not", () => { - let a = new Decimal128("42.678"); - let b = new Decimal128("42.6789"); - expect(a.equals(b)).toStrictEqual(false); - }); - test("negative and positive are different", () => { - expect( - new Decimal128("-123.456").equals(new Decimal128("123.456")) - ).toStrictEqual(false); - }); - test("limit of significant digits", () => { - expect( - new Decimal128("0.4166666666666666666666666666666667").equals( - new Decimal128("0.4166666666666666666666666666666666") - ) - ).toStrictEqual(false); - }); - test("beyond limit of significant digits", () => { - expect( - new Decimal128("0.41666666666666666666666666666666667").equals( - new Decimal128("0.41666666666666666666666666666666666") - ) - ).toStrictEqual(true); - }); - test("non-example", () => { - expect( - new Decimal128("0.037").equals(new Decimal128("0.037037037037")) - ).toStrictEqual(false); - }); - describe("examples from a presentation", () => { - let a = new Decimal128("1.00"); - let b = new Decimal128("1.0000"); - let c = new Decimal128("1.0001"); - let d = new Decimal128("0.9999"); - test("use mathematical equality by default", () => { - expect(a.equals(b)).toStrictEqual(true); - }); - test("mathematically distinct", () => { - expect(a.equals(c)).toStrictEqual(false); - }); - test("mathematically distinct, again", () => { - expect(b.equals(d)).toStrictEqual(false); - }); - test("mathematically distinct, once more", () => { - expect(a.equals(d)).toStrictEqual(false); - }); - }); -}); - -describe("many digits", () => { - test("non-integers get rounded", () => { - expect( - new Decimal128( - "0." + "4".repeat(MAX_SIGNIFICANT_DIGITS + 50) - ).equals(new Decimal128("0." + "4".repeat(MAX_SIGNIFICANT_DIGITS))) - ).toStrictEqual(true); - }); - test("non-equality within limits", () => { - expect( - new Decimal128("0." + "4".repeat(33)).equals( - new Decimal128("0." + "4".repeat(MAX_SIGNIFICANT_DIGITS)) - ) - ).toStrictEqual(false); - }); - describe("NaN", () => { - test("NaN equals NaN throws", () => { - expect(() => nan.equals(nan)).toThrow(RangeError); - }); - test("number equals NaN throws", () => { - expect(() => one.equals(nan)).toThrow(RangeError); - }); - test("NaN equals number throws", () => { - expect(() => nan.equals(one)).toThrow(RangeError); - }); - }); - describe("minus zero", () => { - test("left hand", () => { - expect(negZero.equals(zero)).toStrictEqual(true); - }); - test("right hand", () => { - expect(zero.equals(negZero)).toStrictEqual(true); - }); - test("both arguments", () => { - expect(negZero.equals(negZero)).toStrictEqual(true); - }); - }); - describe("infinity", () => { - let posInf = new Decimal128("Infinity"); - let negInf = new Decimal128("-Infinity"); - test("positive infinity vs number", () => { - expect(posInf.equals(one)).toStrictEqual(false); - }); - test("negative infinity vs number", () => { - expect(negInf.equals(one)).toStrictEqual(false); - }); - test("negative infintity vs positive infinity", () => { - expect(negInf.equals(posInf)).toStrictEqual(false); - }); - test("positive infinity vs negative infinity", () => { - expect(posInf.equals(negInf)).toStrictEqual(false); - }); - test("positive infinity both arguments", () => { - expect(posInf.equals(posInf)).toStrictEqual(true); - }); - test("negative infinity both arguments", () => { - expect(negInf.equals(negInf)).toStrictEqual(true); - }); - test("compare number to positive infinity", () => { - expect(one.equals(posInf)).toStrictEqual(false); - }); - test("compare number to negative infinity", () => { - expect(one.equals(negInf)).toStrictEqual(false); - }); - }); -}); - -describe("zero", () => { - test("positive zero", () => { - expect(zero.equals(zero)).toStrictEqual(true); - }); - test("negative zero", () => { - expect(negZero.equals(negZero)).toStrictEqual(true); - }); - test("negative zero vs zero", () => { - expect(negZero.equals(zero)).toStrictEqual(true); - }); -}); - -describe("normalization", () => { - let d1 = new Decimal128("1.2"); - let d2 = new Decimal128("1.20"); - let d3 = new Decimal128("1.200"); - test("compare normalized to normalized", () => { - expect(d1.equals(d2)).toStrictEqual(true); - }); - test("compare normalized to normalized", () => { - expect(d2.equals(d3)).toStrictEqual(true); - }); - test("compare normalized to normalized", () => { - expect(d1.equals(d3)).toStrictEqual(true); - }); -}); - -describe("examples from the General Decimal Arithmetic specification", () => { - describe("compare", () => { - test("example one", () => { - expect( - new Decimal128("2.1").equals(new Decimal128("3")) - ).toStrictEqual(false); - }); - test("example two", () => { - expect( - new Decimal128("2.1").equals(new Decimal128("2.1")) - ).toStrictEqual(true); - }); - test("example three", () => { - expect( - new Decimal128("2.1").equals(new Decimal128("2.10")) - ).toStrictEqual(true); - }); - test("example four", () => { - expect( - new Decimal128("3").equals(new Decimal128("2.1")) - ).toStrictEqual(false); - }); - test("example five", () => { - expect( - new Decimal128("2.1").equals(new Decimal128("-3")) - ).toStrictEqual(false); - }); - test("example five", () => { - expect( - new Decimal128("-3").equals(new Decimal128("2.1")) - ).toStrictEqual(false); - }); - }); - describe("compare-total", () => { - test("example one", () => { - expect( - new Decimal128("12.73").equals(new Decimal128("127.9"), { - normalize: true, - }) - ).toStrictEqual(false); - }); - test("example two", () => { - expect( - new Decimal128("-127").equals(new Decimal128("12")) - ).toStrictEqual(false); - }); - test("example three", () => { - expect( - new Decimal128("12.30").equals(new Decimal128("12.3")) - ).toStrictEqual(true); // would be false if we didn't normalize - }); - test("example four", () => { - expect( - new Decimal128("12.30").equals(new Decimal128("12.30")) - ).toStrictEqual(true); - }); - test("example five", () => { - expect( - new Decimal128("12.3").equals(new Decimal128("12.300")) - ).toStrictEqual(true); // would be false if we didn't normalize - }); - test("example six", () => { - expect(() => - new Decimal128("12.3").equals(new Decimal128("NaN")) - ).toThrow(RangeError); // wouldn't throw if we did a total comparison - }); - describe("inline examples", () => { - test("example one", () => { - expect( - new Decimal128("-Infinity").equals(new Decimal128("-127")) - ).toStrictEqual(false); - }); - test("example two", () => { - expect( - new Decimal128("-1.00").equals(new Decimal128("-1")) - ).toStrictEqual(true); // would be false if we didn't normalize - }); - test("example three", () => { - expect(new Decimal128("-0.000").equals(negZero)).toStrictEqual( - true - ); // would be false if we didn't normalize - }); - test("example four", () => { - expect(negZero.equals(zero)).toStrictEqual(true); // would be false if we didn't normalize - }); - test("example five", () => { - expect( - new Decimal128("1.2300").equals(new Decimal128("1.23")) - ).toStrictEqual(true); // would be false if we didn't normalize - }); - test("example six", () => { - expect( - new Decimal128("1.23").equals(new Decimal128("1E+9")) - ).toStrictEqual(false); - }); - test("example seven", () => { - expect( - new Decimal128("1E+9").equals(new Decimal128("Infinity")) - ).toStrictEqual(false); - }); - test("example eight", () => { - expect(() => - new Decimal128("Infinity").equals(new Decimal128("NaN")) - ).toThrow(RangeError); // wouldn't throw if we did a total comparison - }); - }); - }); -}); diff --git a/tests/Decimal128/lessthan.test.js b/tests/Decimal128/lessthan.test.js deleted file mode 100644 index f29510f..0000000 --- a/tests/Decimal128/lessthan.test.js +++ /dev/null @@ -1,248 +0,0 @@ -import { Decimal128 } from "../../src/Decimal128.mjs"; - -const MAX_SIGNIFICANT_DIGITS = 34; -const nan = new Decimal128("NaN"); -const one = new Decimal128("1"); -const zero = new Decimal128("0"); -const negZero = new Decimal128("-0"); -let posInf = new Decimal128("Infinity"); -let negInf = new Decimal128("-Infinity"); - -describe("lessThan", () => { - let d1 = new Decimal128("987.123"); - let d2 = new Decimal128("123.456789"); - test("not true", () => { - expect(d1.lessThan(d1)).toStrictEqual(false); - }); - test("true", () => { - expect(d2.lessThan(d1)).toStrictEqual(true); - }); - test("negative numbers", () => { - let a = new Decimal128("-123.456"); - let b = new Decimal128("-987.654"); - expect(a.lessThan(b)).toStrictEqual(false); - expect(b.lessThan(a)).toStrictEqual(true); - }); - test("integer part is the same, decimal part is not", () => { - let a = new Decimal128("42.678"); - let b = new Decimal128("42.6789"); - expect(a.lessThan(b)).toStrictEqual(true); - expect(b.lessThan(a)).toStrictEqual(false); - }); - test("negative and positive are different", () => { - expect( - new Decimal128("-123.456").lessThan(new Decimal128("123.456")) - ).toStrictEqual(true); - }); - test("limit of significant digits", () => { - expect( - new Decimal128("0.4166666666666666666666666666666667").lessThan( - new Decimal128("0.4166666666666666666666666666666666") - ) - ).toStrictEqual(false); - }); - test("beyond limit of significant digits", () => { - expect( - new Decimal128("0.41666666666666666666666666666666667").lessThan( - new Decimal128("0.41666666666666666666666666666666666") - ) - ).toStrictEqual(false); - }); - test("more digits available", () => { - expect( - new Decimal128("0.037").lessThan(new Decimal128("0.037037037037")) - ).toStrictEqual(true); - }); - describe("examples from a presenation", () => { - let a = new Decimal128("1.00"); - let b = new Decimal128("1.0000"); - let c = new Decimal128("1.0001"); - let d = new Decimal128("0.9999"); - test("use mathematical equality by default", () => { - expect(b.lessThan(a)).toStrictEqual(false); - }); - test("mathematically distinct", () => { - expect(a.lessThan(c)).toStrictEqual(true); - }); - test("mathematically distinct, again", () => { - expect(b.lessThan(d)).toStrictEqual(false); - }); - test("mathematically distinct, once more", () => { - expect(a.lessThan(d)).toStrictEqual(false); - }); - }); -}); - -describe("many digits", () => { - test("non-equality within limits", () => { - expect( - new Decimal128("0." + "4".repeat(33)).lessThan( - new Decimal128("0." + "4".repeat(MAX_SIGNIFICANT_DIGITS)) - ) - ).toStrictEqual(true); - }); - describe("NaN", () => { - test("NaN lessThan NaN throws", () => { - expect(() => nan.lessThan(nan)).toThrow(RangeError); - }); - test("number lessThan NaN throws", () => { - expect(() => one.lessThan(nan)).toThrow(RangeError); - }); - test("NaN lessThan number throws ", () => { - expect(() => nan.lessThan(one)).toThrow(RangeError); - }); - }); - describe("minus zero", () => { - test("left hand", () => { - expect(negZero.lessThan(zero)).toStrictEqual(false); - }); - test("right hand", () => { - expect(zero.lessThan(negZero)).toStrictEqual(false); - }); - test("both arguments", () => { - expect(negZero.lessThan(negZero)).toStrictEqual(false); - }); - }); - describe("infinity", () => { - test("positive infinity vs number", () => { - expect(posInf.lessThan(one)).toStrictEqual(false); - }); - test("negative infinity vs number", () => { - expect(negInf.lessThan(one)).toStrictEqual(true); - }); - test("negative infintity vs positive infinity", () => { - expect(negInf.lessThan(posInf)).toStrictEqual(true); - }); - test("positive infinity vs negative infinity", () => { - expect(posInf.lessThan(negInf)).toStrictEqual(false); - }); - test("positive infinity both arguments", () => { - expect(posInf.lessThan(posInf)).toStrictEqual(false); - }); - test("negative infinity both arguments", () => { - expect(negInf.lessThan(negInf)).toStrictEqual(false); - }); - test("compare number to positive infinity", () => { - expect(one.lessThan(posInf)).toStrictEqual(true); - }); - test("compare number to negative infinity", () => { - expect(one.lessThan(negInf)).toStrictEqual(false); - }); - }); -}); - -describe("zero", () => { - test("positive zero", () => { - expect(zero.lessThan(zero)).toStrictEqual(false); - }); - test("negative zero", () => { - expect(negZero.lessThan(negZero)).toStrictEqual(false); - }); - test("negative zero vs zero", () => { - expect(negZero.lessThan(zero)).toStrictEqual(false); - }); - test("zero vs negative zero, normalization disabled", () => { - expect(zero.lessThan(negZero, { normalize: true })).toStrictEqual( - false - ); - }); -}); - -describe("normalization", () => { - let d1 = new Decimal128("1.2"); - let d2 = new Decimal128("1.20"); - let d3 = new Decimal128("1.200"); - test("compare normalized to normalized", () => { - expect(d1.lessThan(d2)).toStrictEqual(false); - }); - test("compare normalized to normalized", () => { - expect(d2.lessThan(d3)).toStrictEqual(false); - }); - test("compare normalized to normalized", () => { - expect(d1.lessThan(d3)).toStrictEqual(false); - }); - test("compare non-normal (1)", () => { - expect(d1.lessThan(d2)).toStrictEqual(false); - }); - test("compare two non-normal values", () => { - expect(d2.lessThan(d3)).toStrictEqual(false); - }); - test("compare two non-normal values", () => { - expect(d3.lessThan(d2)).toStrictEqual(false); - }); -}); - -describe("examples from the General Decimal Arithmetic specification", () => { - describe("compare", () => { - test("example one", () => { - expect( - new Decimal128("2.1").lessThan(new Decimal128("3")) - ).toStrictEqual(true); - }); - test("example two", () => { - expect( - new Decimal128("2.1").lessThan(new Decimal128("2.1")) - ).toStrictEqual(false); - }); - test("example three", () => { - expect( - new Decimal128("2.1").lessThan(new Decimal128("2.10")) - ).toStrictEqual(false); - }); - test("example four", () => { - expect( - new Decimal128("3").lessThan(new Decimal128("2.1")) - ).toStrictEqual(false); - }); - test("example five", () => { - expect( - new Decimal128("2.1").lessThan(new Decimal128("-3")) - ).toStrictEqual(false); - }); - test("example five", () => { - expect( - new Decimal128("-3").lessThan(new Decimal128("2.1")) - ).toStrictEqual(true); - }); - }); - describe("compare-total", () => { - test("example one", () => { - expect( - new Decimal128("12.73").lessThan(new Decimal128("127.9"), { - normalize: true, - }) - ).toStrictEqual(true); - }); - test("example two", () => { - expect( - new Decimal128("-127").lessThan(new Decimal128("12"), { - normalize: true, - }) - ).toStrictEqual(true); - }); - test("example three", () => { - expect( - new Decimal128("12.30").lessThan(new Decimal128("12.3")) - ).toStrictEqual(false); // would be true if we were to respect trailing zeroes - }); - test("example four", () => { - expect( - new Decimal128("12.30").lessThan(new Decimal128("12.30"), { - normalize: true, - }) - ).toStrictEqual(false); - }); - test("example five", () => { - expect( - new Decimal128("12.3").lessThan(new Decimal128("12.300"), { - normalize: true, - }) - ).toStrictEqual(false); - }); - test("example six", () => { - expect(() => - new Decimal128("12.3").lessThan(new Decimal128("NaN")) - ).toThrow(RangeError); // wouldn't throw if we were to use total ordering - }); - }); -});