Skip to content

Commit 913ad70

Browse files
committed
Fix minimum tick (and minimum tick value) precision (use decimals not floats)
1 parent 0890f4a commit 913ad70

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

docs/content/release_notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release Notes
22

3+
## 6.1.1
4+
**Bug Fixes**
5+
6+
* Corrected precision issue exhibited by `UnitCode.getMinimumTick` function by switching internal calculations to use `Decimal` instances (instead of native floating point numbers).
7+
* Corrected precision issue exhibited by `UnitCode.getMinimumTickValue` function by switching internal calculations to use `Decimal` instances (instead of native floating point numbers).
8+
39
## 6.1.0
410
**New Features**
511

docs/content/releases/6.1.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**Bug Fixes**
2+
3+
* Corrected precision issue exhibited by `UnitCode.getMinimumTick` function by switching internal calculations to use `Decimal` instances (instead of native floating point numbers).
4+
* Corrected precision issue exhibited by `UnitCode.getMinimumTickValue` function by switching internal calculations to use `Decimal` instances (instead of native floating point numbers).

docs/content/sdk/lib-utilities-data.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
* [.getFractionFactor([special])](#UnitCodegetFractionFactor) ⇒ <code>Number</code> \| <code>undefined</code>
167167
* [.getFractionDigits([special])](#UnitCodegetFractionDigits) ⇒ <code>Number</code> \| <code>undefined</code>
168168
* [.getMinimumTick(tickIncrement)](#UnitCodegetMinimumTick) ⇒ <code>Number</code>
169-
* [.getMinimumTickValue(tickIncrement, pointValue)](#UnitCodegetMinimumTickValue)
169+
* [.getMinimumTickValue(tickIncrement, pointValue)](#UnitCodegetMinimumTickValue) ⇒ <code>Number</code>
170170
* _static_
171171
* [.parse(code)](#UnitCodeparse)[<code>UnitCode</code>](#UnitCode) \| <code>null</code>
172172
* [.fromBaseCode(code)](#UnitCodefromBaseCode)[<code>UnitCode</code>](#UnitCode) \| <code>null</code>
@@ -306,6 +306,7 @@
306306
> up by the minimum tick.
307307
308308
**Kind**: instance method of [<code>UnitCode</code>](#UnitCode)
309+
**Returns**: <code>Number</code>
309310
**Access**: public
310311

311312
| Param | Type | Description |

lib/utilities/data/UnitCode.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const assert = require('@barchart/common-js/lang/assert'),
2+
Decimal = require('@barchart/common-js/lang/Decimal'),
23
is = require('@barchart/common-js/lang/is');
34

45
const Enum = require('@barchart/common-js/lang/Enum');
@@ -177,15 +178,20 @@ module.exports = (() => {
177178
getMinimumTick(tickIncrement) {
178179
assert.argumentIsValid(tickIncrement, 'tickIncrement', is.integer, 'must be an integer');
179180

181+
const one = new Decimal(1);
182+
const ten = new Decimal(10);
183+
180184
let discretePrice;
181185

182186
if (this.supportsFractions) {
183-
discretePrice = 1 / this._fractionFactor;
187+
discretePrice = one.divide(this._fractionFactor);
184188
} else {
185-
discretePrice = 1 / (Math.pow(10, this._decimalDigits));
189+
discretePrice = one.divide(ten.raise(this._decimalDigits));
186190
}
187191

188-
return discretePrice * tickIncrement;
192+
const minimumTick = discretePrice.multiply(tickIncrement);
193+
194+
return minimumTick.toFloat();
189195
}
190196

191197
/**
@@ -201,7 +207,10 @@ module.exports = (() => {
201207
assert.argumentIsValid(tickIncrement, 'tickIncrement', is.integer, 'must be an integer');
202208
assert.argumentIsValid(pointValue, 'pointValue', is.number, 'must be a number');
203209

204-
return this.getMinimumTick(tickIncrement) * pointValue;
210+
const minimumTick = new Decimal(this.getMinimumTick(tickIncrement));
211+
const minimumTickValue = minimumTick.multiply(pointValue);
212+
213+
return minimumTickValue.toFloat();
205214
}
206215

207216
toString() {

test/specs/utilities/data/UnitCodeSpec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ describe('When calculating minimum ticks and minimum tick values', () => {
985985
});
986986
});
987987

988-
describe('For unit code "5" and with a tickIncrement of 1 and a pointValue of 1000 (e.g. t-notes)', () => {
988+
describe('For unit code "5" and with a tickIncrement of 1 and a pointValue of 1,000 (e.g. t-notes)', () => {
989989
let uc;
990990

991991
beforeEach(() => {
@@ -1000,5 +1000,21 @@ describe('When calculating minimum ticks and minimum tick values', () => {
10001000
expect(uc.getMinimumTickValue(1, 1000)).toEqual(15.625);
10011001
});
10021002
});
1003+
1004+
describe('For unit code "E" and with a tickIncrement of 10 and a pointValue of 500,000 (e.g. mexican pesos)', () => {
1005+
let uc;
1006+
1007+
beforeEach(() => {
1008+
uc = UnitCode.parse('E');
1009+
});
1010+
1011+
it('The minimum tick should be 0.015625', () => {
1012+
expect(uc.getMinimumTick(10)).toEqual(0.00001);
1013+
});
1014+
1015+
it('The minimum tick value should be 15.625', () => {
1016+
expect(uc.getMinimumTickValue(10, 500000)).toEqual(5);
1017+
});
1018+
});
10031019
});
10041020

0 commit comments

Comments
 (0)