The <cmath>
header declares a number of mathematical functions (from the C standard <math.h>
). Most functions have multiple, overloaded versions for different parameter types; each function's syntax description shows all the overloaded functions.
If an argument is out of range, a domain error occurs. The function sets errno
to EDOM
and returns an error value. The value is defined by the implementation, so the only portable way test for a domain error is to check errno
. If the function's result is an overflow, a range error occurs. The function returns HUGE_VAL
and sets errno
to ERANGE
. If underflow occurs, the function returns zero, and may or may not set errno
to ERANGE
.
HUGE_VAL
is defined to be a double
, and the C++ standard does not define a suitable value for the float
and long
double
versions of the math functions. If you are using a system that has infinity as an explicit floating point value (such as IEC 60559/IEEE 754, which is found on PCs, Macintoshes, and modern workstations), most likely the overloaded versions of a function return infinity for overflow, so there is no problem with the float
and long
double
versions of the functions. For maximum portability, though, use only the double
versions of the math functions.
All the trigonometric functions use radians.
Several other headers in the standard library declare additional mathematical functions:
<complex>
<complex>
header declares types and functions for working with complex numbers.<cstdlib>
<cstdlib>
header declares integer absolute value functions, and functions that compute quotient and remainder in a single operation.<numeric>
<numeric>
header declares generic numerical algorithms. <limits>
<limits>
header for the limits of the numerical types, e.g., the largest float
, the precision of double
, and so on.<valarray>
<valarray>
header declares types and functions that work with arrays of numbers. Absolute value
float abs(float x) double abs(double x) long double abs(long double x)
The abs
function returns the absolute value of its argument: if x
< 0, it returns -x
; otherwise, it returns x
.
The abs
function is the same as fabs
. The <cstdlib>
header declares integer versions of the abs
function.
abs function in <cstdlib>, fabs function
Inverse cosine
float acos(float x) double acos(double x) long double acos(long double x)
The acos
function returns the inverse cosine of its argument. The parameter x
must be in the range [-1, 1], or a domain error occurs. The return value is in the range [0, pi ].
Inverse sine
float asin(float x) double asin(double x) long double asin(long double x)
The asin
function returns the inverse sine of its argument. The parameter x
must be in the range [-1, 1], or a domain error occurs. The return value is in the range [-pi /2, pi /2].
Inverse tangent
float atan(float x) double atan(double x) long double atan(long double x)
The atan
function returns the inverse tangent of its argument. The return value is in the range [-pi /2, pi /2].
Inverse tangent
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 inverse tangent of y/x
, using the sign of both numbers to determine the quadrant for the return value. It correctly handles the case where x
is 0 (that is, pi /2 times the sign of y
for non-zero y
; if y
is zero, the result is implementation-defined and might be a range error). The return value is in the range [-pi , pi ].
Ceiling
float ceil(float x) double ceil(double x) long double ceil(long double x)
The ceil
function returns the smallest integer that is greater than or equal to x
.
floor function
Cosine
float cos(float x) double cos(double x) long double cos(long double x)
The cos
function returns the cosine of its argument. The return value is in the range [-1, 1].
Hyperbolic cosine
float cosh(float x) double cosh(double x) long double cosh(long double x)
The cosh
function returns the hyperbolic cosine of its argument. Note that there are no inverse hyperbolic trigonometric functions.
Exponential
float exp(float x) double exp(double x) long double exp(long double x)
The exp
function returns ex. If x
is too large, a range error occurs.
log function, pow function
Absolute value
float fabs(float x) double fabs(double x) long double fabs(long double x)
The fabs
function returns the absolute value of its argument: if x
< 0, it returns -x
; otherwise, it returns x
.
The fabs
function is the same as abs
for floating point numbers. It exists only for compatibility with C.
abs function, abs function in <cstdlib>
Floor
float floor(float x) double floor(double x) long double floor(long double x)
The floor
function returns the largest integer that is less than or equal to x
.
ceil function
Modulus
float fmod(float x, float y) double fmod(double x, double y) long double fmod(long double x, long double y)
The fmod
function returns the floating point remainder of dividing x
by y
. If y is zero, the behavior is implementation defined: the return value might be zero, or a domain error can occur. If y
is not zero, the return value is x
- i × y
, such that the result has the same sign as x
and an absolute value less than the absolute value of y
.
Fraction and exponent
float frexp(float x, int* exp) double frexp(double x, int* exp) long double frexp(long double x, int* exp)
The frexp
function separates a floating point number into a fraction and an exponent (with a base of 2). The exponent is stored in *exp
. The return value is the normalized fractional part of x
in the range [1/2, 1) or zero. If x
is zero, the return value and *exp
are zero.
ldexp function, modf function
Range error value
double HUGE_VAL
When an overflow occurs, most functions set errno
to ERANGE
and return HUGE_VAL
, with the correct sign of the result. The exact value of HUGE_VAL
is implementation-defined. It might be a value that can be returned as a valid result from the function. In that case, the only way to discover whether an overflow occurred is to test errno
, as shown in Example 13-6.
Example 13-6. Computing a logarithm to any base.
// Return the logarithm of x to the base n. template<typename T> T logn(T x, T n) { errno = 0; T logx = log(x); if (errno == ERANGE) return logx; // should be HUGE_VAL else if (errno != 0) return logx; // implementation defined T logn = log(n); if (errno == ERANGE) return logn; // should be HUGE_VAL else if (errno != 0) return logn; // implementation defined if (logn == 0) { errno = EDOM; return 0; } return logx / logn; }
<cerrno>
Floating point from fraction and exponent
float ldexp(float frac, int exp) double ldexp(double frac, int exp) long double ldexp(long double frac, int exp)
The ldexp
function returns a floating point number that it constructs from a fractional part and an exponent (base 2). The return value is frac
× 2exp.
frexp function, modf function
Natural logarithm
float log(float x) double log(double x) long double log(long double x)
The log
function returns the natural (base e) logarithm of its argument. A domain error occurs if x
is negative. A range error might occur is x
is zero.
Common logarithm
float log10(float x) double log10(double x) long double log10(long double x)
The log10
function returns the common (base 10) logarithm of its argument. A domain error occurs if x
is negative. A range error might occur is x
is zero.
Integer and fraction parts
float modf(float x, float* iptr) double modf(double x, double* iptr) long double modf(long double x, long double* iptr)
The modf
function splits a floating point number into integral and fractional parts. Both parts have the same sign as x
. The integral part is stored in *iptr
; the return value is the fractional part.
frexp function, ldexp function
Power
float pow(float x, float y) float pow(float x, int y) double pow(double x, double y) double pow(double x, int y) long double pow(long double x, long double y) long double pow(long double x, int y)
The pow
function raises x
to the y
power. If x
is negative and y
is not an integral value, a domain error occurs. If x
is zero, and y
is less than or equal to zero, and the result cannot be represented as a real number, a domain error occurs. A range error can occur if the result is out of range.
exp function
Sine
float sin(float x) double sin(double x) long double sin(long double x)
The sin
function returns the sine of its argument. The return value is in the range [-1, 1].
Hyperbolic sine
float sinh(float x) double sinh(double x) long double sinh(long double x)
The sinh
function returns the hyperbolic sine of its argument. Note that there are no inverse hyperbolic trigonometric functions.
Square root
float sqrt(float x) double sqrt(double x) long double sqrt(long double x)
The sqrt
function returns the square root or its argument. If x
is negative, a domain error occurs. The return value is always positive or zero.
Tangent
float tan(float x) double tan(double x) long double tan(long double x)
The tan
function returns the tangent of its argument. The standard does not specify the result when the tangent is undefined (that is, where x
is kpi +pi /2 for any integer k), but a reasonable result is a range error. Due to the nature of the tangent function, the sign of the return value (HUGE_VAL
) can be positive or negative.
Hyperbolic tangent
float tanh(float x) double tanh(double x) long double tanh(long double x)
The tanh
function returns the hyperbolic tangent of its argument. Note that there are no inverse hyperbolic trigonometric functions.