# Thread: Pi and the standard library

1. ## Pi and the standard library

While I guess Pi is not explicitly shown in the math library, its trig functions work in radians and so the value of Pi is somewhat important to know. By playing with some of the trig functions, I found that the value it uses for Pi is one or two significant digits less than I expected, at least with my compiler. A trivial matter I suppose, but when my function figures out which side of a line a point is on, the answer might be different for certain situations if the value for Pi is different.

My question is this, "Will the different C/C++ compilers for the 32bit windows all use the same values for the standard library?" Or would I need to do some testing if I use diferent compilers?

2. It could very well be different across compilers. Looking at my gcc's math.h I found this definition:
Code:
`# define M_PIl          3.1415926535897932384626433832795029L  /* pi */`

3. Since NO machine will be able to accurately store PI in any finite representation, then I guess you'll need another approach.

floats and doubles are approximations, so you need to program accordingly.

4. Would there be a good way for the preprocessor to take care of it? You know, some thing like

#define Pi (asin(...something...))

Or would that just plugin in the expression everywhere instead of evaluating it to a constant?

5. It doesn't matter. Those functions you're using to define PI would only return approximations of whatever number they're returning also. The simple fact is that a CPU cannot represent an irrational number with 100% accuracy. PI to about the 20th place isn't good enough for you?

6. 15 digits of precision is greater than any measurement humans make accurately. That's what a double provides in most C implementations.

If you have to have precision beyond that use long doubles or use a BCD library which can represent numbers more accurately than floating point.

7. The idea for me was to have a consistant answer generated from my function with different compilers. I determine which side of a line the point lands by determining which half of the circle it is in, or if it lands on the line. For this, you have to know what the trig function returns for 180 degrees. It's not so much the accuracy or precision of Pi so much as how the trig function is thinking.

8. So you make the "line" part of one hemisphere or the other.
Then you can do reasonable floating point comparisons like
x < 0.0
x >= 0.0

> or if it lands on the line
Or you decide that within a given error, values are considered "on the line".
http://cboard.cprogramming.com/showt...ht=approxequal

Read Goldberg's paper on floating point before trying to implement something.