# Math fma() method in Java with Examples

In Math 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 **Math 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 = Math.fma(a, b, c); ` ` ` ` ` `// print result ` ` ` `System.out.println(a + ` `" * "` `+ b ` ` ` `+ ` `" + "` `+ c ` ` ` `+ ` `" = "` `+ d); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**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 = Math.fma(a, b, c); ` ` ` ` ` `// print result ` ` ` `System.out.println(a + ` `" * "` `+ b ` ` ` `+ ` `" + "` `+ c ` ` ` `+ ` `" = "` `+ d); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

19.2 * 16.3 + 44.1 = 357.06

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

This method of **Math 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 = Math.fma(a, b, c); ` ` ` ` ` `// print result ` ` ` `System.out.println(a + ` `" * "` `+ b ` ` ` `+ ` `" + "` `+ c ` ` ` `+ ` `" = "` `+ d); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**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 = Math.fma(a, b, c); ` ` ` ` ` `// print result ` ` ` `System.out.println(a + ` `" * "` `+ b ` ` ` `+ ` `" + "` `+ c ` ` ` `+ ` `" = "` `+ d); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

49.29 * 28.58 + 33.63 = 1442.3383

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

## Recommended Posts:

- Java Math cos() method with Examples
- Java Math ulp() method with Examples
- Java Math tan() method with Examples
- Java Math min() method with Examples
- Java Math max() method with Examples
- Java Math sin() method with Examples
- Java Math abs() method with Examples
- Math multiplyFull() method in Java with Examples
- Java Math copySign() method with Examples
- Java Math acos() method with Examples
- Java Math cosh() method with Examples
- Java Math tanh() method with Examples
- Java Math sinh() method with Examples
- Math multiplyHigh() method in Java with Examples
- Java Math IEEEremainder() method with Examples

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.