Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naive fallback sqrt #90

Open
rinne opened this issue Jan 11, 2017 · 1 comment
Open

Naive fallback sqrt #90

rinne opened this issue Jan 11, 2017 · 1 comment

Comments

@rinne
Copy link

rinne commented Jan 11, 2017

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 <[email protected]>

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
Copy link
Author

rinne commented Jan 11, 2017

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 <[email protected]>

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant