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

Commit 24b0f31

Browse files
authored
Remove rounding in arithmetic operations (#106)
1 parent 153344e commit 24b0f31

File tree

2 files changed

+15
-43
lines changed

2 files changed

+15
-43
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+
## [13.0.0] - 2024-04-24
11+
12+
### Removed
13+
14+
- Rounding options for arithmetic operations. The rounding mode is now always, implicitly, `halfEven`. Rounding is still supported in the constructor.
15+
1016
## [12.3.0] - 2024-04-16
1117

1218
### Added

src/decimal128.mts

+9-43
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,6 @@ const DEFAULT_CONSTRUCTOR_OPTIONS: FullySpecifiedConstructorOptions =
861861
normalize: CONSTRUCTOR_SHOULD_NORMALIZE,
862862
});
863863

864-
interface ArithmeticOperationOptions {
865-
roundingMode?: RoundingMode;
866-
}
867-
868864
interface FullySpecifiedArithmeticOperationOptions {
869865
roundingMode: RoundingMode;
870866
}
@@ -914,25 +910,6 @@ function ensureFullySpecifiedConstructorOptions(
914910
return opts;
915911
}
916912

917-
function ensureFullySpecifiedArithmeticOperationOptions(
918-
options?: ArithmeticOperationOptions
919-
): FullySpecifiedArithmeticOperationOptions {
920-
let opts = { ...DEFAULT_ARITHMETIC_OPERATION_OPTIONS };
921-
922-
if (undefined === options) {
923-
return opts;
924-
}
925-
926-
if (
927-
"string" === typeof options.roundingMode &&
928-
ROUNDING_MODES.includes(options.roundingMode)
929-
) {
930-
opts.roundingMode = options.roundingMode;
931-
}
932-
933-
return opts;
934-
}
935-
936913
function ensureFullySpecifiedToStringOptions(
937914
options?: ToStringOptions
938915
): FullySpecifiedToStringOptions {
@@ -1294,9 +1271,8 @@ export class Decimal128 {
12941271
* Add this Decimal128 value to one or more Decimal128 values.
12951272
*
12961273
* @param x
1297-
* @param opts
12981274
*/
1299-
add(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
1275+
add(x: Decimal128): Decimal128 {
13001276
if (this.isNaN || x.isNaN) {
13011277
return new Decimal128(NAN);
13021278
}
@@ -1318,14 +1294,13 @@ export class Decimal128 {
13181294
}
13191295

13201296
if (this.isNegative && x.isNegative) {
1321-
return this.neg().add(x.neg(), opts).neg();
1297+
return this.neg().add(x.neg()).neg();
13221298
}
13231299

13241300
let resultRat = Rational.add(this.rat, x.rat);
1325-
let options = ensureFullySpecifiedArithmeticOperationOptions(opts);
13261301
let initialResult = new Decimal128(
13271302
resultRat.toDecimalPlaces(MAX_SIGNIFICANT_DIGITS + 1),
1328-
{ roundingMode: options.roundingMode }
1303+
{ roundingMode: ROUNDING_MODE_DEFAULT }
13291304
);
13301305
let adjusted = initialResult.setExponent(
13311306
Math.min(this.exponent, x.exponent)
@@ -1338,9 +1313,8 @@ export class Decimal128 {
13381313
* Subtract another Decimal128 value from one or more Decimal128 values.
13391314
*
13401315
* @param x
1341-
* @param opts
13421316
*/
1343-
subtract(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
1317+
subtract(x: Decimal128): Decimal128 {
13441318
if (this.isNaN || x.isNaN) {
13451319
return new Decimal128(NAN);
13461320
}
@@ -1369,9 +1343,7 @@ export class Decimal128 {
13691343
MAX_SIGNIFICANT_DIGITS + 1
13701344
);
13711345

1372-
let options = ensureFullySpecifiedArithmeticOperationOptions(opts);
1373-
1374-
let initialResult = new Decimal128(rendered, options);
1346+
let initialResult = new Decimal128(rendered);
13751347
let adjusted = initialResult.setExponent(
13761348
Math.min(this.exponent, x.exponent)
13771349
);
@@ -1384,9 +1356,8 @@ export class Decimal128 {
13841356
* If no arguments are given, return this value.
13851357
*
13861358
* @param x
1387-
* @param opts
13881359
*/
1389-
multiply(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
1360+
multiply(x: Decimal128): Decimal128 {
13901361
if (this.isNaN || x.isNaN) {
13911362
return new Decimal128(NAN);
13921363
}
@@ -1425,8 +1396,7 @@ export class Decimal128 {
14251396

14261397
let resultRat = Rational.multiply(this.rat, x.rat);
14271398
let initialResult = new Decimal128(
1428-
resultRat.toDecimalPlaces(MAX_SIGNIFICANT_DIGITS + 1),
1429-
ensureFullySpecifiedArithmeticOperationOptions(opts)
1399+
resultRat.toDecimalPlaces(MAX_SIGNIFICANT_DIGITS + 1)
14301400
);
14311401
let adjusted = initialResult.setExponent(this.exponent + x.exponent);
14321402

@@ -1445,9 +1415,8 @@ export class Decimal128 {
14451415
* Divide this Decimal128 value by another Decimal128 value.
14461416
*
14471417
* @param x
1448-
* @param opts
14491418
*/
1450-
divide(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
1419+
divide(x: Decimal128): Decimal128 {
14511420
if (this.isNaN || x.isNaN) {
14521421
return new Decimal128(NAN);
14531422
}
@@ -1529,10 +1498,7 @@ export class Decimal128 {
15291498
}
15301499

15311500
let resultExponent = this.exponent - (x.exponent + adjust);
1532-
return new Decimal128(
1533-
`${resultCoefficient}E${resultExponent}`,
1534-
ensureFullySpecifiedArithmeticOperationOptions(opts)
1535-
);
1501+
return new Decimal128(`${resultCoefficient}E${resultExponent}`);
15361502
}
15371503

15381504
/**

0 commit comments

Comments
 (0)