Skip to content

Commit 548b808

Browse files
committed
add note explaining decimal scale logic is based on testing, rather than defined in spec
1 parent c3d72ae commit 548b808

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/datatypes/decimal.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export class Decimal {
6868

6969
const unscaledResult = new Decimal(operation.call(this.value, decimalOther.value));
7070

71+
// NOTE: As of 2.0.0, the CQL spec says that scale of a Decimal should be preserved,
72+
// but does not describe how to propagate scale through arithmetic operations.
73+
// Unless otherwise stated, all the scale logic in this class is a best-guess based on testing.
7174
if (scaleLogic) {
7275
const targetScale = scaleLogic.call(null, this.scale, decimalOther.scale);
7376
return unscaledResult.withScale(targetScale);
@@ -96,6 +99,8 @@ export class Decimal {
9699
throw new RangeError('Cannot divide a decimal by zero');
97100
}
98101
// division scaling is more complex, depends on whether the actual result can be represented exactly
102+
// IMPORTANT: The details of how to propagate Decimal scale through math are not defined in the CQL spec.
103+
// The notes below are a best-guess on how to get desirable results based on some examples.
99104
const unscaledResult = this.applyWrapper(this.value.dividedBy, decimalOther);
100105
const unscaledDecimalPlaces = unscaledResult.value.decimalPlaces();
101106
if (unscaledDecimalPlaces > CQL_IMPLICIT_SCALE) {
@@ -111,7 +116,7 @@ export class Decimal {
111116
const preferredScale = Math.max(this.scale - decimalOther.scale, 0);
112117

113118
// examples:
114-
// | | Preferred | Expected |
119+
// | | Preferred | Desired |
115120
// | Expression | scale | result |
116121
// | ------------- | --------: | ---------: |
117122
// | 4.0 / 2 | 1 | 2.0 |

0 commit comments

Comments
 (0)