diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ff7e73..08dea5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [12.1.0] - 2024-04-11 + +### Added + +- New negation operator `neg` + ## [12.0.0] - 2024-04-10 ### Removed diff --git a/src/decimal128.mts b/src/decimal128.mts index 02e25c8..619b94e 100644 --- a/src/decimal128.mts +++ b/src/decimal128.mts @@ -1209,14 +1209,14 @@ export class Decimal128 { if (!this.isFinite) { if (this.isNegative) { - return this.negate(); + return this.neg(); } return this.clone(); } if (this.isNegative) { - return this.negate(); + return this.neg(); } return this.clone(); @@ -1250,7 +1250,7 @@ export class Decimal128 { } if (this.isNegative && x.isNegative) { - return this.negate().add(x.negate(), opts).negate(); + return this.neg().add(x.neg(), opts).neg(); } let resultRat = Rational.add(this.rat, x.rat); @@ -1290,11 +1290,11 @@ export class Decimal128 { } if (!x.isFinite) { - return x.negate(); + return x.neg(); } if (x.isNegative) { - return this.add(x.negate()); + return this.add(x.neg()); } let rendered = Rational.subtract(this.rat, x.rat).toDecimalPlaces( @@ -1348,11 +1348,11 @@ export class Decimal128 { } if (this.isNegative) { - return this.negate().multiply(x).negate(); + return this.neg().multiply(x).neg(); } if (x.isNegative) { - return this.multiply(x.negate()).negate(); + return this.multiply(x.neg()).neg(); } let resultRat = Rational.multiply(this.rat, x.rat); @@ -1413,11 +1413,11 @@ export class Decimal128 { } if (this.isNegative) { - return this.negate().divide(x).negate(); + return this.neg().divide(x).neg(); } if (x.isNegative) { - return this.divide(x.negate()).negate(); + return this.divide(x.neg()).neg(); } let adjust = 0; @@ -1520,7 +1520,7 @@ export class Decimal128 { ); } - private negate(): Decimal128 { + neg(): Decimal128 { let s = this.toString({ normalize: false }); if (s.match(/^-/)) { @@ -1543,11 +1543,11 @@ export class Decimal128 { } if (this.isNegative) { - return this.negate().remainder(d).negate(); + return this.neg().remainder(d).neg(); } if (d.isNegative) { - return this.remainder(d.negate()); + return this.remainder(d.neg()); } if (!this.isFinite) { diff --git a/tests/neg.test.js b/tests/neg.test.js new file mode 100644 index 0000000..c9b1deb --- /dev/null +++ b/tests/neg.test.js @@ -0,0 +1,52 @@ +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("neg", () => { + test("minus zero", () => { + expect(new Decimal128("-0").neg().toString()).toStrictEqual("0"); + }); + test("zero", () => { + expect(new Decimal128("0").neg().toString()).toStrictEqual("-0"); + }); + test("NaN", () => { + expect(new Decimal128("NaN").neg().toString()).toStrictEqual("NaN"); + }); + test("negative number", () => { + expect(new Decimal128("-42.51").neg().toString()).toStrictEqual( + "42.51" + ); + }); + test("positive number", () => { + expect(new Decimal128("42.51").neg().toString()).toStrictEqual( + "-42.51" + ); + }); + test("preserve trailing zeros", () => { + expect( + new Decimal128("-42.510").neg().toString({ normalize: false }) + ).toStrictEqual("42.510"); + }); + test("-Infinity", () => { + expect(new Decimal128("-Infinity").neg().toString()).toStrictEqual( + "Infinity" + ); + }); + test("Infinity", () => { + expect(new Decimal128("Infinity").neg().toString()).toStrictEqual( + "-Infinity" + ); + }); + test("limit of digits", () => { + expect(new Decimal128("-" + bigDigits).neg().toString()).toStrictEqual( + bigDigits + ); + }); +});