The C++ Mathematical Functions

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.

acos

#include <cmath>float acos(float arg);double acos(double arg);
long double acos(long double arg);
 
The acos( ) function returns the arc cosine of arg. The argument to acos( ) must be in the range –1 to 1; otherwise, a domain error will occur.

asin

#include <cmath>float asin(float arg);double asin(double arg); 
long double asin(long double arg); 
 
The asin( ) function returns the arc sine of arg. The argument to asin( ) must be in the range –1 to 1; otherwise, a domain error will occur.

atan

#include <cmath>float atan(float arg);double atan(double arg);
long double atan(long double arg);
 
The atan( ) function returns the arc tangent of arg.

atan2

#include <cmath>float atan2(float y, float x);double atan2(double y, double x);
long double atan2(long double y, long double x);
 
The atan2( ) function returns the arc tangent of y/x. It uses the signs of its arguments to compute the quadrant of the return value.

atan

#include <cmath>float atan(float arg);double atan(double arg);
long double atan(long double arg);
 
The atan( ) function returns the arc tangent of arg.

atan2

#include <cmath>float atan2(float y, float x);double atan2(double y, double x);
long double atan2(long double y, long double x);
 
The atan2( ) function returns the arc tangent of y/x. It uses the signs of its arguments to compute the quadrant of the return value.

ceil

#include <cmath>float ceil(float num);double ceil(double num);
long double ceil(long double num); 
 
The ceil( ) function returns the smallest integer (represented as a floating-point value) not less than num. For example, given 1.02, ceil( ) would return 2.0. Given –1.02, ceil( ) would return –1.
Related functions are floor( ) and fmod( ).

cos

#include <cmath>float cos(float arg);double cos(double arg);
long double cos(long double arg);
 
The cos( ) function returns the cosine of arg. The value of arg must be in radians.

cosh

#include <cmath>float cosh(float arg);double cosh(double arg);
long double cosh(long double arg);
 

Followers