JDK-4220710 : Java 1.2 trig functions mess up on big values
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_98
  • CPU: x86
  • Submitted: 1999-03-16
  • Updated: 2001-06-21
  • Resolved: 2001-06-21
Related Reports
Duplicate :  
Relates :  
Description

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) 
======================================================================

Comments
EVALUATION This bug was a consequence of trying to use the raw x86 fsin and fcos instructions to implement Java's Math.{sin, cos} methods. Unassisted fsin and fcos cannot be for this purpose due to limited range (abs(x) < 2^63) and suboptimal argument reduction. However, these issues have been fully addressed in Merlin. See bugs 4345903 and 4345910 for a full explanation of the problem and how it can be solved. joe.darcy@eng 2001-06-21
21-06-2001