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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.