×

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

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 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.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute read.