Use PHP functions in JavaScript

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}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Brett Zamir
25 Jul '11 Permalink

q  @dude: OK!!!! :) (I also applied in Git to some other functions using PI or LN10.)

Gravatar
dude
30 Jun '11 Permalink

q  just go with "return 3.141592653589793" BECAUSE IT NEVER CHANGES!!!!

Gravatar
Onno Marsman
15 Oct '08 Permalink

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

Gravatar
Philip Peterson
14 Oct '08 Permalink

q  I guess similarly, shouldn't all numbers (e.g. square roots) be truncated to 14 digits?

Gravatar
Philip Peterson
14 Oct '08 Permalink

q  Since PHP defaults to float 14, shouldn't we use the PHP-standard value of pi, 3.1415926535898 ?


Contribute a New function

More functions

In this category

abs
acos
acosh
asin
asinh
atan
atan2
atanh
base_convert
bindec
ceil
cos
cosh
decbin
dechex
decoct
deg2rad
exp
expm1
floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
log
log10
log1p
max
min
mt_getrandmax
mt_rand
octdec
» pi
pow
rad2deg
rand
round
sin
sinh
sqrt
tan
tanh

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !