C++ Standard Library C++ STL Library

C++ <cmath> - FP_FAST_FMA, FP_FAST_FMAF, FP_FAST_FMAL



The C++ <cmath> fma() function returns (x*y) + z. The function computes the result without losing precision and rounded only once to fit the result type.

FP_FAST_FMA, FP_FAST_FMAF and FP_FAST_FMAL macro constants can be defined in an implementation to signal the function to evaluate faster (in addition to being more precise) than the expression (x*y) + z for float, double, and long double arguments, respectively. If defined, these macros evaluate to integer 1.

MacrosDescription
FP_FAST_FMA When defined, function fma() evaluates faster (in addition to being more precise) than the expression (x*y) + z for type double.
FP_FAST_FMAF When defined, function fma() evaluates faster (in addition to being more precise) than the expression (x*y) + z for type float.
FP_FAST_FMAL When defined, function fma() evaluates faster (in addition to being more precise) than the expression (x*y) + z for type long double.

Definition in the <cmath> header file is:

#define FP_FAST_FMA  /* implementation-defined */
#define FP_FAST_FMAF /* implementation-defined */
#define FP_FAST_FMAL /* implementation-defined */                          

Example:

The example below shows the usage of these macro constants.

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

int main (){
  double x, y, z, result;
  x = 2.1;
  y = 4.2;
  z = 10.3;
  
  #ifdef FP_FAST_FMA 
    result = fma(x, y, z);
  #else
    result = (x * y) + z;
  #endif

  cout<<"(x * y) + z = "<<result;

  return 0;
}

The output of the above code will be:

(x * y) + z = 19.12

❮ C++ <cmath> Library