Math fma() method in Java with Examples
Last Updated :
29 Oct, 2018
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
// 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);
}
}
Output:
9.2 * 6.3 + 4.1 = 62.059999999999995
Program 2:
Java
// 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);
}
}
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
// 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);
}
}
Output:
29.2 * 18.4 + 43.1 = 580.38
Program 2:
Java
// 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);
}
}
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)
Similar Reads
Math pow() Method in Java with Example The Math.pow() method in Java is used to calculate a number raised to the power of some other number. It is part of the java.lang.Math class and is widely used in mathematical computations, scientific calculations, and algorithmic problems. This function accepts two parameters and returns the value
3 min read
Java Math IEEEremainder() method with Examples The IEEEremainder() method in Java is a part of the java.lang.Math class. This method calculates the remainder operation on two arguments based on the IEEE 754 standard, which is different from the regular modulus "%" operator.Usage of IEEEremainder() MethodThe method calculates the remainder using
2 min read
Java Math sin() method with Examples The java.lang.Math.sin() returns the trigonometry sine of an angle in between 0.0 and pi. If the argument is NaN or infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument. The value returned will be between -1 and 1. Syntax : public st
2 min read
Java Math max() method with Examples The max() method in Java is a part of java.lang.Math class. This is an inbuilt function in Java that returns maximum of two numbers. This method can work with any type of number, such as int, float, long, and double.Special Cases:If a negative and a positive number are passed as arguments, then the
2 min read
Java Math ulp() method with Examples In Java, the Math.ulp() method returns the size of the unit of least precision (ulp) of a given number. This method is used to find the smallest difference between a given floating-point number and the next larger number it can represent. It works with both float and double types. In this article, w
3 min read