Patricia said:
In this case the difference is most likely in the reduction step - if it
had produced 0, the sin result would have been 0.
I guess 2*pi is "exact" in the sense that the operation * itself
has no loss, since we only need to adjust the exponent.
I forget to mention, we also get a non-zero value for sin(pi)
in Java, and not only for sin(2*pi).
Easiest reduction is sin(x) = cos(x-pi/2). But this reduction
seems not to be used:
double x=Math.PI;
System.out.println("pi="+x+", sin(pi)="+Math.sin(x));
x=Math.PI-Math.PI/2;
System.out.println("pi-pi/2="+x+", cos(pi-pi/2)="+Math.cos(x));
x=Math.PI/2;
System.out.println("pi/2="+x+", cos(pi/2)="+Math.cos(x));
Gives:
pi=3.141592653589793, sin(pi)=1.2246467991473532E-16
pi/2=1.5707963267948966, cos(pi/2)=6.123233995736766E-17
pi-pi/2=1.5707963267948966, cos(pi-pi/2)=6.123233995736766E-17
So it seems that the error in PI representation seems the
only reason to be not to return zero. So we have sin(x) with
x approximating pi. The error is:
sin(x) - sin(pi) = sin(pi+(x-pi)) - 0.
= - sin(x-pi)
~ - (x - pi)
= pi - x
So I guess the PI representation is below the real pi. Since
we get a positive result for sin(x). Lets check:
Java PI:
3.141592653589793
real pi:
3.141592653589793238462643383...
Difference:
0.000000000000000238462643383...
Well the above idea does also not work, we only get:
x=0.000000000000000238462643383;
System.out.println("PI-pi="+x+", sin(PI-pi)="+Math.sin(x));
PI-pi=2.38462643383E-16, sin(PI-pi)=2.38462643383E-16
But not 1.2246467991473532E-16. But we can try a fully exact decimal
development of the approximate machine PI. Using the double to
BigDecimal conversion we get (a thing I like most with the
BigDecimal package):
System.out.println("PI="+new BigDecimal(Math.PI));
PI=3.141592653589793115997963468544185161590576171875
Therefore:
Java PI:
3.141592653589793115997963468...
real pi:
3.141592653589793238462643383...
Difference:
0.000000000000000122464679915...
Everything fits perfectly.
Question is why Go gets zero?
Bye
Small-angle Approximation taken from here:
http://en.wikipedia.org/wiki/Small-angle_approximation
Additional PI Digits taken from here:
http://en.wikipedia.org/wiki/Pi