Name: vi73552 Date: 03/16/99
Java 1.2 (and 1.1 versions) return really strange values of Math.sin(x) and Math.cos(x) when x is 2^63 or larger. In particular, they sometimes return x and sometimes return NaN. Which of these results occurs is unpredictable.
The following code demonstrates the problem:
import java.lang.Math;
public class sintest {
strictfp public static void main(String argv[]) {
double x;
for (int i=60; i<65; i++) {
x=Math.pow(2,i);
System.out.println("sin(2^"+i+") = " + Math.sin(x));
System.out.println("cos(2^"+i+") = " + Math.cos(x));
}
System.out.println();
for (int i=60; i<65; i++) {
x=Math.pow(2,i);
System.out.println("sin(2^"+i+") = " + Math.sin(x));
}
System.out.println();
for (int i=60; i<65; i++) {
x=Math.pow(2,i);
System.out.println("cos(2^"+i+") = " + Math.cos(x));
}
}
}
The above program produces this output when run on a Windows 95 machine with JDK 1.2-V:
sin(2^60) = -0.831474748109363
cos(2^60) = -0.555562546664614
sin(2^61) = 0.9238724570939123
cos(2^61) = -0.3827005134870572
sin(2^62) = -0.7071329274527789
cos(2^62) = -0.7070806339534855
sin(2^63) = NaN
cos(2^63) = 9.223372036854776E18
sin(2^64) = NaN
cos(2^64) = 1.8446744073709552E19
sin(2^60) = -0.831474748109363
sin(2^61) = 0.9238724570939123
sin(2^62) = -0.7071329274527789
sin(2^63) = 9.223372036854776E18
sin(2^64) = 1.8446744073709552E19
cos(2^60) = -0.555562546664614
cos(2^61) = -0.3827005134870572
cos(2^62) = -0.7070806339534855
cos(2^63) = 9.223372036854776E18
cos(2^64) = 1.8446744073709552E19
Note that for the same value of x, sometimes sin(x) returns a very large number (in fact, x) and sometimes sin(x) returns NaN.
The fact that unreliable values for sin() and cos() are returned are not (in some sense) as troubling as the fact that the values are not returned on the interval [-1,1].
(Review ID: 53997)
======================================================================