[
https://issues.apache.org/jira/browse/MATH-1591?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17353511#comment-17353511
]
Alex Herbert commented on MATH-1591:
------------------------------------
{quote}would any of the code from ExtendedPrecision apply here?
{quote}
Yes. You have to compute a full precision multiplication of two doubles. There
is code to do this here:
{code:java}
org.apache.commons.numbers.examples.jmh.arrays.DoublePrecision
static void multiply(double x, double y, Quad c);
{code}
The Quad has a high and low part. For FMA you then add your addend to low. The
result you add to high.
But it is complicated. For an IEEE result you need to have the correct
rounding. So when you add the addend to low you have to carry any lost bits.
This involves inspecting the roundoff from adding the addend to low. If the
round-off is non-zero then you have created a triple split number:
{noformat}
high low
+ addend
= high sum round-off
{noformat}
You cannot just add high to sum. The round-off contains extra bits that will
determine the rounding of high and sum. If the final bit of sum is 1 then it
already contains the information for a round-ties-to-even result. If the final
bit of sum is 0, and the round-off is non-zero then you have to update the
final bit of sum to a 1. This is in the direction of the sign of the round-off
(i.e. when sum was rounded to 0 for the final bit, there was a tiny amount
extra left over and it may be positive or negative) accounting for the existing
sign of sum.
You also need to handle scaling to avoid intermediate overflow, e.g. this
should work:
{code:java}
double max = Double.MAX_VALUE;
double x = Math.fma(max, 2, -max);
assert x == max;
{code}
There are edge cases to consider. NaN inputs, Infinite inputs and the case
where the addend is of a totally different magnitude to product so cannot be
added directly.
One advantage is that you can test against the Java 9 implementation which will
be correct.
> Add missing methods for FastMath to bring FastMath up to JDK9 level
> -------------------------------------------------------------------
>
> Key: MATH-1591
> URL: https://issues.apache.org/jira/browse/MATH-1591
> Project: Commons Math
> Issue Type: Improvement
> Affects Versions: 4.0
> Reporter: Erik Svensson
> Priority: Major
>
> With JDK9, 6 new methods were added to java.lang.Math which do not exist in
> FastMath, which means that FastMath is not a drop-in replacement for jlM.
> I propose to add them
> The new methods are :
> fma
> ([https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#fma(double,double,double)])
> multiplyFull
> ([https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#multiplyFull(int,int)])
> multiplyHigh
> ([https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#multiplyHigh(long,long)])
> negateExact
> ([https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#negateExact(int)])
> toDegrees
> ([https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#toDegrees(double)])
> toRadians
> ([https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Math.html#toRadians(double)])
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)