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

Commit

Permalink
Remove rounding in arithmetic operations (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessealama authored Apr 24, 2024
1 parent 153344e commit 24b0f31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 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]

## [13.0.0] - 2024-04-24

### Removed

- Rounding options for arithmetic operations. The rounding mode is now always, implicitly, `halfEven`. Rounding is still supported in the constructor.

## [12.3.0] - 2024-04-16

### Added
Expand Down
52 changes: 9 additions & 43 deletions src/decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -861,10 +861,6 @@ const DEFAULT_CONSTRUCTOR_OPTIONS: FullySpecifiedConstructorOptions =
normalize: CONSTRUCTOR_SHOULD_NORMALIZE,
});

interface ArithmeticOperationOptions {
roundingMode?: RoundingMode;
}

interface FullySpecifiedArithmeticOperationOptions {
roundingMode: RoundingMode;
}
Expand Down Expand Up @@ -914,25 +910,6 @@ function ensureFullySpecifiedConstructorOptions(
return opts;
}

function ensureFullySpecifiedArithmeticOperationOptions(
options?: ArithmeticOperationOptions
): FullySpecifiedArithmeticOperationOptions {
let opts = { ...DEFAULT_ARITHMETIC_OPERATION_OPTIONS };

if (undefined === options) {
return opts;
}

if (
"string" === typeof options.roundingMode &&
ROUNDING_MODES.includes(options.roundingMode)
) {
opts.roundingMode = options.roundingMode;
}

return opts;
}

function ensureFullySpecifiedToStringOptions(
options?: ToStringOptions
): FullySpecifiedToStringOptions {
Expand Down Expand Up @@ -1294,9 +1271,8 @@ export class Decimal128 {
* Add this Decimal128 value to one or more Decimal128 values.
*
* @param x
* @param opts
*/
add(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
add(x: Decimal128): Decimal128 {
if (this.isNaN || x.isNaN) {
return new Decimal128(NAN);
}
Expand All @@ -1318,14 +1294,13 @@ export class Decimal128 {
}

if (this.isNegative && x.isNegative) {
return this.neg().add(x.neg(), opts).neg();
return this.neg().add(x.neg()).neg();
}

let resultRat = Rational.add(this.rat, x.rat);
let options = ensureFullySpecifiedArithmeticOperationOptions(opts);
let initialResult = new Decimal128(
resultRat.toDecimalPlaces(MAX_SIGNIFICANT_DIGITS + 1),
{ roundingMode: options.roundingMode }
{ roundingMode: ROUNDING_MODE_DEFAULT }
);
let adjusted = initialResult.setExponent(
Math.min(this.exponent, x.exponent)
Expand All @@ -1338,9 +1313,8 @@ export class Decimal128 {
* Subtract another Decimal128 value from one or more Decimal128 values.
*
* @param x
* @param opts
*/
subtract(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
subtract(x: Decimal128): Decimal128 {
if (this.isNaN || x.isNaN) {
return new Decimal128(NAN);
}
Expand Down Expand Up @@ -1369,9 +1343,7 @@ export class Decimal128 {
MAX_SIGNIFICANT_DIGITS + 1
);

let options = ensureFullySpecifiedArithmeticOperationOptions(opts);

let initialResult = new Decimal128(rendered, options);
let initialResult = new Decimal128(rendered);
let adjusted = initialResult.setExponent(
Math.min(this.exponent, x.exponent)
);
Expand All @@ -1384,9 +1356,8 @@ export class Decimal128 {
* If no arguments are given, return this value.
*
* @param x
* @param opts
*/
multiply(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
multiply(x: Decimal128): Decimal128 {
if (this.isNaN || x.isNaN) {
return new Decimal128(NAN);
}
Expand Down Expand Up @@ -1425,8 +1396,7 @@ export class Decimal128 {

let resultRat = Rational.multiply(this.rat, x.rat);
let initialResult = new Decimal128(
resultRat.toDecimalPlaces(MAX_SIGNIFICANT_DIGITS + 1),
ensureFullySpecifiedArithmeticOperationOptions(opts)
resultRat.toDecimalPlaces(MAX_SIGNIFICANT_DIGITS + 1)
);
let adjusted = initialResult.setExponent(this.exponent + x.exponent);

Expand All @@ -1445,9 +1415,8 @@ export class Decimal128 {
* Divide this Decimal128 value by another Decimal128 value.
*
* @param x
* @param opts
*/
divide(x: Decimal128, opts?: ArithmeticOperationOptions): Decimal128 {
divide(x: Decimal128): Decimal128 {
if (this.isNaN || x.isNaN) {
return new Decimal128(NAN);
}
Expand Down Expand Up @@ -1529,10 +1498,7 @@ export class Decimal128 {
}

let resultExponent = this.exponent - (x.exponent + adjust);
return new Decimal128(
`${resultCoefficient}E${resultExponent}`,
ensureFullySpecifiedArithmeticOperationOptions(opts)
);
return new Decimal128(`${resultCoefficient}E${resultExponent}`);
}

/**
Expand Down

0 comments on commit 24b0f31

Please sign in to comment.