Table of Contents
Previous Section Next Section

Chapter 9: The C++ Mathematical Functions

Overview

As explained in Chapter 8, both C and C++ originally supported the same set of 22 math functions. However, as C++ matured, it added overloaded versions of these original functions. Then, C99 greatly increased the size of the C math library in ways that C++ does not support. The net result of these changes is that today, the C and C++ math libraries have diverged, although both still support the original core set of math functions. Because of the differences between C and C++ in this area, the C math functions (including those added by C99) are described in Chapter 8. This chapter describes the math functions defined by C++.

In C++, the math functions require the header <cmath>. In addition to declaring the math functions, this header defines the macro called HUGE_VAL. The macros EDOM and ERANGE are also used by the math functions. These macros are defined in the header <cerrno>. If an argument to a math function is not in the domain for which it is defined, an implementation-defined value is returned, and the built-in global integer variable errno is set equal to EDOM. If a routine produces a result that is too large to be represented, an overflow occurs. This causes the routine to return HUGE_VAL, and errno is set to ERANGE, indicating a range error. If an underflow happens, the function returns zero and sets errno to ERANGE.

C++ supports the original math functions defined by C89. However, in C89, these functions operate only on floating-point values of type double. To these functions, C++ adds overloaded versions that explicitly accommodate values of type float and long double. The operation of the functions is otherwise unchanged.

All angles are in radians.


Table of Contents
Previous Section Next Section