Contents|Index|Next

Mathematical Functions (‘math.h’)

This documentation groups a wide variety of mathematical functions. The corresponding definitions and declarations are in math.h. The following two definitions from math.h are of particular interest.

- The representation of infinity as a double is defined as HUGE_VAL; this number is returned on overflow by many functions.
- The structure exception is used when you write customized error handlers for the mathematical functions. You can customize error handling for most of these functions by defining your own version of matherr; see matherr for details.

Since the error handling code calls fputs, the mathematical sub-routines require stubs or minimal implementations for the same list of OS subroutines as fputs: close, fstat, isatty, lseek, read, sbrk, write. See System Calls in GNUPro C Library in GNUPro Libraries, for a discussion and for sample minimal implementations of these support subroutines.

Alternative declarations of the mathematical functions, which exploit specific machine capabilities to operate faster—but generally have less error checking and may reflect additional limitations on some machines—are available when you include fastmath.h instead of math.h.

Version of library

There are four different versions of the math library routines: IEEE, POSIX, X/Open, or SVID.

The version may be selected at runtime by setting the global variable, _LIB_VERSION, defined in math.h. It may be set to one of the following constants defined in math.h: _IEEE_, _POSIX_, _XOPEN_, or _SVID_.

The _LIB_VERSION variable is not specific to any thread, and changing it will affect all threads.

The versions of the library differ only in how errors are handled.

In IEEE mode, the matherr function is never called, no warning messages are printed, and errno is never set.

In POSIX mode, errno is set correctly, but the matherr function is never called and no warning messages are printed.

In X/Open mode, errno is set correctly, and matherr is called, but warning message are not printed. In SVID mode, functions which overflow return 3.40282346638528860e+38, the maximum single precision floating point value, rather than infinity. Also, errno is set correctly, matherr is called, and, if matherr returns 0, warning messages are printed for some errors. For example, by default ‘log(-1.0)’ writes this message on standard error output: log: DOMAIN error.

The library is set to X/Open mode by default.