Close

Java - Math.fma() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Methods:

public static double fma(double a,
                         double b,
                         double c)

Returns the fused multiply add of the three arguments; that is, returns the exact product of the first two arguments summed with the third argument and then rounded once to the nearest double . The rounding is done using the round to nearest even rounding mode . In contrast, if a * b + c is evaluated as a regular floating-point expression, two rounding errors are involved, the first for the multiply operation, the second for the addition operation.

API Note:
This method corresponds to the fusedMultiplyAdd operation defined in IEEE 754-2008.
Parameters:
a - a value
b - a value
c - a value
Returns:
(a * b + c) computed, as if with unlimited range and precision, and rounded once to the nearest double value
Since:
9



public static float fma(float a,
                        float b,
                        float c)

Returns the fused multiply add of the three arguments; that is, returns the exact product of the first two arguments summed with the third argument and then rounded once to the nearest float . The rounding is done using the round to nearest even rounding mode . In contrast, if a * b + c is evaluated as a regular floating-point expression, two rounding errors are involved, the first for the multiply operation, the second for the addition operation.

API Note:
This method corresponds to the fusedMultiplyAdd operation defined in IEEE 754-2008.
Parameters:
a - a value
b - a value
c - a value
Returns:
(a * b + c) computed, as if with unlimited range and precision, and rounded once to the nearest float value
Since:
9


Examples


package com.logicbig.example.math;

public class FmaExample {

public static void main(String... args) {
findFma(1.0, 4.0, 5.0);
findFma(4.10, 6.20, 8.90);
findFma(20.19, 13.50, 40.20);
}

private static void findFma(double x, double y, double z) {
double fmaValue = Math.fma(x, y, z);
System.out.printf("Math.fma(x*y+z)= Math(%s,%s,%s)= %.2f%n", x, y, z, fmaValue);
}
}

Output

Math.fma(x*y+z)= Math(1.0,4.0,5.0)= 9.00
Math.fma(x*y+z)= Math(4.1,6.2,8.9)= 34.32
Math.fma(x*y+z)= Math(20.19,13.5,40.2)= 312.77




See Also