From fa035c5dba88e06412ef0714142be5383a58e18d Mon Sep 17 00:00:00 2001 From: Jesse Alama Date: Tue, 7 May 2024 09:30:34 +0200 Subject: [PATCH] Allow construction from Numbers --- src/decimal128.mts | 11 +---------- tests/constructor.test.js | 12 ++++++------ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/decimal128.mts b/src/decimal128.mts index 9ad9941..a3969d1 100644 --- a/src/decimal128.mts +++ b/src/decimal128.mts @@ -790,16 +790,7 @@ export class Decimal128 { ensureFullySpecifiedConstructorOptions(options); if ("number" === typeof n) { - if (!Number.isInteger(n)) { - throw new SyntaxError("Non-integer number not permitted"); - } - if (!Number.isSafeInteger(n)) { - throw new RangeError( - "Integer is too large to be exactly represented" - ); - } - - s = n.toString(); + s = Object.is(n, -0) ? "-0" : n.toString(); } else if ("bigint" === typeof n) { s = n.toString(); } else { diff --git a/tests/constructor.test.js b/tests/constructor.test.js index 49690af..933b101 100644 --- a/tests/constructor.test.js +++ b/tests/constructor.test.js @@ -587,18 +587,18 @@ describe("number arguments", () => { expect(new Decimal128(42).toString()).toStrictEqual("42"); }); test("non-integer number", () => { - expect(() => new Decimal128(42.5)).toThrow(SyntaxError); + expect(new Decimal128(42.5).toString()).toStrictEqual("42.5"); }); test("NaN", () => { - expect(() => new Decimal128(NaN)).toThrow(SyntaxError); + expect(new Decimal128(NaN).toString()).toStrictEqual("NaN"); }); test("minus zero", () => { - expect(new Decimal128(-0).toString()).toStrictEqual("0"); + expect(new Decimal128(-0).toString()).toStrictEqual("-0"); }); - test("too big", () => { + test("very large value gets approximated", () => { expect( - () => new Decimal128(123456789012345678901234567890123456789) - ).toThrow(RangeError); + new Decimal128(123456789012345678901234567890123456789).toString() + ).toStrictEqual("123456789012345680000000000000000000000"); }); });