Java StrictMath fma()方法及示例
在StrictMath类中,有两种类型的fma()方法,取决于传递给它的参数。
这些方法是。
fma(double a, double b, double c)。
StrictMath类的这个方法用于返回前两个双数与第三个双数相加的精确乘积,然后将结果四舍五入到最近的双数。让a, b, c是三个双数,那么a * b + c被评估为一个常规的浮点表达式,涉及两个舍入错误,第一个是a*b的乘法运算,第二个是与c的乘积结果的加法运算。
这种方法的一些特殊情况是。
如果这三个参数中的任何一个是NaN,那么结果就是NaN。如果前两个参数中的一个是无限的,另一个是零,那么结果就是NaN。如果两个参数的精确乘积是无限的,而第三个参数是相反符号的无限,那么结果就是NaN。例子。
Input: a=2.0, b=3.0, c=3.0Output: 9.0 As 2.0 * 3.0 + 3.0 = 9.0Input: a=9.20, b=6.30, c=4.10Output: 62.059999999999995 As 9.20 * 6.30 + 4.10 = 62.059999999999995
语法
public static double fma(double a, double b, double c)
参数: 该方法接受三个双数 a、b、c 作为参数,其中a和b是要乘的参数,c是要与乘积相加的参数。
返回值: 该方法在计算完 a * b + c 后返回舍入的双倍值,范围和精度不受限制。
注意: 这个方法是在 JDK 9 中添加的 , 因此它不会在在线IDE中运行。
以下程序说明了fma()方法。
程序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 = StrictMath.fma(a, b, c); // print result System.out.println(a + " * " + b + " + " + c + " = " + d); }}
输出
9.2 * 6.3 + 4.1 = 62.059999999999995
程序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 = StrictMath.fma(a, b, c); // print result System.out.println(a + " * " + b + " + " + c + " = " + d); }}
输出
19.2 * 16.3 + 44.1 = 357.06
fma(float a, float b, float c)。
StrictMath类的这个方法与fma(double a, double b, double c)的工作原理相同,但不同的是,它以浮点数为属性,以浮点数为返回值。所以我们可以说这个方法返回前两个参数(浮点数)与第三个参数(浮点数)的精确乘积,然后将结果四舍五入到最接近的浮点数。四舍五入是使用四舍五入模式完成的。让a,b,c是三个浮点数,那么a*b+c被评估为一个常规的浮点数表达式,涉及两个舍入错误,第一个是a*b的乘法运算,第二个是乘积结果与c的加法运算。
例子:
Input: a=29.20f, b=18.40f, c=43.10f;Output: 580.38 As 29.20f * 18.40f + 43.10f = 580.38Input: a=9.20f, b=6.30f, c=4.10f;Output: 62.059999999999995f As 9.20f * 6.30f + 4.10f = 62.059999999999995f
语法
public static double fma(float a, float b, float c)
参数: 该方法接受三个浮点数 a、b、c 作为参数,其中a和b是我们要乘的参数,c是我们要加的参数。
返回值: 该方法在计算完 a * b + c 后返回舍入的浮点数,范围和精度不限。
注意: 这个方法是在 JDK 9 中添加的,因此它不会在在线IDE中运行。
以下程序说明了fma()方法。
程序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 = StrictMath.fma(a, b, c); // print result System.out.println(a + " * " + b + " + " + c + " = " + d); }}输出
29.2 * 18.4 + 43.1 = 580.38
程序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 = StrictMath.fma(a, b, c); // print result System.out.println(a + " * " + b + " + " + c + " = " + d); }}输出
49.29 * 28.58 + 33.63 = 1442.3383
参考文献: https://docs.oracle.com/javase/10/docs/api/java/lang/StrictMath.html#fma(double, double, double) , https://docs.oracle.com/javase/10/docs/api/java/lang/StrictMath.html#fma(float, float, float)
