-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: #1485 improve conversion of numbers with a round-off errors into…
… a BigNumber
- Loading branch information
Showing
5 changed files
with
100 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { digits } from '../number.js' | ||
|
||
/** | ||
* Convert a number into a BigNumber when it is safe to do so: only when the number | ||
* is has max 15 digits, since a JS number can only represent about ~16 digits. | ||
* This function will correct round-off errors introduced by the JS floating-point | ||
* operations. For example, it will change 0.1+0.2 = 0.30000000000000004 into 0.3. | ||
* | ||
* The function throws an Error when the number cannot be converted safely into a BigNumber. | ||
* | ||
* @param {number} x | ||
* @param {function} BigNumber The bignumber constructor | ||
* @returns {BigNumber} | ||
*/ | ||
export function convertNumberToBigNumber (x, BigNumber) { | ||
const d = digits(x) | ||
if (d.length <= 15) { | ||
return new BigNumber(x) | ||
} | ||
|
||
// recognize round-off errors like 0.1 + 0.2 = 0.30000000000000004, which should be 0.3 | ||
// we test whether the first 15 digits end with at least 6 zeros, and a non-zero last digit | ||
// note that a number can optionally end with an exponent | ||
const xStr = x.toString() | ||
const matchTrailingZeros = xStr.match(/(?<start>.+)(?<zeros>0{6,}[1-9][0-9]*)(?<end>$|[+-eE])/) | ||
if (matchTrailingZeros) { | ||
const { start, end } = matchTrailingZeros.groups | ||
return new BigNumber(start + end) | ||
} | ||
|
||
// recognize round-off errors like 40 - 38.6 = 1.3999999999999986, which should be 1.4 | ||
// we test whether the first 15 digits end with at least 6 nines, and a non-nine and non-zero last digit | ||
// note that a number can optionally end with an exponent | ||
const matchTrailingNines = xStr.match(/(?<start>.+)(?<digitBeforeNines>[0-8])(?<nines>9{6,}[1-8][0-9]*)(?<end>$|[+-eE])/) | ||
if (matchTrailingNines) { | ||
const { start, digitBeforeNines, end } = matchTrailingNines.groups | ||
return new BigNumber(start + String(parseInt(digitBeforeNines) + 1) + end) | ||
} | ||
|
||
throw new TypeError('Cannot implicitly convert a number with >15 significant digits to BigNumber ' + | ||
'(value: ' + x + '). ' + | ||
'Use function bignumber(x) to convert to BigNumber.') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/unit-tests/utils/bignumber/convertNumberToBigNumber.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import assert from 'assert' | ||
import BigNumber from 'decimal.js' | ||
import { convertNumberToBigNumber } from '../../../../src/utils/bignumber/convertNumberToBigNumber.js' | ||
|
||
describe('convertNumberToBigNumber', function () { | ||
it('should convert numbers to BigNumbers when it is safe to do', function () { | ||
assert.deepStrictEqual(convertNumberToBigNumber(2.4, BigNumber), new BigNumber('2.4')) | ||
assert.deepStrictEqual(convertNumberToBigNumber(2, BigNumber), new BigNumber('2')) | ||
assert.deepStrictEqual(convertNumberToBigNumber(-4, BigNumber), new BigNumber('-4')) | ||
assert.deepStrictEqual(convertNumberToBigNumber(0.1234567, BigNumber), new BigNumber('0.1234567')) | ||
assert.deepStrictEqual(convertNumberToBigNumber(0.12345678901234, BigNumber), new BigNumber('0.12345678901234')) | ||
assert.deepStrictEqual(convertNumberToBigNumber(0.00000000000004, BigNumber), new BigNumber('0.00000000000004')) | ||
assert.deepStrictEqual(convertNumberToBigNumber(1.2e-24, BigNumber), new BigNumber('1.2e-24')) | ||
}) | ||
|
||
it('should convert numbers with round-off errors to BigNumbers when it is safe to do', function () { | ||
// a round-off error above the actual value | ||
assert.deepStrictEqual(convertNumberToBigNumber(0.1 + 0.2, BigNumber).toString(), '0.3') // 0.30000000000000004 | ||
assert.deepStrictEqual(convertNumberToBigNumber(0.1 + 0.24545, BigNumber).toString(), '0.34545') // 0.34545000000000003 | ||
|
||
// a round-off error below the actual value | ||
assert.deepStrictEqual(convertNumberToBigNumber(40 - 38.6, BigNumber).toString(), '1.4') // 1.3999999999999986 | ||
assert.deepStrictEqual(convertNumberToBigNumber(159.119 - 159, BigNumber).toString(), '0.119') // 0.11899999999999977 | ||
assert.deepStrictEqual(convertNumberToBigNumber(159.11934444 - 159, BigNumber).toString(), '0.11934444') // 0.11934443999999189 | ||
}) | ||
|
||
it('should throw an error when converting an number to BigNumber when it is NOT safe to do', function () { | ||
const errorRegex = /Cannot implicitly convert a number with >15 significant digits to BigNumber/ | ||
|
||
assert.throws(() => convertNumberToBigNumber(Math.PI, BigNumber), errorRegex) | ||
assert.throws(() => convertNumberToBigNumber(1 / 3, BigNumber), errorRegex) | ||
assert.throws(() => convertNumberToBigNumber(1 / 7, BigNumber), errorRegex) | ||
assert.throws(() => convertNumberToBigNumber(0.1234567890123456, BigNumber), errorRegex) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters