×

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

Related Topics

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.