Wrong value for cos(pi/2)

This is a discussion on Wrong value for cos(pi/2) within the C Programming forums, part of the General Programming Boards category; Gentlemen, Im developing a 3D algorithm and I need to evaluate cos(pi/2). Im using the a constant defined by M_PI_2 ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question Wrong value for cos(pi/2)

    Gentlemen,

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

    Im 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 Im facing with inconsistent results.

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

    Need some help. Thanks.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Ok...Ok...I certanly agree but...

    Im 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! (Dont know why...sorry :-) )

    BTW, if we all know that cos(pi/2) is 0.0 shouldnt 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. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Thank you anyway, Shiro!

    Probably, Ill 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 couldnt find none...

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21