Comments (11)
Type 9007199254740993
into a JavaScript REPL. What do you get?
from bignumber.js.
JavaScript uses ties-to-even semantics. Here I've configured this library to use half-up semantics, and I expected that to apply to this operation.
from bignumber.js.
As per the documentation, x.toNumber()
does not use ROUNDING_MODE
and is the equivalent to Number(x)
.
from bignumber.js.
Shouldn't it? Is there a reason it doesn't? It seems like if you're doing your operations with a particular rounding mode, and then trying to get a Number at the end, you'd probably want the rounding mode to apply to the process of getting the Number as well. I did, at least.
from bignumber.js.
Type
9007199254740993
into a JavaScript REPL. What do you get?
The reason I asked this is because the language itself cannot, using its native number-type, represent the number you wish to calculate.
To represent numbers that the native JavaScript number-type cannot, you must use strings and a library like BigNumber that can work with them.
The problem cannot be worked-around. If you want an accurate decimal number type (at this time) in JavaScript, it's strings or nothing.
from bignumber.js.
I'm not asking to represent the mathematical value 9007199254740993 in JS. I'm asking that, when I represent that value using this library, and convert it to a JS number (which necessarily entails rounding), the conversion to a JS number should respect the rounding mode configured in this library.
from bignumber.js.
Press F12 to open the Chrome console.
Type the following:
9007199254740993 === 9007199254740992
If you can explain how a library like BigNumber.js is able to solve this problem, please let us know so we can write a Pull Request to fix it.
from bignumber.js.
I know how numbers in JavaScript work. BigNumber('9007199254740993')
is not a JavaScript number. As long as the library is configured with enough precision, it precisely represents the mathematical value 9007199254740993. It is perfectly possible to write an algorithm which converts that value to a JavaScript number under any choice of rounding mode.
from bignumber.js.
BigNumber('9007199254740993')
is indeed not a JavaScript number, but you did say you want to convert it to one, right? In such a case, the library must consider more cases than just this single number.
Perhaps I'm missing the point, but the assumption I've been working under is that the whole purpose of BigNumber et al. is to avoid the pitfalls of IEEE-754 and MAX_SAFE_INTEGER.
Apologies, but I'm not very clear on the use-case of implementing such a library and then converting its results to the very thing we're trying to save ourselves from.
from bignumber.js.
I think it's a reasonable enhancement proposal on the face of it, but ultimately just isn't worth implementing.
Although straightforward, it would only make a difference when converting values with more than 15 significant digits that do not decimal-binary-decimal round-trip, so I am not convinced its limited utility justifies the extra code weight and implementation effort required. It would also be considerably slower than the current method as it would involve converting the value to binary before rounding.
There is also the issue of what should be the result of converting your example value, 9007199254740993
, when the rounding mode is ROUND_HALF_EVEN
, which is defined here as:
Rounds towards nearest neighbour. If equidistant, rounds towards even neighbour.
when 9007199254740992
and 9007199254740994
are both even!
from bignumber.js.
Fair enough. Might be worth documenting how toNumber
chooses which JS value to use when the true value is not precisely representable?
There is also the issue of what should be the result of converting your example value,
9007199254740993
, when the rounding mode isROUND_HALF_EVEN
, which is defined here as:
True! I think of ROUND_HALF_EVEN
as meaning IEEE's ties-to-even, which refers to the evenness of the significand (and so it would be 9007199254740992
). But that's not quite the meaning here.
from bignumber.js.
Related Issues (20)
- Using version 9.1.1, run new BigNumber(0).exponentiatedBy('-12345678901234567890123456789012345678901234567890').valueOf() Is return to infinity normal? HOT 1
- Issue toNumber() function round up when number is too big (v9.1.1) HOT 2
- Division on exponential values leads to infinite loop HOT 3
- 负数加减乘除不好用 HOT 1
- [Feature Request]: Can we support `toBigInt`?
- Type error HOT 2
- Big Number (toFixed,dp,decimalPlaces,toFormat) - Chrome Update Issue 115.0.5790.102 HOT 24
- Minus sign in formatted strings HOT 2
- wrong calculation result on multipliedBy/times methods HOT 1
- Calculation error HOT 1
- Is there any other way to handle new BigNumber(999999999999999999999) without using strings? HOT 2
- Difference between multipliedBy and dividedBy behaviors HOT 2
- Different result between toFixed and toNumber HOT 1
- Can't convert to Base64 HOT 1
- Tree shake commonjs version from mjs version HOT 1
- toFixed error
- [Bug] Return Type of Methods HOT 2
- Logarithms (I know there is another issue about this)
- toString() default base is not 10? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bignumber.js.