Thread: Pi and the standard library

  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,218
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  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,218
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    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.

Popular pages Recent additions subscribe to a feed