Comments (15)
This is because the internal word size is limited to 2^26
and as can be seen, it is stored directly, not splitting the number across multiple words.
This is clearly intended behaviour, I guess it might be up for discussion as to whether it is developer friendly though.
from bn.js.
@ryanxcharles I think I could support numbers up to 2^52 - 1
, but... is it really required? :)
from bn.js.
In order to make bn.js backwards-compatible with our old big number implementation in bitcore, we've had to work around this by converting Numbers into Strings before creating a new bn, which we do by using a different constructor. IMO the developer-friendly version would accept any integer as input, or at least throw an error when you have surpassed the internal word size.
from bn.js.
Ok... I think one more branch won't hurt.
from bn.js.
Fixed and published in 0.13.3, thanks! (I don't see any significant difference in benchmarks)
from bn.js.
Awesome, thanks.
from bn.js.
it is possible to extend this solution to support any numbers
from bn.js.
Yes, but... why? :) There is a precision loss after that boundary.
from bn.js.
I think, division by power of 2 and multiplication should be exact in case it is IEEE 754 binary, although, i am not an expert.
Anyway, just to complete number to bn conversion:
if (number < 0x4000000 * 0x4000000) {
// ...
} else {
number = Math.floor(number);
this.words = [];
while (number !== 0) {
var q = Math.floor(number / 0x4000000);
var digit = number - 0x4000000 * q;
this.words.push(digit);
number = q;
}
this.length = this.words.length;
}
from bn.js.
This will work, but the supplied number is not an integer anymore. See Number.MAX_SAFE_INTEGER
for details.
from bn.js.
@Yaffle also I do object increasing the number of branches in .init()
.
from bn.js.
@indutny , ok
actually, i was thinking about performance for small integers...
in dart and some other languages build-in integers allows to efficiently work with small numbers and switch to bigint, as described in https://www.dartlang.org/articles/numeric-computation/#integers
What do you think, when will this be possible in javascript?
i tried to use "Number.prototype.add" to implement something similar: (1).add(2).add(9007199254740991)
, but performance is not good in Chrome.
from bn.js.
@Yaffle I do exploit the integer representation in the JavaScript compilers heavily. There are usually two classes of numbers: SMIs (small 32bit integers) and Heap numbers (64bit floating point number). The bn.js mostly operates on 26bit SMIs, converting them to heap numbers only to perform multiplication. 26bit * 26bit multiplication results in 52bit numbers, which is exactly how much you could store in a IEE754 FP number without a precision loss.
from bn.js.
@indutny ,
var a = bn(1);
var b = bn(3);
var c = a.add(b);
can this code perform as fast, as with integers ?
var a = 1;
var b = 3;
var c = a + b;
in javascript there are no "value objects":
http://wiki.ecmascript.org/doku.php?id=strawman:value_objects
in javascript there are no built-in "bignum".
But there is "Number.prototype", which cannot give comparable performance do not know why...
from bn.js.
it certainly can't perform as fast. The whole point of this module is big integer arithmetics, not small ones.
from bn.js.
Related Issues (20)
- Easiest way to get percentage HOT 10
- muln with fractional number gives wrong result HOT 3
- Add method for "not equal"
- Assertion failure error message could be helpful
- Use bn.js instead of bignumber.js
- Incorrect multiplication result when using muln() HOT 1
- Converting json.stringified BN back into BN HOT 2
- use in web HOT 1
- TypeError: bn_js_1.default is not a constructor
- Add support for raising a `BN` object to the power of a plain JavaScript Number
- Function `mod` gives incorrect results when the arguments are of opposite signs
- setn function signature in index.tx doesn't correspond with the library HOT 1
- Which rounding method is used?
- Base64 encoding
- Wrong `cmpn` result HOT 1
- The unit of length in `toBuffer(endian, length)` is unclear
- Property 'divmod' does not exist on type 'BN'
- Add link to library this is based on to docs
- BN.muln with number bigger than 0x4000000 HOT 1
- I'm gonna find every one of you
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 bn.js.