Giter Site home page Giter Site logo

Comments (15)

dcousens avatar dcousens commented on July 27, 2024

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.

indutny avatar indutny commented on July 27, 2024

@ryanxcharles I think I could support numbers up to 2^52 - 1, but... is it really required? :)

from bn.js.

ryanxcharles avatar ryanxcharles commented on July 27, 2024

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.

indutny avatar indutny commented on July 27, 2024

Ok... I think one more branch won't hurt.

from bn.js.

indutny avatar indutny commented on July 27, 2024

Fixed and published in 0.13.3, thanks! (I don't see any significant difference in benchmarks)

from bn.js.

ryanxcharles avatar ryanxcharles commented on July 27, 2024

Awesome, thanks.

from bn.js.

Yaffle avatar Yaffle commented on July 27, 2024

it is possible to extend this solution to support any numbers

from bn.js.

indutny avatar indutny commented on July 27, 2024

Yes, but... why? :) There is a precision loss after that boundary.

from bn.js.

Yaffle avatar Yaffle commented on July 27, 2024

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.

indutny avatar indutny commented on July 27, 2024

This will work, but the supplied number is not an integer anymore. See Number.MAX_SAFE_INTEGER for details.

from bn.js.

indutny avatar indutny commented on July 27, 2024

@Yaffle also I do object increasing the number of branches in .init().

from bn.js.

Yaffle avatar Yaffle commented on July 27, 2024

@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.

indutny avatar indutny commented on July 27, 2024

@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.

Yaffle avatar Yaffle commented on July 27, 2024

@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.

indutny avatar indutny commented on July 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.