Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaStrictMath fma() method in Java with Examples

StrictMath fma() method in Java with Examples

In StrictMath class, there are two types of fma() method depending upon the parameters passed to it.

The methods are:

fma(double a, double b, double c):

This method of StrictMath class is used to return the exact product of the first two doubles with the addition of the third double and then round the result to the nearest double. The rounding is done using the round to nearest even rounding mode. let a, b, c are three doubles then a * b + c is evaluated as a regular floating-point expression, two rounding errors are involved, the first for the multiply operation of a*b, the second for the addition operation of product result with c.

Some Special cases for this method are:

  • If any argument from these three is NaN, then result is NaN.
  • If one of the first two arguments is infinite and the other is zero, then the result is NaN.
  • If the exact product of two arguments is infinite and the third argument is an infinity of the opposite sign, the result is NaN.

Example:

Input: a=2.0, b=3.0, c=3.0
Output: 9.0
    As 2.0 * 3.0 + 3.0 = 9.0

Input: a=9.20, b=6.30, c=4.10
Output: 62.059999999999995 
    As 9.20 * 6.30 + 4.10 = 62.059999999999995 

Syntax:

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

Parameters: This method accepts three doubles a, b, c as parameter where a and b are argument to be multiplied and c is argument to be added with product.

Return Value: This method returns rounded double value after calculating a * b + c with unlimited range and precision.

Note: This method was added in JDK 9. Hence it wont run in Online IDE.

Below programs illustrate the fma() method:

Program 1:




// Java program to demonstrate the fma() Method.
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // three double values
        double a = 9.20, b = 6.30, c = 4.10;
  
        // apply fma method
        double d = StrictMath.fma(a, b, c);
  
        // print result
        System.out.println(a + " * " + b
                           + " + " + c
                           + " = " + d);
    }
}


Output:

9.2 * 6.3 + 4.1 = 62.059999999999995

Program 2:




// Java program to demonstrate the fma() Method.
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // three double values
        double a = 19.20, b = 16.30, c = 44.10;
  
        // apply fma method
        double d = StrictMath.fma(a, b, c);
  
        // print result
        System.out.println(a + " * " + b
                           + " + " + c
                           + " = " + d);
    }
}


Output:

19.2 * 16.3 + 44.1 = 357.06

fma(float a, float b, float c):

This method of StrictMath class work same as fma(double a, double b, double c) but difference is that it take float as attribute and return float as return value. So we can say that this method returns the exact product of the first two arguments(float values) with the addition of the third argument(float value) and then round the result to the nearest float. The rounding is done using the round to nearest even rounding mode. let a, b, c are three floating numbers then a * b + c is evaluated as a regular floating-point expression, two rounding errors are involved, the first for the multiply operation of a*b, the second for the addition operation of product result with c.

Example:

Input: a=29.20f, b=18.40f, c=43.10f;
Output: 580.38 
    As 29.20f * 18.40f + 43.10f = 580.38

Input: a=9.20f, b=6.30f, c=4.10f;
Output: 62.059999999999995f 
    As 9.20f * 6.30f + 4.10f = 62.059999999999995f

Syntax:

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

Parameters: This method accepts three floats a, b, c as parameter where a and b are argument we want to multiply and c is argument we want to add with product.

Return Value: This method returns rounded float value after calculating a * b + c with unlimited range and precision.

Note: This method was added in JDK 9. Hence it wont run in Online IDE.

Below programs illustrate the fma() method:

Program 1:




// Java program to demonstrate
// the fma() Method.
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // three double values
        float a = 29.20f, b = 18.40f, c = 43.10f;
  
        // apply fma method
        float d = StrictMath.fma(a, b, c);
  
        // print result
        System.out.println(a + " * " + b
                           + " + " + c
                           + " = " + d);
    }
}


Output:

29.2 * 18.4 + 43.1 = 580.38

Program 2:




// Java program to demonstrate
// the fma() Method.
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        // three double values
        float a = 49.29f, b = 28.58f, c = 33.63f;
  
        // apply fma method
        float d = StrictMath.fma(a, b, c);
  
        // print result
        System.out.println(a + " * " + b
                           + " + " + c
                           + " = " + d);
    }
}


Output:

49.29 * 28.58 + 33.63 = 1442.3383

References: https://docs.oracle.com/javase/10/docs/api/java/lang/StrictMath.html#fma(double, double, double), https://docs.oracle.com/javase/10/docs/api/java/lang/StrictMath.html#fma(float, float, float)

RELATED ARTICLES

Most Popular

Recent Comments