T
Tom McGlynn
Perhaps Andreas may be thinking of the StrictMath class rather than
strictfp? This class duplicates the functionality of Math but
requires that the results be identical to the fdlibm results. [I
think somewhere else in this thread I also confused this with
strictfp].It's a bit counterintuitive, but I believe the general effect of
either a strictfp block or use of the StrictMath class is a less
accurate calculation. They are all about consistency, not accuracy.
I think StrictMath can go either way. There are generally two
representable numbers that could be a valid Math.sin result for a given
angle, because each is within one ULP of the infinite precision answer.
Math.sin can return either. StrictMath.sin must return the one fdlibm
would pick, which may or may not be the closer of the two.
Patricia
You're certainly correct that it's possible in principle to build a
variant Math library which is legal but less accurate that
StrictMath. My sense, though is that it's not actually that easy
(e.g., see below). So in practice where Math and StrictMath differ
(I'm not aware of anyplace this has actually been done) my suspicion
would be that the Math library is likely to be the more accurate
overall.
Two items of interest that I came up in looking up some of the
background...
First, the problem with using sine hardware is that while it generally
provides accuracy to better than 10^-16, one needs to compute sines to
an absolute accuracy of 10^-32 when one considers the regions where
the sine crosses 0. That's not a problem at x=0, but it is at other
integral multiples of pi. E.g., Math.sin(Math.PI) is of order 10^-16,
but the Java requirement states that it needs to be within one ULP of
the correct answer, so it needs an absolute accuracy of 10^-32.
Second, the discussion of how to make trigonometric calculations
efficient in Java (e.g., to use FPU hardware), notes that an optimizer
is free to substitute an FPU sine call when it knows it will be within
the required accuracy. This suggested to me that it might be possible
for the value of the sine function to change during the execution of a
program that uses runtime optimization, like HotSpot, giving one
answer before the function was optimized and a different answer
(presumably 1 ULP away) afterwards. That sounds a bit weird, and
perhaps its forbidden somewhere... The explicit wiggle room given in
the definition of the Math libraries though makes them a bit different
from most functions though.
Regards,
Tom McGlynn