Related Tags

c

# What is fma() in C?

Kainat Asif

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The fma() (“fused multiply-add”) ﻿function is used to compute the following expression:

fma() computes the expression $(a * b + c)$ to infinite precision by computing the expression as a single ternary operation. The expression result is rounded off only once upto the decimal places that can fit in the output data type.

## Library

To use the fma() function, include the following library:

#include <math.h>


## Syntax

The declaration of the fma() function is shown below:

double fma (double a, double b, double c);


The fma() function computes the expression (a * b + c) on its input parameters and returns the result of the expression.

## Code

Consider the code snippet below, which uses fma() to compute ( a * b + c):

#include <stdio.h>#include <math.h>int main() {  double a = 1.556532;  double b = 2.389987;  double c = 0.147897;  double x = fma(a, b, c);  printf("fma ( %f, %f, %f ) = ( %lf ) \n", a, b, c, x);    return 0;}

## Explanation

The fma() function is used in line 10 to compute the expression (a * b + c) on variables a, b, and c that are created in lines 5-8.

RELATED TAGS

c

CONTRIBUTOR

Kainat Asif