Home » C++ programming language

fma() function with example in C++

C++ fma() function: Here, we are going to learn about the fma() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 27, 2019

C++ fma() function

fma() function is a library function of cmath header, it is used to find the result of multiply-add, it accepts three arguments and returns the result of the expression where first and second arguments will be multiplied and the third argument will be added to the multiplied result. It the arguments are x, y and z, it returns (x*y+z).

Note: The fma() function computes and returns the exact result without losing precision.

Syntax of fma() function:

    fma(x, y, z);

Parameter(s): x, y, z – are the numbers to calculate the multiply-add.

Return value: double – it returns double value that is the result of x*y+z.

Example:

    Input:
    float x = 10.20;
    float y = 20.91;
    float z = 30.12;

    Function call:
    fma(x, y, z);

    Output:
    243.402

C++ code to demonstrate the example of fma() function

// C++ code to demonstrate the example of 
// fma() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x,y,z;
    float result;
    
    x = 1;
    y = 2;
    z = 3;
    result = fma(x,y,z);
    cout<<"result: "<<result<<endl;

    x = 10.20;
    y = 20.91;
    z = 30.12;
    result = fma(x,y,z);
    cout<<"result: "<<result<<endl;    

    x = -10.21220;
    y = 20.9122;
    z = -30.1212;
    result = fma(x,y,z);
    cout<<"result: "<<result<<endl;    

    return 0;
}

Output

result: 5
result: 243.402
result: -243.681



Comments and Discussions!

Load comments ↻






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