9.6. Mathematical functions

If you are writing mathematical programs, involving floating point calculations and so on, then you will undoubtedly require access to the mathematics library. This set of functions all take double arguments, and return a double result. The functions and associated macros are defined in the include file <math.h>.

The macro HUGE_VAL is defined, which expands to a positive double expression, which is not necessarily representable as a float.

For all the functions, a domain error occurs if an input argument is outside the domain over which the function is defined. An example might be attempting to take the square root of a negative number. If this occurs, errno is set to the constant EDOM, and the function returns an implementation defined value.

If the result of the function cannot be represented as a double value then a range error occurs. If the magnitude of the result is too large, the functions return ±HUGE_VAL (the sign will be correct) and errno is set to ERANGE. If the result is too small, 0.0 is returned and the value of errno is implementation defined.

The following list briefly describes each of the functions available:

double acos(double x);
Principal value of the arc cosine of x in the range 0–π radians.

Errors: EDOM if x is not in the range −1–1.
double asin(double x);
Principal value of the arc sine of x in the range -π/2–+π/2 radians.

Errors: EDOM if x is not in the range −1–1.
double atan(double x);
Principal value of the arc tangent of x in the range -π/2–+π/2 radians.
double atan2(double y, double x);
Principal value of the arc tangent of y/x in the range -π–+π radians, using the signs of both arguments to determine the quadrant of the return value.

Errors: EDOM may occur if both x and y are zero.
double cos(double x);
Cosine of x (x measured in radians).
double sin(double x);
Sine of x (x measured in radians).
double tan(double x);
Tangent of x (x measured in radians). When a range error occurs, the sign of the resulting HUGE_VAL is not guaranteed to be correct.
double cosh(double x);
Hyperbolic cosine of x.

Errors: ERANGE occurs if the magnitude of x is too large.
double sinh(double x);
Hyperbolic sine of x.

Errors: ERANGE occurs if the magnitude of x is too large.
double tanh(double x);
Hyperbolic tangent of x.
double exp(double x);
Exponential function of x. Errors: ERANGE occurs if the magnitude of x is too large.
double frexp(double value, int *exp);
Break a floating point number into a normalized fraction and an integral power of two. This integer is stored in the object pointed to by exp.
double ldexp(double x, int exp);
Multiply x by 2 to the power exp

Errors: ERANGE may occur.
double log(double x);
Natural logarithm of x.

Errors: EDOM occurs if x is negative. ERANGE may occur if x is zero.
double log10(double x);
Base-ten logarithm of x.

Errors: EDOM occurs if x is negative. ERANGE may occur if x is zero.
double modf(double value, double *iptr);
Break the argument value into integral and fractional parts, each of which has the same sign as the argument. It stores the integrbal part as a double in the object pointed to by iptr, and returns the fractional part.
double pow(double x, double y);
Compute x to the power y.

Errors: EDOM occurs if x < 0 and y not integral, or if the result cannot be represented if x is 0, and y ≤ 0. ERANGE may also occur.
double sqrt(double x);
Compute the square root of x.

Errors: EDOM occurs if x is negative.
double ceil(double x);
Smallest integer not less than x.
double fabs(double x);
Absolute value of x.
double floor(double x);
Largest integer not greater than x.
double fmod(double x, double y);
Floating point remainder of x/y.

Errors: If y is zero, it is implementation defined whether fmod returns zero or a domain error occurs.