Potential bug when using "FixedNumber.fromValue" with large number of decimals
See original GitHub issueWhile working with FixedNumber
, I got this error after setting the number of decimals to 77:
fractional component exceeds decimals (fault=“underflow”, operation=“parseFixed”, code=NUMERIC_FAULT, version=bignumber/5.4.2)
Here’s my code:
const foo = "115792089237316195423570985008687907853269984665640564039457584007913129639935";
const decimals = 77;
const result = FixedNumber.fromValue(foo, decimals);
console.log(result.toString());
I then peaked into the guts of the FixedNumber
implementation, and noticed that the number of decimals gets overridden to 18 in this function:
That is, even if it’s 77 when it enters the fromValue
function, it gets set to 18 by the time it is used in fromString
.
My request is to document the maximum number of decimals that can be set when working with FixedNumber
, plus any other constraints that there might be.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Error: fractional component exceeds decimals in eithersJS
I think it's because your floating point number has too many decimal places to be represented by BigNumber thus, you need to truncate...
Read more >decimal — Decimal fixed point and floating point arithmetic ...
The decimal module incorporates a notion of significant places so that 1.30 + 1.20 is 2.50 . The trailing zero is kept to...
Read more >FixedNumber - ethers
A FixedNumber is a fixed-width (in bits) number with an internal base-10 divisor, which allows it to represent a decimal fractional component.
Read more >Documentation - Ethers.js
A Signer is a class which (usually) in some way directly or indirectly has access to a private key, which can sign messages...
Read more >Number.prototype.toFixed() - JavaScript - MDN Web Docs
The toFixed() method returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think there is some confusion as to what the
decimals
andformat
mean.The
decimals
indicates the number of decimals to assume the input value is operating under. For example,"10"
with0
decimals is10
, while"10"
with2
decimals is0.1
. It only impacts the interpretation of the value.The
format
parameter indicates the internal representation used during fixed-point mathematical operations.So, the default decimals of
0
and the default format offixed128x18
indicates that whatever value is passed in should be taken literally and that 18 decimal places are used during maths.So, the value
"123"
is represented internally as123.000_000_000_000_000_000
(underscores added for readability).Using a value
"123"
with a decimals of2
and the default format is represented internally as1.230_000_000_000_000_000
.Does that make sense?
Just discussing this with my partner, and this example came up, which might help explain where the logic and names stem from.
Hopefully that helps future peeps who happen across this issue too. 😃