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

Commit

Permalink
Add neg(ation) (#103)
Browse files Browse the repository at this point in the history
* Add neg(ation)

* Document change
  • Loading branch information
jessealama authored Apr 11, 2024
1 parent 3388436 commit 642cf49
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions src/decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1520,7 +1520,7 @@ export class Decimal128 {
);
}

private negate(): Decimal128 {
neg(): Decimal128 {
let s = this.toString({ normalize: false });

if (s.match(/^-/)) {
Expand All @@ -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) {
Expand Down
52 changes: 52 additions & 0 deletions tests/neg.test.js
Original file line number Diff line number Diff line change
@@ -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
);
});
});

0 comments on commit 642cf49

Please sign in to comment.