1

I know two ways of calculating pi in code. Either

pi = 4.0 * atan(1.0)

or

pi = acos(-1.0)

What is the benefit of one vs. the other? I know there are some languages that have a built in pi representation, but this is not asking about them. Also, are there any other common ways of calculating pi as well as how they compare to others?

1
  • 1
    what are you trying to compare? Accuracy? Speed? Commented May 30, 2017 at 22:17

1 Answer 1

2

What is the benefit of one vs. the other?

These functions were not designed only to approximate the value of π. In its case, I see no significant speed.

As a matter of fact, I did a experiment on my system, where I see the same approximated value of pi with both functions, and the same speed, more or less.

Georgioss-MacBook-Pro:~ gsamaras$ g++ -std=c++0x -Wall -O3 atan.cpp Georgioss-MacBook-Pro:~ gsamaras$ ./a.out It took me on average 1.69e-13 seconds. 3.14159265358979312 Georgioss-MacBook-Pro:~ gsamaras$ g++ -std=c++0x -Wall -O3 acos.cpp Georgioss-MacBook-Pro:~ gsamaras$ ./a.out It took me on average 1.7e-13 seconds. 3.14159265358979312 

The code computes π in a loop, and adds the loop's counter to it, making sure that the compiler won't optimize that away (same value at every iteration):

Georgioss-MacBook-Pro:~ gsamaras$ cat acos.cpp #include <iostream> #include <ctime> #include <ratio> #include <chrono> #include <cmath> #include <limits> #define ITER 1000000 int main () { using namespace std::chrono; high_resolution_clock::time_point t1 = high_resolution_clock::now(); for(int i = 0; i < ITER; ++i) { auto pi = acos(-1.0) + i; pi += i + i; } high_resolution_clock::time_point t2 = high_resolution_clock::now(); duration<double> time_span = duration_cast<duration<double>>(t2 - t1); std::cout << "It took me on average " << time_span.count()/(double)ITER << " seconds."; std::cout << std::endl; auto pi = acos(-1.0); std::cout.precision(std::numeric_limits< double >::max_digits10); std::cout << std::fixed << pi << std::endl; return 0; } 

based on my Time measurements (C++). The atan() code is the same, just the function changes.

Also, are there any other common ways of calculating pi as well as how they compare to others?

There are many other ways of approximating π and comparing them all is just too broad. For example, Plato has approximated π like this.

Sign up to request clarification or add additional context in comments.

3 Comments

The Indiana state legislature decided to approximate pi as 3.2: en.wikipedia.org/wiki/Indiana_Pi_Bill
FTR, your wording looks wrong to me. These "functions" were not "designed" to "approximate" the value of Pi. In math sense, values of both acos(-1) and 4 * atan(1) are Pi, also coincidentally being equal. The same might be said about 2 * asin(1) and many other expressions.
@duffymo interesting, hadn't heard of it! iehrlich, I said not only. However, I am afraid I am that I might be a victim of the poorness of English.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.