vukicevic / crunch Goto Github PK
View Code? Open in Web Editor NEWJavaScript arbitrary-precision arithmetic library. Built for speed.
Home Page: http://crunch.js.org
License: MIT License
JavaScript arbitrary-precision arithmetic library. Built for speed.
Home Page: http://crunch.js.org
License: MIT License
var crunch = new Crunch();
var a = crunch.parse((Math.pow(2, 27) + 1).toString());
var b = crunch.parse(Math.pow(2, 27 + 28).toString());
var c = crunch.div(a, b);
console.log(c); // [1], should be [0]
P.S.
https://github.com/vukicevic/crunch/blob/master/crunch.js#L411
d = u.length - v.length;
It seems, some code below this line should not be executed when d < 0.
C:\Users\Sam\Documents\GitHub\Project\node_modules\number-crunch\crunch.js:805
return rawIn ? a : Array.prototype.slice.call(a).map(function (v) { return ci(v.slice(1)) });
^
TypeError: v.slice is not a function
at C:\Users\Sam\Documents\GitHub\Project\node_modules\number-crunch\crunch.js:805:85
at Array.map (native)
at transformIn (C:\Users\Sam\Documents\GitHub\Project\node_modules\number-crunch\crunch.js:805:54)
at Object.Crunch.nextPrime (C:\Users\Sam\Documents\GitHub\Project\node_modules\number-crunch\crunch.js:1115:25)
at generateKey (C:\Users\Sam\Documents\GitHub\Project\keygen.js:46:14)
at Object. (C:\Users\Sam\Documents\GitHub\Project\keygen.js:7:2)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
the while loop should have a > 0
thx
Meno
function rsh (x, s) {
var ss = s % 28,
ls = Math.floor(s/28),
l = x.length - ls,
z = x.slice(0,l);
if (ss) {
while (--l > 0) {
z[l] = ((z[l] >> ss) | (z[l-1] << (28-ss))) & 268435455;
}
z[l] = z[l] >> ss;
if (z[0] === 0) {
z.shift();
}
}
Related to the not catching the problems in bug #16, the negative values for compare really aren't negative in the web site test suite at http://crunch.js.org/tests/ . For example:
var x = [5, 10],
y = [-5, 10];
crunch.compare(x, y).should.equal(1);
The problem is that crunch.compare treats all input as raw, and in raw mode you have to set y.negative = true. It's just a wild coincidence that the existing tests pass. If it is change to to following, it fails but it shouldn't.
var x = [5, 10],
y = [-5, 10, 1];
crunch.compare(x, y).should.equal(1);
This is because cmp doesn't see y.negative, so it is treated as a positive (greater than x). (The test suite really needs some more test cases.)
Crunch.compare doesn't use transformIn like all the other API methods.
Line 1103 in 594aec8
Should it?
Most JS bigint libraries forget about this.
crunch.compare(crunch.parse("2"), crunch.parse("-100")) // -> 1
crunch.compare(crunch.parse("2"), crunch.parse("-1000")) // -> -1
crunch.compare(crunch.parse("-256"), crunch.parse("-65535")) // -> 1
crunch.compare(crunch.parse("-256"), crunch.parse("-65536")) // -> -1
It seems that any multi-byte negative representation is treated as greater than any single-byte representation, and the same for other differing sizes.
Hello, I am creating a benchmark for arbitrary-precision integer libs in js, I have found some issues with your lib:
crunch.parse("-256")
gives [1, 0]
, not [-1, 0]
- how to parse negative numbers?
crunch.mod([-4], [3])
gives [-4]
, not [-1]
crunch.leftShift([-3], 8)
gives [3, 0]
, not [-3, 0]
crunch.rightShift([-3], 8)
gives [0]
, not [-1]
crunch.leftShift([1, 0, 0, 0, 0], 1)
gives [0]
, not [2, 0, 0, 0, 0]
Seems next change should fix this
// https://github.com/vukicevic/crunch/blob/master/crunch.js#L979
- return transformOut(lsh(transformIn([x]), s));
+ return transformOut(lsh(transformIn([x])[0], s));
// https://github.com/vukicevic/crunch/blob/master/crunch.js#L991
- return transformOut(rsh(transformIn([x]), s));
+ return transformOut(rsh(transformIn([x])[0], s));
Doing something like:
var crunch = new Crunch();
var x = crunch.parse("-123456789");
var z = crunch.stringify(x);
causes the last line to go into an infinite loop.
crunch.stringify(crunch.div(crunch.parse("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), crunch.parse("1234567890")));
should return 10000000001000000000100000000010000000001000000000100000000010000000001
, returns 10000000001000000000100000000010000000001000000000100000000010000000000
similar to silentmatt/javascript-biginteger#18
var crunch = new Crunch();
var a = crunch.parse("-9999999999999999");
var b = crunch.parse("0");
var c = crunch.compare(a, b);
console.log(c); // 1, should be -1
P.S.
May be transformIn
is needed for compare
.
crunch.nextPrime ([1]) returns 3 instead of 2.
crunch.nextPrime([1]);
[3]
0: 3
length: 1
proto: Array(0)
Would it be possible to pass primitive number or integer or numeric string as method parameter?
If I pass numeric literals to any crunch method, either it errors out or gives weird result.
For eg:
var y = crunch.add ([25] , 20) ;
Uncaught TypeError: v.slice is not a function
crunch.add ([25] , "20") ;
[0]
0: 0
length: 1
proto: Array(0)
can we mix and match Interger, Number, Numeric strung, & crunch number etc? otherwise, it becomes very difficult to use Crunch as it is very tedious to convert each and every parameters using crunch.parse() at some point in the application..
nextPrime x First prime after x
> require('number-crunch').nextPrime(3)
[ 3 ]
I expect nextPrime(3)
to return 5.
I'm curious why you don't use typed Arrays when they are available. They should be a bit faster.
var crunch = new Crunch();
var a = crunch.parse("-10");
var b = crunch.parse("10");
var c = crunch.div(a, b);
console.log(c); // [-0], should be [-1]
P.S.
https://github.com/vukicevic/crunch/blob/master/crunch.js#L417 - cmp(u, k)
should compare absolute values, may be
var crunch = new Crunch();
var a = crunch.parse("-999999999");
var b = crunch.parse("999999998");
var c = crunch.div(a, b);
var s = crunch.stringify(c);
console.log(s);// "0", should be "-1"
var crunch = new Crunch();
var a = crunch.parse("2");
var b = crunch.parse("-2");
var c = crunch.div(a, b);
var s = crunch.stringify(c);
console.log(s);// "1", should be "-1"
var crunch = new Crunch();
var a = crunch.parse("67108855");
var b = crunch.parse("-9007199254740983");
var c = crunch.mod(a, b);
var s = crunch.stringify(c);
console.log(s);// should return 67108855
the following test fails on both your current version and my local version.
var c=Crunch(true,true);
c.div([7425251],[3725290, 80117760])
//returns [1]
btw i've tried to modify it like this:
//yt = v[0]*268435456 + v[1];
yt = [v[0], v[1]];
//xt = u[i-1]*72057594037927936 + u[i]*268435456 + u[i+1];
xt = [u[i-1], u[i], u[i+1]];
//while (q[i]*yt > xt) { //condition check can fail due to precision problem at 28-bit
while (cmp(mul([q[i]], yt), xt) === 1) { //condition check can fail due to precision problem at 28-bit
this seems to solve the case at #13, but doesn't help with this one.
sorry i can't fully understand the div code. only i know is that it runs into the loop commented "only cmp as last resort" and q[0]++ which should not happen. maybe problem with the loop condition.
hope this helps.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.