JavaScript pi
Returns an approximation of pi
1 2 3 4 56 7 8 9 1011 | function pi () { // Returns an approximation of pi // // version: 1109.2015 // discuss at: http://phpjs.org/functions/pi // + original by: Onno Marsman // + improved by: dude // * example 1: pi(8723321.4); // * returns 1: 3.141592653589793 return 3.141592653589793; // Math.PI} |
Examples
Running
1 | pi(8723321.4); |
Should return
1 | 3.141592653589793 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have pi goodness in JavaScript.
My opinions:
1. We don't want to actually recreate PHP. We want to make PHP functions available for javascript, so a function that returns Pi, should return the javascript representation of Pi. The same goes for sqrt(2).
2. I doubt that an increase in precision will bother anybody, or that a decrease will help anybody.
3. There is a more obvious type representation difference between PHP and javascript that is bothering a lot of people: associative arrays. In JS we need objects to do this. I would like to make all array functions in a way they can handle these objects next to the normal arrays, but of course we'll never make a new Array object which handles associative arrays as well. And that's basically what you're asking here: to create a new Number type, which behaves differently than the JS native one.
Could we and would we want to change the outcome of something like 1+pow(10, -15) ? I think not. (I haven't tested this for differences but you'll get the idea)


Brett Zamir
25 Jul '11