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

Calculating PI to x DP #13

Closed
Xotic750 opened this issue Sep 14, 2013 · 2 comments
Closed

Calculating PI to x DP #13

Xotic750 opened this issue Sep 14, 2013 · 2 comments

Comments

@Xotic750
Copy link
Contributor

Hi!

Would you be interested in a function like this?

/*jslint maxerr: 50, indent: 4, browser: true, white: true, devel: true */
/*global BigNumber */

(function() {
    "use strict";

    function pi(digits) {
        digits = Math.floor(digits);
        var config = BigNumber.config(),
            previousConfig = {},
            k = 0,
            prop,
            sum,
            ta,
            tb,
            divisor,
            a,
            b;

        for (prop in config) {
            if (config.hasOwnProperty(prop)) {
                previousConfig[prop] = config[prop];
            }
        }

        BigNumber.config({
            DECIMAL_PLACES : digits,
            ROUNDING_MODE : 4,
            EXPONENTIAL_AT : [-7, 20],
            RANGE : [-1000000000, 1000000000],
            ERRORS : true
        });

        sum = new BigNumber(0);
        a = ta = new BigNumber(16).div(5);
        b = tb = new BigNumber(-4).div(239);
        while (!a.equals(b)) {
            divisor = 2 * k + 1;
            a = ta.div(divisor);
            b = tb.div(divisor);
            sum = sum.plus(a).plus(b);
            ta = ta.neg().div(25);
            tb = tb.neg().div(57121);
            k += 1;
        }

        for (prop in previousConfig) {
            if (previousConfig.hasOwnProperty(prop)) {
                config[prop] = previousConfig[prop];
            }
        }

        return sum;
    }

    console.log(pi(20).toString());
}());

jsfiddle

@MikeMcl
Copy link
Owner

MikeMcl commented Sep 16, 2013

Hello
I'm not looking to add a pi calculation function to the library at this point, but some visitors may find it useful.
I tried it out and it seems to work okay apart from the fact that the last digit is sometimes incorrect, e.g. with pi(1) and pi(100).
To avoid this the DECIMAL_PLACES value could be increased to say digits + 2 for the calculation and then sum could be round to digits before its return.
I am not clear why you are storing and setting all the config properties, surely it's only the DECIMAL_PLACES value that needs to be stored, set and then restored.
Also, it looks like you are using the Machin formula and there are others that are considerably faster, for example see http://beej.us/blog/data/pi-chudnovsky-gmp/.

@Xotic750
Copy link
Contributor Author

I was storing and restoring because in some of the testing I was changing
these and found that if you change the rounding scheme then it causes the
PI calculation to go into an infinite loop. I haven't fully checked as to
why yet, just wanted to see if it would of any interest wrt the library.
The last digit is being rounded and perhaps it should be truncated instead,
something I will need to think about a little. I will take a look at the
link, thanks.

On 16 September 2013 21:04, Michael Mclaughlin notifications@github.comwrote:

Hello
I'm not looking to add a pi calculation function to the library at this
point, but some visitors may find it useful.
I tried it out and it seems to work okay apart from the fact that the last
digit is sometimes incorrect, e.g. with pi(1) and pi(100).
To avoid this the DECIMAL_PLACES value could be increased to say digits +
2 for the calculation and then sum could be round to digits before its
return.
I am not clear why you are storing and setting all the config properties,
surely it's only the DECIMAL_PLACES value that needs to be stored, set
and then restored.
It looks like you are using the Machin formula and there are others that
are considerably faster, for example see
http://beej.us/blog/data/pi-chudnovsky-gmp/.


Reply to this email directly or view it on GitHubhttps://github.com//issues/13#issuecomment-24535467
.

@MikeMcl MikeMcl closed this as completed Sep 17, 2013
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

2 participants