Fix js rounding errors

pull/10616/head
Andrew Watkins 2017-08-28 11:58:04 -07:00
parent 1f7f8e02c4
commit 78cab61c9c
3 changed files with 19 additions and 12 deletions

View File

@ -96,6 +96,7 @@
},
"dependencies": {
"axios": "^0.13.1",
"bignumber.js": "^4.0.2",
"bootstrap": "^3.3.7",
"calculate-size": "^1.1.1",
"classnames": "^2.2.3",

View File

@ -1,4 +1,7 @@
const PADDING_FACTOR = 0.1
import BigNumber from 'bignumber.js'
const ADD_FACTOR = 1.1
const SUB_FACTOR = 0.9
const considerEmpty = (userNumber, number) => {
if (userNumber) {
@ -16,19 +19,21 @@ const getRange = (
const {value, rangeValue, operator} = ruleValues
const [userMin, userMax] = userSelectedRange
const subtractPadding = val => +val - Math.abs(val * PADDING_FACTOR)
const addPadding = val => +val + Math.abs(val * PADDING_FACTOR)
const addPad = bigNum => bigNum.times(ADD_FACTOR).toNumber()
const subPad = bigNum => bigNum.times(SUB_FACTOR).toNumber()
const pad = val => {
if (val === null || val === '') {
const pad = v => {
if (v === null || v === '') {
return null
}
const val = new BigNumber(v)
if (operator === 'less than') {
return val < 0 ? addPadding(val) : subtractPadding(val)
return val.lessThan(0) ? addPad(val) : subPad(val)
}
return val < 0 ? subtractPadding(val) : addPadding(val)
return val.lessThan(0) ? subPad(val) : addPad(val)
}
const points = [...timeSeries, [null, pad(value)], [null, pad(rangeValue)]]
@ -58,11 +63,8 @@ const getRange = (
)
const [calcMin, calcMax] = range
const [min, max] = [
considerEmpty(userMin, calcMin),
considerEmpty(userMax, calcMax),
]
const min = considerEmpty(userMin, calcMin)
const max = considerEmpty(userMax, calcMax)
if (min === max) {
if (min > 0) {

View File

@ -1446,6 +1446,10 @@ big.js@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.1.3.tgz#4cada2193652eb3ca9ec8e55c9015669c9806978"
bignumber.js@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.0.2.tgz#2d1dc37ee5968867ecea90b6da4d16e68608d21d"
binary-extensions@^1.0.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.8.0.tgz#48ec8d16df4377eae5fa5884682480af4d95c774"