Using the Intel Math Library

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

Example Using Real Functions

// real_math.c

 

#include <stdio.h>

#include <mathimf.h>

 

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;

}

Since the example program above includes the long double data type, be sure to include the -long_double compiler option:

IA-32 Systems: icc -long_double real_math.c

Itanium®-based Systems: ecc -long_double 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 <stdio.h>

#include <mathimf.h>

 

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;

}

IA-32 Systems: icc complex_math.c

Itanium®-based Systems: ecc 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.

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.

Depending on the data types used, some important compiler options include: