# C in a Nutshell (In a Nutshell (OReilly)) - page 201

 fma

Multiplies two numbers and adds a third number to their product

```#include <math.h>
double

fma

( double

x

, double

y

, double

z

);
float

fmaf

( float

x

, float

y

, float

z

);
long

double

fmal( long double

x

, long double

y

, long double

z

);
```

The name of the fma( ) function stands for "fused multiply-add." fma( ) multiplies its first two floating-point arguments, then adds the third argument to the result. The advantage over the expression ( x * y ) + z , with two separate arithmetic operations, is that fma( ) avoids the error that would be incurred by intermediate rounding, as well as intermediate overflows or underflows that might otherwise be caused by the separate multiplication.

If the implementation defines the macro FP_FAST_FMA in math.h , that indicates that the fma( ) function is about as fast to execute as, or faster than, the expression ( x * y ) + z . This is typically the case if the fma( ) function makes use of a special FMA machine operation. The corresponding macros FP_FAST_FMAF and FP_FAST_FMAL provide the same information about the float and long double versions.

#### Example

```double x, y, z;

x = nextafter( 3.0, 4.0 ); // Smallest possible double value greater than 3
y = 1.0/3.0;
z = -1.0;

printf( "x = %.15G\n"
"y = %.15G\n"
"z = %.15G\n", x, y, z );

#ifdef FP_FAST_FMA

printf( "fma( x, y, z) = %.15G\n", fma( x, y, z) );

#else     // i.e., not def FP_FAST_FMA

double product = x * y;

printf( "x times y = %.15G\n", product );
printf( "%.15G + z = %.15G\n", product, product + z );

#endif   // def FP_FAST_FMA
```

 fmax

Determines the greater of two floating-point numbers

```#include <math.h>
double

fmax

( double

x

, double

y

);
float

fmaxf

( float

x

, float

y

);
long double

fmaxl

( long double

x

, long double

y

);
```

The fmax( ) functions return the value of the greater argument.

#### Example:

```// Let big equal the second-greatest possible double value ...
const double big = nextafter( DBL_MAX, 0.0 );
// ... and small the second-least possible double value:
const double small = nextafter( DBL_MIN, 0.0 );

double a, b, c;

/* ... */

if ( fmin( fmin( a, b ), c ) <= small )
printf( "At least one value is too small.\n" );
if (

fmax

(

fmax

( a, b ), c ) >= big )
printf( "At least one value is too great.\n" );
```

fabs( ) , fmin( )

 fmin

Determines the lesser of two floating-point numbers

```#include <math.h>
double

fmin

( double

x

, double

y

);
float

fminf

( float

x

, float

y

);
long double

fminl

( long double

x

, long double

y

);
```

The fmin( ) functions return the value of the lesser argument.

#### Example

See the example for fmax( ) .

fabs( ) , fmax( )

 fmod

Performs the modulo operation

```#include <math.h>
double

fmod

( double

x

, double

y

);
float

fmodf

( float

x

, float

y

);

(C99)

long double

fmodl

( long double

x

, long double

y

);

(C99)

```

The fmod( ) function returns the remainder of the floating-point division of x by y , called " x modulo y ." The remainder is equal to x minus the product of y and the largest integer quotient whose absolute value is not greater than that of y . This quotient is negative (or 0) if x and y have opposite signs, and the return value has the same sign as x . If the argument y is zero, fmod( ) may incur a domain error, or return 0.

#### Example

```double people = -2.25, apples = 3.3, eachgets = 0.0, someleft = 0.0;
int saverounding = fegetround( );       // Save previous setting

fesetround(FE_TOWARDZERO);

eachgets = rint( apples / people );
someleft =

fmod

( apples, people );

printf( "If there are %+.2f of us and %+.2f apples, "
"each of us gets %+.2f, with %+.2f left over.\n",
people, apples, eachgets, someleft );

fesetround( saverounding );            // Restore previous setting
```

This code produces the following output:

```If there are -2.25 of us and +3.30 apples, each of us gets -1.00, with +1.05 left over.
```