[754] in java-interest

home help back first fref pref prev next nref lref last post

Re: trig precision problems?

daemon@ATHENA.MIT.EDU (Richard Tuck)
Tue Jul 18 20:59:02 1995

Date: Tue, 18 Jul 1995 13:28:50 +0800
From: rt@scndprsn.Eng.Sun.COM (Richard Tuck)
To: java-interest@java.Eng.Sun.COM, misfeldt@mda.ca


         From: misfeldt@mda.ca (Trevor Misfeldt)
         To: java-interest@java.Eng.Sun.COM
         Subject: trig precision problems?
         
         
         In porting C++ programs to java, I came across a
         problem in the accuracy of the java trigonmetric
         functions.  As an example, I use sin() here.  
          
           double x = 0.5;
           double y = Math.sin( 0.5 );
           System.out.println( "y: " + y );
           double y2 = y - 0.4794; 
Your problem is here       ^^^^^^
           System.out.println( "y2: " + y2 );
          
         The above code fragment produces:
         
           y: 0.479426 
           y2: 2.55297e-05
         
         which I have taken to be:
         
         0.4794255297
         
         BTW, the y2 variable is introduced here because I have not
         figured out how to get better precision in the output of a
         double. 
         
         Now, an analagous C++ program (running on the same machine,
         also with a double ) produces:
         
         0.479425538604203
         
         Similarly, an HP scientific calculator yields:
         
         0.479425538604
         
         A calculator tool produces:
         
         0.479425538604
         
         
         Although I have agreement to 7 significant digits, I need 
         much better.  I have ported a function which produces 
         results differing by up to 50% from the C++ implementation
         because of the trig differences.
         
         
         Questions
         =========
         
         Is this a display problem or a processing problem?
         
         Can anyone else verify this?
         
         Why does the Java sin function for double agree to only 7
         significant digits with a C program when the float results
         agree to 8 digits?
The sin function is fine, but the float constant is
only single precision!
         
         Is this discrepancy known and will it be fixed in an upcoming
         release or am I stuck with this accuracy?
         
         
         Cheers,
         Trevor Misfeldt
         
         
         
         
Consider this program:

    class tsin {
    public static void main( String xxxx[] ) {
	       double x = 0.5;
	       double y = Math.sin( 0.5 );
	       System.out.println( "y: " + y );
	       double y2 = y - 0.4794; 
	       System.out.println( "y2: " + y2 );
	       double y3 = y - 0.4794d; 
	       System.out.println( "y3: " + y3 );
    }
    }
It produces this output:
y: 0.479426
y2: 2.55297e-05
y3: 2.55386e-05

Notice how y3 differs from y2. Notice how 0.4794d differs
from 0.4794 

Java float constants are single-precision by default. To make
them double, append type suffix d. The sin() function is the same.

			rt			 

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post