-
Notifications
You must be signed in to change notification settings - Fork 5.1k
BigNumber.js -> bn.js #102
Conversation
@@ -77,7 +81,7 @@ var formatInputBool = function (value) { | |||
/// Values are multiplied by 2^m and encoded as integers | |||
/// @returns byte representation of real | |||
var formatInputReal = function (value) { | |||
return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128))); | |||
return formatInputInt(new BigNumber(value).mul(new BigNumber(2).toRed(BigNumber.red('k256')).redPow(new BigNumber(128)).fromRed())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is k256 ok for the max int? Do you have a max int that ethereum.js can handle defined somewhere?
so BigNumber.js was accepting floats on input. bn.js is not (or I do not know how to do this properly) |
@debris i haven't went through the codebase to see where |
floats assert.equal(parser.test([2.125]), "0000000000000000000000000000000220000000000000000000000000000000");
assert.equal(parser.test([8.5]), "0000000000000000000000000000000880000000000000000000000000000000"); |
Travis shows only linting errors. |
|
||
if (value.lessThan(0)) | ||
value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1); | ||
if (value.cmpn(0) === -1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be value.cmpn(new BN(0))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right, I will fix this
@debris on the floats issue BN will always around down. So Just multiply by how many decimal places of accuracy you need. Make sense? |
@wanderer I tried this idea, it breaks always somewhere like when you want to convert 1111111111111111111 wei into ether for instance or for any units that are 'far' from each other and require the numbers to be expressed in BN. That should be 1.1 and BN as far as I see cannot support decimal. The only way I found (still not acceptable) is to do this:
The first block is what is done today and it works fine. This is the non decimal part. @debris did you progress on the topic? I don´t really see how to solve that. |
@chevdor if you are converting units around you probably don't need bignum at all. Thats should be easy todo with pure string manipulation since all the operations result in a simple decimal shift. |
Hello @wanderer, If you take units that are close to each other (ie wei and Kwei), I am totally with you and the "limited solution I explained" works. However, if you take units far away from each other, then it depends if number a > number b. If the div is a large number, then the BN lib does the trick very well. The other way around is the problem. I think the main question is: do we want to keep some of the methods and keep them accurate. I see neither a way nor a need to express 1 wei into Mether (for instance). Stephan - I think - was suggesting to stay in wei as much as possible (= all the time) for the calculations (and using BN then works all fine) and just making conversion (with loss) for the user and only for display purposes. In short I would be fine saying that 1 wei = 0 Mether instead of 0.000....001 Mether. I think there is an important decision to make here. Once everyone agrees, we can all move forward but I feel like @debris at the moment. I try to solve the problem he mentioned on my side and I get to the same issues he explained. My take: remove functions and tests that end up with decimal BN (since that does not exist) and aimed at anything else than display. |
temporary closing |
I'm trying to migrate from BigNumber.js to bn.js, but I'm having troubles with 4 tests (formatting real numbers)
@ethers @wanderer can you help me? Check failing tests on travis.
fixes #99