Golang math.FMA() Function with Examples

Golang | math.FMA() Function: Here, we are going to learn about the FMA() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 31, 2021

math.FMA()

The FMA() function is an inbuilt function of the math package which is used to get the fused multiply-add of x, y, and z (i.e., x * y + z) computed with only one rounding. Where, x, y, and z are the parameters.

It accepts three parameters (x, y, and z), and returns the fused multiply-add of x, y, and z (i.e., x * y + z).

Syntax:

func FMA(x, y, z float64) float64

Parameter(s):

  • x, y, z : The values of float64 type whose fused multiply-add to be found.

Return Value:

The return type of FMA() function is a float64, it returns the fused multiply-add of x, y, and z (i.e., x * y + z).

Example 1:

// Golang program to demonstrate the
// example of math.FMA() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.FMA(0, 0, 0))
	fmt.Println(math.FMA(1, 1, 1))
	fmt.Println(math.FMA(0, 1, 2))
	fmt.Println(math.FMA(1, 2, 3))
	fmt.Println(math.FMA(-10, -20, 30))
	fmt.Println(math.FMA(10, 20, -30))
	fmt.Println(math.FMA(0.15, 0.23, -1.23))

	fmt.Println(math.FMA(math.Inf(1), math.Inf(1), 5))
	fmt.Println(math.FMA(math.NaN(), 1, 5))
}

Output:

0
2
2
5
230
170
-1.1955
+Inf
NaN

Example 2:

// Golang program to demonstrate the
// example of math.FMA() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var y float64
	var z float64
	var FMAXYZ float64

	x = 0
	y = 1
	z = 2
	FMAXYZ = math.FMA(x, y, z)
	fmt.Println("FMA(", x, ",", y, ",", z, ") = ", FMAXYZ)

	x = 1
	y = 2
	z = -3
	FMAXYZ = math.FMA(x, y, z)
	fmt.Println("FMA(", x, ",", y, ",", z, ") = ", FMAXYZ)

	x = -10
	y = -20
	z = 30
	FMAXYZ = math.FMA(x, y, z)
	fmt.Println("FMA(", x, ",", y, ",", z, ") = ", FMAXYZ)

	x = 0.5
	y = 1.5
	z = 2.5
	FMAXYZ = math.FMA(x, y, z)
	fmt.Println("FMA(", x, ",", y, ",", z, ") = ", FMAXYZ)
}

Output:

FMA( 0 , 1 , 2 ) =  2
FMA( 1 , 2 , -3 ) =  -1
FMA( -10 , -20 , 30 ) =  230
FMA( 0.5 , 1.5 , 2.5 ) =  3.25

Golang math Package Constants and Functions »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.