Pi and the standard library

This is a discussion on Pi and the standard library within the C Programming forums, part of the General Programming Boards category; While I guess Pi is not explicitly shown in the math library, its trig functions work in radians and so ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    51

    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?

    Thanks in advance

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    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 */
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    51
    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. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    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?
    If you understand what you're doing, you're not learning anything.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    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. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    51
    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. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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".
    Program stuck in infinite loop-->PLEASE HELP

    Read Goldberg's paper on floating point before trying to implement something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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