Namespaces
Variants
Views
Actions

fma

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
fma
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       fmaf( float x, float y, float z );
(since C99)
double      fma( double x, double y, double z );
(since C99)
long double fmal( long double x, long double y, long double z );
(since C99)

The fma functions compute (x*y) + z, rounded as one ternary operation, according to the rounding mode characterized by the value of FLT_ROUNDS.

Contents

[edit] Parameters

x, y, z - floating point values

[edit] Return value

(x*y) + z, rounded as one ternary operation

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{ 
    #ifdef FP_FAST_FMAF
        printf("FP_FAST_FMAF defined\n");
    #else
        printf("FP_FAST_FMAF undefined\n");
    #endif
    #ifdef FP_FAST_FMA
        printf("FP_FAST_FMA  defined\n");
    #else
        printf("FP_FAST_FMA  undefined\n");
    #endif
    #ifdef FP_FAST_FMAL
        printf("FP_FAST_FMAL defined\n");
    #else
        printf("FP_FAST_FMAL undefined\n");
    #endif
 
    /* Expect the software version to be slower. */
    printf("fma(2.0,3.0,1.0) = %.1f\n", fma(2.0,3.0,1.0));
    printf("\n");
 
    /* "invalid" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fma(INFINITY,0.0,NAN)      = %f\n", fma(INFINITY,0.0,NAN));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fma(INFINITY,0.0,1.0)      = %f\n", fma(INFINITY,0.0,1.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fma(DBL_MAX,2.0,-INFINITY) = %f\n", fma(DBL_MAX,2.0,-INFINITY));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

FP_FAST_FMAF undefined
FP_FAST_FMA  undefined
FP_FAST_FMAL undefined
fma(2.0,3.0,1.0) = 7.0
 
fma(INFINITY,0.0,NAN)      = -nan
Success
current exceptions raised:  FE_INVALID
fma(INFINITY,0.0,1.0)      = -nan
Success
current exceptions raised:  FE_INVALID
fma(DBL_MAX,2.0,-INFINITY) = -inf
Success
current exceptions raised:  none

[edit] See also

signed remainder of the division operation
(function) [edit]
(C99)
signed remainder as well as the three last bits of the division operation
(function) [edit]