Math fma () method in java
In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given to it.
The first method prototype is
fma (double x, double y, double z):
The above prototype is used when the product of first two double values along with the addition of the third double is required. The result is rounded to the nearest double value. This rounding is done by using the round to nearest even rounding value.
Assume three double values to be x, y, and z. So, the above method will return the result of the expression x*y+z. In this expression two rounding errors are involved. One is occurred during the computation of x*y and the other is occurred during the addition of the product with the third double value i.e z.
NOTE POINTS:
- If any value of x, y, z is NaN, then the result of the whole expression is NaN.
- As the first two double values are involved in the product operation, if any one of them is infinite, and the third double value is zero, then the result value is NaN.
- If the result of the exact product of the first two double values is infinity and the third argument is opposite sign of infinity, then the final result of the expression is NaN.
SYNTAX:
public static double fma (double x, double y, double z)
x, y, z are the double values which are accepted by the fma function.
This method returns the computation of the expression x*y+z which is a double value.
Example:
Let the values of x, y, z be
INPUT:
X = 3.0
Y = 2.0
Z = 4.0
OUTPUT:
10.0 à As the computation goes with (3.0 * 2.0) + 4.0 = 10.0
Let the values of x, y, z be
INPUT:
X = 3.50
Y = 2.00
Z = 4.20
OUTPUT:
11.20 à As the computation goes with (3.50 * 2.00) + 4.20 = 11.20
Let’s look into the implementation of the fma() Method in a simple java program.
import java.io.*;
public class FmaDemo
{
public static void main (String args [])
{
double x = 3.50;
double y = 2.00;
double z = 4.20;
double result = Math. fma (x, y, z);
//using of fma method by sending three double values as parameters.
System.out.println (x + “*” + y + “+” + z + “=” + result);
}
}
OUTPUT:
3.50 * 2.00 + 4.20 = 11.20
NOTE:
This method was added in JDK 9 only. So, for the earlier versions, this function doesn’t work. So, while implementing this method, do check the version of JDK.
The second method prototype is
fma (float x, float y, float z):
The above prototype is used when the product of first two float values along with the addition of the third float is required. The result is rounded to the nearest float value. This rounding is done by using the round to nearest even rounding value.
Assume three float values to be x, y, and z. So, the above method will return the result of the expression x*y+z which is a float value. In this expression two rounding errors are involved. One is occurred during the computation of x*y and the other is occurred during the addition of the product with the third float value i.e z.
NOTE POINTS:
- If any value of x, y, z is NaN, then the result of the whole expression is NaN.
- As the first two float values are involved in the product operation, if any one of them is infinite, and the third float value is zero, then the result value is NaN.
- If the result of the exact product of the first two float values is infinity and the third argument is opposite sign of infinity, then the final result of the expression is NaN.
SYNTAX:
public static float fma (float x, float y, float z)
x, y, z are the float values which are accepted by the fma function.
This method returns the computation of the expression x*y+z which is a float value.
The return value can be with unlimited range and precision.
Example:
Let the values of x, y, z be
INPUT:
X = 3.30f
Y = 2.05f
Z = 4.23f
OUTPUT:
10.995f
à As the computation goes with (3.30f * 2.05f) + 4.23f = 10.995f
Let the values of x, y, z be
INPUT:
X = 28.20f
Y = 17.40f
Z = 42.10f
OUTPUT:
532.78f
à As the computation goes with (28.20f * 17.40f) + 42.10f = 532.78f
Let’s look into the implementation of the fma () Method in a simple java program.
import java.io.*;
public class FmaDemo
{
public static void main (String args [])
{
float x = 28.20f;
float y = 17.40f;
float z = 42.10f;
float result = Math. fma (x, y, z);
//using of fma method by sending three double values as parameters.
System.out.println (x + “*” + y + “+” + z + “=” + result);
}
}
OUTPUT:
28.20f * 17.40f + 42.10f = 532.78f
NOTE:
This method was added in JDK 9 only. So, for the earlier versions, this function doesn’t work. So, while implementing this method, do check the version of JDK.
fma () Method Example
import java.io.*;
import java.util.*;
public class FmaDemo
{
public static void main (String args [])
{
Scanner sc = new Scanner (System.in);
float x = sc.nextFloat();
float y = sc.nextFloat();
float z = sc.nextFloat();
calculatefmafloat (x, y, z);
double p = sc.nextDouble();
double q = sc.nextDouble();
double r = sc.nextDouble();
calculatefmadouble (p, q, r);
}
private static void calculatefmafloat(float a, float b, float c)
{
float result = Math.fma(a, b, c);
System.out.println(“Math.fma(a, b, c) = %.2f%n”, result);
}
private static void calculatefmadouble(double x, double y, double z)
{
double result = Math.fma(x, y, z);
System.out.println(“Math.fma(x, y, z) = %.2f%n”, result);
}
}
INPUT:
28.20f
17.40f
42.10f
3.50
2.00
4.20
OUTPUT:
Math.fma(x, y, z) = 532.78f
Math.fma(x, y, z) = = 11.20