Ano-Tech Computers
Enter keyword:

Programming: How to calculate pi
Problem:
Not every programming environment has a constant for pi defined. How can this value be calculated?
 
Solution:
If you have functions for basic trigonometry available, you can use the following formula:

pi = 4 * atan(1.0)

The atan() function returns the arc tangent in radians and the value is mathematically defined to be between -PI/2 and PI/2 (inclusive).

See "man 3 atan" for more info.
 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles