java. lang.Math. Is pi equal to m of GCC_ PI?
I have written several reference algorithms in Java and C / C + + Some of these algorithms use π I want two implementations of each algorithm to produce the same result without different rounding One approach that has worked so far is to use custom PI constants, which are identical in both languages, such as 3.14159 However, when high - precision constants are already defined in Java and GCC libraries, it is foolish to define PI
I spent some time writing a quick test program, looking at the documentation of each library, and reading floating point types But I can't convince myself lang.Math. PI (or Java. Lang. strictmath. PI) in math Is h equal to m_ PI.
GCC 3.4. 4(cygwin)math. H includes:
#define M_PI 3.14159265358979323846 ^^^^^
But this
printf("%.20f",M_PI);
produce
3.14159265358979311600 ^^^^^
This indicates that the last five digits cannot be trusted
Meanwhile, JavaDocs says Java lang.Math. PI is:
and
public static final double PI 3.141592653589793d
The last five suspicious digits are omitted from the constant
System.out.printf("%.20f\n",Math.PI);
produce
3.14159265358979300000 ^^^^^
If you have some expertise in floating-point data types, can you convince me that these library constants are exactly equal? Or are they definitely unequal?
Solution
Please note the following
These two numbers are the same, 16 digits after the decimal point This is almost the same as 48 bits
In IEEE 64 bit floating point numbers, all bits are not symbols or exponents
#define M_ PI has 21 digits; This is approximately 63 bit precision, which is advantageous for IEEE 80 bit floating point values
I think what you see is m_ Normal truncation of bits in PI values