justmoon / node-bignum

Big integers for Node.js using OpenSSL
419 stars 116 forks source link

Naive fallback sqrt #90

Open rinne opened 7 years ago

rinne commented 7 years ago

Hi

Square root not supported by OpenSSL is a bit annoying but having the dummy interface that doesn't do anything else than print an error message is quite useless. Here is a dummy Newton-like implementation of sqrt that you can, if you wish include into your package. The idea being that for as log as the OpenSSL can't help in making this calculation, fallback code would be used, but once the library support mystically appears, it can be used instead and everything would still work as expected. It is also pretty simple to implement root() interface in the same way, but since I don't need it just now, I won't do it. (I can do it, if someone is interested).

Here is the code as a main level function and it of course has to go inside the class method, but can't find time to do that now.

// Naive truncating square root implementation for
// https://github.com/justmoon/node-bignum/
// Signed-off by: Timo J. Rinne <tri@iki.fi>

const bignum = require('bignum');

function sqrt(n) {
    if (bignum.isBigNum(n)) {
        /*NOTHING*/
    } else if (Number.isSafeInteger(n)) {
        n = bignum(n);
    } else if ((typeof(n) === 'string') && n.match(/^(0|-?[1-9][0-9]*)$/)) {
        n = bignum(n);
    } else {
        return undefined;
    }
    if (n.eq(0)) {
        return bignum(0);
    }
    if (n.lt(0)) {
        return undefined;
    }
    var M = n, m = bignum(1);
    while (true) {
        var d = M.add(m).shiftRight(1);
        if (d.mul(d).gt(n)) {
            if (d.eq(M)) {
                return d;
            }
            M = d;
        } else {
            if (d.eq(m)) {
                return d;
            }
            m = d;
        }
    }
}
rinne commented 7 years ago

And here is the root() implementation to the same bunch because it's so trivial. I didn't test it too well, but it seems to be ok.

// Naive truncating nth-root implementation for
// https://github.com/justmoon/node-bignum/
// Signed-off by: Timo J. Rinne <tri@iki.fi>

function root(n, o) {
    var negative = false;
    if (bignum.isBigNum(n)) {
    /*NOTHING*/
    } else if (Number.isSafeInteger(n)) {
    n = bignum(n);
    } else if ((typeof(n) === 'string') && n.match(/^(0|-?[1-9][0-9]*)$/)) {
    n = bignum(n);
    } else {
    return undefined;
    }
    if (bignum.isBigNum(o)) {
    /*NOTHING*/
    } else if (Number.isSafeInteger(o)) {
    o = bignum(o);
    } else if ((typeof(o) === 'string') && o.match(/^(0|-?[1-9][0-9]*)$/)) {
    o = bignum(o);
    } else {
    return undefined;
    }
    if (n.eq(0)) {
    return bignum(0);
    }
    if (o.eq(0)) {
    return bignum(1);
    }
    if (o.lt(0)) {
    return undefined;
    }
    if (n.lt(0)) {
    if (o.mod(2).eq(0)) {
        return undefined;
    }
    negative = true;
    n = n.neg();
    }
    var M = n, m = bignum(1);
    while (true) {
    var d = M.add(m).shiftRight(1);
    if (d.pow(o).gt(n)) {
        if (d.eq(M)) {
        return negative ? d.neg() : d;
        }
        M = d;
    } else {
        if (d.eq(m)) {
        return negative ? d.neg() : d;
        }
        m = d;
    }
    }
}