[742] in java-interest
Re: trig precision problems?
daemon@ATHENA.MIT.EDU (Bob Hoekstra)
Tue Jul 18 12:58:23 1995
In-Reply-To: <9507171740.AA05631@mda.ca>
To: Trevor Misfeldt <misfeldt@mda.ca>, java-interest@java.Eng.Sun.COM
Reply-To: roh@scuk.demon.co.uk
From: Bob Hoekstra <roh@scuk.demon.co.uk>
Date: Tue, 18 Jul 95 09:39:00 +0100 (BST)
Hi,
<<< Trevor Misfeldt wrote >>>
> 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;
> 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
> {snip}
> Can anyone else verify this?
> {snip}
You may (or may not) be comforted to hear that my Dyalog APL
interpreter (which I normally find fairly accurate on such
matters) yields 0.479425538604203 on a similar calculation
with "printing precision" set to 15. If nothing else, at
least this gives you comfort that you C++ compiler is
generating the correct machine code!
I think the Java accuracy is inadequate if that is the best
that can be done!
----------------------------------------------
Bob Hoekstra | Phone: (+44) 1932 567311
Consultant | Fax: (+44) 1932 568145
SimCorp Ltd | SnailMail on request.
----------------------------------------------
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com