# Thread: Wrong value for cos(pi/2)

1. ## Wrong value for cos(pi/2)

Gentlemen,

I´m developing a 3D algorithm and I need to evaluate cos(pi/2).

I´m using the a constant defined by M_PI_2 as the argument above.

While debugging my code, I notice that evaluating cos(M_PI_2) returns a value that is "almost" zero: 6.32...e-17.

In this situation, the incorrect value I ended up with is being propagate through my code and I´m facing with inconsistent results.

Does anyone know how to deal with this? Limiting float or double precision...

Need some help. Thanks.

2. First, why do you need to calculate cos(pi/2) if you know it is 0?

Second, you could create a lookup-table for such known values. Lookup tables are very handy and fast.

3. Ok...Ok...I certanly agree but...

I´m using a C function to rotate a point and the angle (in radians) to be used by the cos() function is an argument of this function.

I know that I could state case or if statements to treat known values.
I already did this, but, believe me, the result is still no good! (Don´t know why...sorry :-) )

BTW, if we all know that cos(pi/2) is 0.0 shouldn´t it be returned by the cos() function?
(If you evaluate sin(pi/2) the function returns 1.0!!)

Can you figure out any other recommendations for this?

4. The computer computes cos(phi) by using series. If it would compute the result exactly, then it should compute an infinite series. Since this is impossible, it stops after a certain number of terms. This results in a non-exact result.

5. Thank you anyway, Shiro!

Probably, I´ll modify my code using a table as you suggested. Tanx.

Can you point me how can I define how many precision digits for float-point operations?
(Not for printing purposes, I know how to do it. I need it for computing values at most with 10 digits, for example.)

I searched many places, but couldn´t find none...

6. You could define a cosine table with 10 digits. When a value is needed that is not defined in the table, you could use interpolation.

Google gives a lot on results when searching for "high precision float computing cosine". Perhaps it's an idea to check that, but I guess you've already done.

I don't know how in scientific applications such is being done. But I have an idea, but it is not fast, just an idea.

Create a string to contain floating point values, for example:

a = "4.342342325345346364746745674672366453534"

And define your own functions to handle such string-values.

7. Found some libraries that might be useful when programming with high precision values.

http://netlib.org/cephes/
http://www.nersc.gov/~dhbailey/mpdist/mpdist.html