I encode several reference algorithms in both Java and C / C ++. Some of these algorithms use & pi ;. I would like for two implementations of each algorithm to get identical results , without rounding differently. One way to do this, which has worked so far, is to use a custom constant pi, which in both languages is exactly the same as 3.14159. However, it seems silly to me to define pi when there are already high-precision constants defined both in Java libraries and in GCC.
I spent some time creating quick test programs, looking at the documentation for each library, and reading on floating point types. But I could not convince myself that java.lang.Math.PI (or java.lang.StrictMath.PI) is equal or not equal to M_PI in math.h.
GCC 3.4.4 (cygwin) math.h contains:
#define M_PI 3.14159265358979323846
^^^^^
but this one
printf("%.20f", M_PI);
produces
3.14159265358979311600
^^^^^
which suggests that the last 5 digits cannot be trusted.
Meanwhile, Javadocs says java.lang.Math.PI:
A value doublethat is closer than any other to pi is the ratio of the circumference of a circle to its diameter.
and
public static final double PI 3.141592653589793d
which omits the dubious last five digits of the constant.
System.out.printf("%.20f\n", Math.PI);
produces
3.14159265358979300000
^^^^^
If you have experience with floating point data types, can you convince me that these library constants are exactly equal? Or that they are definitely not equal?