# Using the Intel Math Library

To use the Intel math library, include the header file, mathimf.h, in your program. Here are two example programs that illustrate the use of the math library.

## Example Using Real Functions

 // real_math.c   #include #include   int main() {   float  fp32bits; double fp64bits; long double fp80bits; long double pi_by_four = 3.141592653589793238/4.0;   // pi/4 radians is about 45 degrees.   fp32bits = (float) pi_by_four;   // float approximation to pi/4 fp64bits = (double) pi_by_four;  // double approximation to pi/4 fp80bits = pi_by_four;           // long double (extended) approximation to pi/4   // The sin(pi/4) is known to be 1/sqrt(2) or approximately .7071067   printf("When x = %8.8f, sinf(x) = %8.8f \n", fp32bits, sinf(fp32bits)); printf("When x = %16.16f, sin(x) = %16.16f \n", fp64bits, sin(fp64bits)); printf("When x = %20.20Lf, sinl(x) = %20.20f \n", fp80bits, sinl(fp80bits));   return 0; }

Compiling real_math.c:

prompt>icc real_math.c

The output of a.out will look like this:

 When x = 0.78539816, sinf(x) = 0.70710678 When x = 0.7853981633974483, sin(x) = 0.7071067811865475 When x = 0.78539816339744827900, sinl(x) = 0.70710678118654750275

## Example Using Complex Functions

 // complex_math.c   #include #include   int main() {   float  _Complex c32in,c32out; double _Complex c64in,c64out; double pi_by_four= 3.141592653589793238/4.0;   c64in = 1.0 + __I__* pi_by_four;   // Create the double precision complex number 1 + (pi/4) * i // where i is the imaginary unit.   c32in = (float _Complex) c64in;   // Create the float complex value from the double complex value.   c64out = cexp(c64in); c32out = cexpf(c32in);   // Call the complex exponential, // cexp(z) = cexp(x+iy) = e^ (x + i y) = e^x * (cos(y) + i sin(y))   printf("When z = %7.7f + %7.7f i, cexpf(z) = %7.7f + %7.7f i \n" ,crealf(c32in),cimagf(c32in),crealf(c32out),cimagf(c32out)); printf("When z = %12.12f + %12.12f i, cexp(z) = %12.12f + %12.12f i \n" ,creal(c64in),cimag(c64in),creal(c64out),cimagf(c64out));   return 0; }

prompt>icc complex_math.c

The output of a.out will look like this:

 When z = 1.0000000 + 0.7853982 i, cexpf(z) = 1.9221154 + 1.9221156 i When z = 1.000000000000 + 0.785398163397 i, cexp(z) = 1.922115514080 + 1.922115514080 i

Note

_Complex data types are supported in C but not in C++ programs.

### Exception Conditions

If you call a math function using argument(s) that may produce undefined results, an error number is assigned to the system variable errno. Math function errors are usually domain errors or range errors.

Domain errors result from arguments that are outside the domain of the function. For example, acos is defined only for arguments between -1 and +1 inclusive. Attempting to evaluate acos(-2) or acos(3) results in a domain error, where the return value is QNaN.

Range errors occur when a mathematically valid argument results in a function value that exceeds the range of representable values for the floating-point data type. Attempting to evaluate exp(1000) results in a range error, where the return value is INF.

When domain or range error occurs, the following values are assigned to errno:

• domain error (EDOM): errno = 33
• range error (ERANGE): errno = 34

The following example shows how to read the errno value for an EDOM and ERANGE error.

 // errno.c   #include #include #include   int main(void) {       double neg_one=-1.0;    double zero=0.0;         // The natural log of a negative number is considered a domain error - EDOM    printf("log(%e) = %e and errno(EDOM) = %d \n",neg_one,log(neg_one),errno);         // The natural log of zero is considered a range error - ERANGE    printf("log(%e) = %e and errno(ERANGE) = %d \n",zero,log(zero),errno); }

The output of errno.c will look like this:

 log(-1.000000e+00) = nan and errno(EDOM) = 33 log(0.000000e+00) = -inf and errno(ERANGE) = 34

For the math functions in this section, a corresponding value for errno is listed when applicable.

### Other Considerations

Some math functions are inlined automatically by the compiler. The functions actually inlined may vary and may depend on any vectorization or processor-specific compilation options used. For more information, see Criteria for Inline Expansion of Functions.

A change of the default precision control or rounding mode may affect the results returned by some of the mathematical functions. See Floating-point Arithmetic Precision.

It's necessary to include the -c99 compiler option when compiling programs that require support for _Complex data types.