Why do I get a different result for pi then what is listed in math.h?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Andrews
    New Member
    • Dec 2010
    • 1

    Why do I get a different result for pi then what is listed in math.h?

    Math.h defines pi as:
    #define M_PI 3.1415926535897 9323846

    Yet I get the fallowing result with the following source.

    C:\BC5\BIN>read s pia.c
    #include <stdio.h>
    #include <math.h>

    int main(void)
    {

    printf("Pi to 32 places is %.31f\n", M_PI);
    return 0;
    }

    C:\BC5\BIN>pia
    Pi to 32 places is 3.1415926535897 931200000000000 000

    C:\BC5\BIN>
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It makes no difference how many decimals are in the #define.

    The value has to be converted to a double so you are limited by the number of decimals available in a double.

    Since the #define has a value that's too long for a double so you get odd results for the part that doesn't fit.

    Comment

    Working...