Code Monkey home page Code Monkey logo

crunch's People

Contributors

jefft0 avatar swenson avatar vukicevic avatar yaffle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

crunch's Issues

a bug in `div`

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.

v.slice is not a function Line:805

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)

rightShift would not stop

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();
      }
    }

Web site test suite negative values for compare really aren't negative

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.

crunch/crunch.js

Line 1103 in 594aec8

return cmp(x, y);

Should it?

Comparison operation inconsistent when byte-lengths differ

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.

Some issues

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));

bug in division

crunch.stringify(crunch.div(crunch.parse("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), crunch.parse("1234567890")));

should return 10000000001000000000100000000010000000001000000000100000000010000000001, returns 10000000001000000000100000000010000000001000000000100000000010000000000

similar to silentmatt/javascript-biginteger#18

a bug in `compare` with a negative argument

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.

feature request:: Would it be possible to pass primitive number or integer or numeric string as method parameter?

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 fails for 3

nextPrime x First prime after x

> require('number-crunch').nextPrime(3)
[ 3 ]

I expect nextPrime(3) to return 5.

typed arrays

I'm curious why you don't use typed Arrays when they are available. They should be a bit faster.

Some bugs with division

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

critical problem with div

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.

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.