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

Commit 642cf49

Browse files
authored
Add neg(ation) (#103)
* Add neg(ation) * Document change
1 parent 3388436 commit 642cf49

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [12.1.0] - 2024-04-11
11+
12+
### Added
13+
14+
- New negation operator `neg`
15+
1016
## [12.0.0] - 2024-04-10
1117

1218
### Removed

src/decimal128.mts

+12-12
Original file line numberDiff line numberDiff line change
@@ -1209,14 +1209,14 @@ export class Decimal128 {
12091209

12101210
if (!this.isFinite) {
12111211
if (this.isNegative) {
1212-
return this.negate();
1212+
return this.neg();
12131213
}
12141214

12151215
return this.clone();
12161216
}
12171217

12181218
if (this.isNegative) {
1219-
return this.negate();
1219+
return this.neg();
12201220
}
12211221

12221222
return this.clone();
@@ -1250,7 +1250,7 @@ export class Decimal128 {
12501250
}
12511251

12521252
if (this.isNegative && x.isNegative) {
1253-
return this.negate().add(x.negate(), opts).negate();
1253+
return this.neg().add(x.neg(), opts).neg();
12541254
}
12551255

12561256
let resultRat = Rational.add(this.rat, x.rat);
@@ -1290,11 +1290,11 @@ export class Decimal128 {
12901290
}
12911291

12921292
if (!x.isFinite) {
1293-
return x.negate();
1293+
return x.neg();
12941294
}
12951295

12961296
if (x.isNegative) {
1297-
return this.add(x.negate());
1297+
return this.add(x.neg());
12981298
}
12991299

13001300
let rendered = Rational.subtract(this.rat, x.rat).toDecimalPlaces(
@@ -1348,11 +1348,11 @@ export class Decimal128 {
13481348
}
13491349

13501350
if (this.isNegative) {
1351-
return this.negate().multiply(x).negate();
1351+
return this.neg().multiply(x).neg();
13521352
}
13531353

13541354
if (x.isNegative) {
1355-
return this.multiply(x.negate()).negate();
1355+
return this.multiply(x.neg()).neg();
13561356
}
13571357

13581358
let resultRat = Rational.multiply(this.rat, x.rat);
@@ -1413,11 +1413,11 @@ export class Decimal128 {
14131413
}
14141414

14151415
if (this.isNegative) {
1416-
return this.negate().divide(x).negate();
1416+
return this.neg().divide(x).neg();
14171417
}
14181418

14191419
if (x.isNegative) {
1420-
return this.divide(x.negate()).negate();
1420+
return this.divide(x.neg()).neg();
14211421
}
14221422

14231423
let adjust = 0;
@@ -1520,7 +1520,7 @@ export class Decimal128 {
15201520
);
15211521
}
15221522

1523-
private negate(): Decimal128 {
1523+
neg(): Decimal128 {
15241524
let s = this.toString({ normalize: false });
15251525

15261526
if (s.match(/^-/)) {
@@ -1543,11 +1543,11 @@ export class Decimal128 {
15431543
}
15441544

15451545
if (this.isNegative) {
1546-
return this.negate().remainder(d).negate();
1546+
return this.neg().remainder(d).neg();
15471547
}
15481548

15491549
if (d.isNegative) {
1550-
return this.remainder(d.negate());
1550+
return this.remainder(d.neg());
15511551
}
15521552

15531553
if (!this.isFinite) {

tests/neg.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Decimal128 } from "../src/decimal128.mjs";
2+
3+
const MAX_SIGNIFICANT_DIGITS = 34;
4+
const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS);
5+
6+
const zero = new Decimal128("0");
7+
const minusZero = new Decimal128("-0");
8+
const one = new Decimal128("1");
9+
const minusOne = new Decimal128("-1");
10+
const two = new Decimal128("2");
11+
12+
describe("neg", () => {
13+
test("minus zero", () => {
14+
expect(new Decimal128("-0").neg().toString()).toStrictEqual("0");
15+
});
16+
test("zero", () => {
17+
expect(new Decimal128("0").neg().toString()).toStrictEqual("-0");
18+
});
19+
test("NaN", () => {
20+
expect(new Decimal128("NaN").neg().toString()).toStrictEqual("NaN");
21+
});
22+
test("negative number", () => {
23+
expect(new Decimal128("-42.51").neg().toString()).toStrictEqual(
24+
"42.51"
25+
);
26+
});
27+
test("positive number", () => {
28+
expect(new Decimal128("42.51").neg().toString()).toStrictEqual(
29+
"-42.51"
30+
);
31+
});
32+
test("preserve trailing zeros", () => {
33+
expect(
34+
new Decimal128("-42.510").neg().toString({ normalize: false })
35+
).toStrictEqual("42.510");
36+
});
37+
test("-Infinity", () => {
38+
expect(new Decimal128("-Infinity").neg().toString()).toStrictEqual(
39+
"Infinity"
40+
);
41+
});
42+
test("Infinity", () => {
43+
expect(new Decimal128("Infinity").neg().toString()).toStrictEqual(
44+
"-Infinity"
45+
);
46+
});
47+
test("limit of digits", () => {
48+
expect(new Decimal128("-" + bigDigits).neg().toString()).toStrictEqual(
49+
bigDigits
50+
);
51+
});
52+
});

0 commit comments

Comments
 (0)