Performs various mathematical operations.
The following constants (macros) are all implementation-defined.
Operation in the event of an error is described below.
A domain error occurs if the value of a parameter input to a function is outside the domain over which the mathematical function is defined. In this case, the value of EDOM is set in errno. The function return value in implementation-defined.
A range error occurs if the result of a function cannot be represented as a value of the double type. In this case, the value of ERANGE is set in errno. If the result overflows, the function returns the value of HUGE_VAL, HUGE_VALF, or HUGE_VALL with the same sign as the correct value of the function. If the result underflows, 0 is returned as the return value.
If there is a possibility of a domain error resulting from a <math.h> function call, it is dangerous to use the resultant value directly. The value of errno should always be checked before using the result in such cases. |
5 printf ("result is : %lf\n",x);
In line 1, the arc sine value is computed using the asin function. If the value of argument a is outside the asin function domain [–1.0, 1.0], the EDOM value is set in errno. Line 2 determines whether a domain error has occurred. If a domain error has occurred, error is output in line 3. If there is no domain error, the arc sine value is output in line 5.
Whether or not a range error occurs depends on the internal representation format of floating-point types determined by the compiler. For example, if an internal representation format that allows an infinity to be represented as a value is used, <math.h> library functions can be implemented without causing range errors. |
Implementation-Defined Specifications
Value returned by a mathematical function if an input argument is out of the range |
A not-a-number is returned. For details on the format of not-a-numbers, refer to section "(5) Floating-Point Number Specifications" in "4.1.4 Internal Data Representation and Areas". |
Whether errno is set to the value of macro ERANGE if an underflow error occurs in a mathematical function |
For the functions that set errno to the value of ERANGE, see "10.5.6 Standard Library Error Messages". The other functions do not set errno to ERANGE. |
Whether a range error occurs if the second argument in the fmod function is 0 |
For details of the return value from fmod, see "fmod/fmodf/fmodl" in "7.4.7 <math.h>". |
Calculates the arc cosine of a floating-point number.
#include <math.h>
double acos (double d)
float acosf (float d)
long double acosl (long double d);
d Floating-point number for which arc cosine is to be computed
Normal: Arc cosine of d
Abnormal: Domain error: Returns not-a-number.
A domain error occurs for a value of d not in the range [–1.0, +1.0].
The acos function returns the arc cosine in the range [0, π] by the radian.
Calculates the arc sine of a floating-point number.
#include <math.h>
double asin (double d)
float asinf (float d)
long double asinl (long double);
d Floating-point number for which arc sine is to be computed
Normal: Arc sine of d
Abnormal: Domain error: Returns not-a-number.
A domain error occurs for a value of d not in the range [–1.0, +1.0].
The asin function returns the arc sine in the range [–π/2, +π/2] by the radian.
Calculates the arc tangent of a floating-point number.
#include <math.h>
double atan (double d)
float atanf (float d)
long double atanl (long double d);
d Floating-point number for which arc tangent is to be computed
The atan function returns the arc tangent in the range (–π/2, +π/2) by the radian.
Calculates the arc tangent of the division of two floating-point numbers.
#include <math.h>
double atan2 (double y, double x)
float atan2f (float y, float x)
long double atan2l (long double y, long double x);
Normal: Arc tangent value when y is divided by x
Abnormal: Domain error: Returns not-a-number.
A domain error occurs if the values of both x and y are 0.0.
The atan2 function returns the arc tangent in the range (–π, +π) by the radian. The meaning of the atan2 function is illustrated in Figure 7.1. As shown in the figure, the result of the atan2 function is the angle between the X-axis and a straight line passing through the origin and point (x, y).
If y = 0.0 and x is negative, the result is π. If x = 0.0, the result is ±π/2, depending on whether y is positive or negative.
Calculates the cosine of a floating-point radian value.
#include <math.h>
double cos (double d)
float cosf (float d)
long double cosl (long double d);
d Radian value for which cosine is to be computed
Calculates the sine of a floating-point radian value.
#include <math.h>
double sin (double d)
float sinf (float d)
long double sinl (long double d);
d Radian value for which sine is to be computed
Calculates the tangent of a floating-point radian value.
#include <math.h>
double tan (double d)
float tanf (float d)
long double tanl (long double d);
d Radian value for which tangent is to be computed
Calculates the hyperbolic cosine of a floating-point number.
#include <math.h>
double cosh (double d)
float coshf (float d)
long double coshl (long double d);
d Floating-point number for which hyperbolic cosine is to be computed
Calculates the hyperbolic sine of a floating-point number.
#include <math.h>
double sinh (double d)
float sinhf (float d)
long double sinhl (long double d);
d Floating-point number for which hyperbolic sine is to be computed
Calculates the hyperbolic tangent of a floating-point number.
#include <math.h>
double tanh (double d)
float tanhf (float d)
long double tanhl (long double d);
d Floating-point number for which hyperbolic tangent is to be computed
Calculates the exponential function of a floating-point number.
#include <math.h>
double exp (double d)
float expf (float d)
long double expl (long double d);
d Floating-point number for which exponential function is to be computed
Exponential function value of d
Breaks a floating-point number into a [0.5, 1.0) value and a power of 2.
#include <math.h>
double frexp (double value, double long *exp);
float frexpf (float value, long * exp);
long double frexpl (long double value, long *exp);
value Floating-point number to be broken into a [0.5, 1.0) value and a power of 2
exp Pointer to storage area that holds power-of-2 value
If value is 0.0: 0.0
If value is not 0.0: Value of ret defined by ret * 2value pointed to by exp = value
The frexp function breaks value into a [0.5, 1.0) value and a power of 2. It stores the resultant power-of-2 value in the area pointed to by exp.
The frexp function returns the return value ret in the range [0.5, 1.0) or as 0.0.
If value is 0.0, the contents of the int storage area pointed to by exp and the value of ret are both 0.0.
Multiplies a floating-point number by a power of 2.
#include <math.h>
double ldexp (double e, long f);
float ldexpf (float e, long f);
long double ldexpl (long double e, long f);
e Floating-point number to be multiplied by a power of 2
f Power-of-2 value
Calculates the natural logarithm of a floating-point number.
#include <math.h>
double log (double d);
float logf (float d);
long double logl (long double d);
d Floating-point number for which natural logarithm is to be computed
Normal: Natural logarithm of d
Abnormal: Domain error: Returns not-a-number.
A domain error occurs if d is negative.
A range error occurs if d is 0.0.
Calculates the base-ten logarithm of a floating-point number.
#include <math.h>
double log10 (double d);
float log10f(float d);
long double log10l(long double d);
d Floating-point number for which base-ten logarithm is to be computed
Normal: Base-ten logarithm of d
Abnormal: Domain error: Returns not-a-number.
A domain error occurs if d is negative.
A range error occurs if d is 0.0.
Breaks a floating-point number into integral and fractional parts.
#include <math.h>
double modf (double a, double*b);
float modff (float a, float *b);
long double modfl (long double a, long double *b);
a Floating-point number to be broken into integral and fractional parts
b Pointer indicating storage area that stores integral part
Calculates a power of floating-point number.
#include <math.h>
double pow (double x, double y);
float powf (float x, float y);
long double powl (long double x, long double y);
x Value to be raised to a power
y Power value
Normal: Value of x raised to the power y
Abnormal: Domain error: Returns not-a-number.
A domain error occurs if x is 0.0 and y is 0.0 or less, or if x is negative and y is not an integer.
Calculates the positive square root of a floating-point number.
#include <math.h>
double sqrt (double d);
float sqrtf (float d);
long double sqrtl (long double d);
d Floating-point number for which positive square root is to be computed
Normal: Positive square root of d
Abnormal: Domain error: Returns not-a-number.
A domain error occurs if d is negative.
Returns the smallest integral value not less than or equal to the given floating-point number.
#include <math.h>
double ceil (double d);
float ceilf (float d);
long double ceill ( long double d);
d Floating-point number for which smallest integral value not less than that number is to be computed
Smallest integral value not less than or equal to d
The ceil function returns the smallest integral value not less than or equal to d, expressed as a double type value. Therefore, if d is negative, the value after truncation of the fractional part is returned.
Calculates the absolute value of a floating-point number.
#include <math.h>
double fabs (double d);
float fabsf (float d);
long double fabsl (long double d);
d Floating-point number for which absolute value is to be computed
Returns the largest integral value not greater than or equal to the given floating-point number.
#include <math.h>
double floor (double d);
float floorf (float d);
long double floorl (long double d);
d Floating-point number for which largest integral value not greater than that number is to be computed
Largest integral value not greater than or equal to d
The floor function returns the largest integral value not greater than or equal to d, expressed as a double type value. Therefore, if d is negative, the value after rounding-up of the fractional part is returned.
Calculates the remainder of a division of two floating-point numbers.
#include <math.h>
double fmod (double x, double y);
float fmodf (float x, float y);
long double fmodl (long double x, long double y);
When y is 0.0: x
When y is not 0.0: Remainder of division of x by y
When x is ±∞ or y is 0: Returns not-a-number and sets global variable errno to the value of macro EDOM.
In the fmod function, the relationship between parameters x and y and return value ret is as follows:
x = y * i + ret (where i is an integer)
The sign of return value ret is the same as the sign of x.
If the quotient of x/y cannot be represented, the value of the result is not guaranteed.
Calculates the hyperbolic arc cosine of a floating-point number.
#include <math.h>
double acosh(double d);
float acoshf(float d);
long double acoshl(long double d);
d Floating-point number for which hyperbolic arc cosine is to be computed
Normal: Hyperbolic arc cosine of d
Abnormal: Domain error: Returns NaN.
Error conditions: A domain error occurs when d is smaller than 1.0.
The acosh function returns the hyperbolic arc cosine in the range [0, +∞].
Calculates the hyperbolic arc sine of a floating-point number.
#include <math.h>
double asinh(double d);
float asinhf(float d);
long double asinhl(long double d);
d Floating-point number for which hyperbolic arc sine is to be computed
Calculates the hyperbolic arc tangent of a floating-point number.
#include <math.h>
double atanh(double d);
float atanhf(float d);
long double atanhl(long double d);
d Floating-point number for which hyperbolic arc tangent is to be computed
Normal: Hyperbolic arc tangent of d
Abnormal: Domain error: Returns HUGE_VAL, HUGE_VALF, or HUGE_VALL depending on the function.
Range error: Returns not-a-number.
A domain error occurs for a value of d not in the range [–1, +1]. A range error may occur for a value of d equal to –1 or 1.
Calculates the value of 2 raised to the power d.
#include <math.h>
double exp2(double d);
float exp2f(float d);
long double exp2l(long double d);
d Floating-point number for which exponential function is to be computed
Normal: Exponential function value of 2
Abnormal: Range error: Returns 0, or returns +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL depending on the function
A range error occurs if the absolute value of d is too large.
Calculates the value of natural logarithm base e raised to the power d and subtracts 1 from the result.
#include <math.h>
double expm1(double d);
float expm1f(float d);
long double expm1l(long double d) ;
d Power value to which natural logarithm base e is to be raised
Normal: Value obtained by subtracting 1 from natural logarithm base e raised to the power d
Abnormal: Range error: Returns -HUGE_VAL, –HUGE_VALF, or –HUGE_VALL depending on the function.
expm1(d) provides more accurate calculation than exp(x) – 1 even when d is near to 0.
#include <math.h>
long ilogb(double d);
long ilogbf(float d);
long ilogbl(long double d);
d Value of which exponent is to be extracted
Normal: Exponential function value of d
d is ∞: INT_MAX
d is not-a-number: FP_ILOGBNAN
d is 0: FP_ILOGBNAN
Abnormal: d is 0 and a range error has occurred: FP_ILOGB0
A range error may occur if d is 0.
Calculates the natural logarithm (base e) of d + 1.
#include <math.h>
double log1p(double d);
float log1pf(float d);
long double log1pl(long double d);
d Value for which the natural logarithm of this parameter + 1 is to be computed
Normal: Natural logarithm of d + 1
Abnormal: Domain error: Returns not-a-number.
Range error: Returns –HUGE_VAL, –HUGE_VALF, or –HUGE_VALL depending on the function.
A domain error occurs if d is smaller than –1.
A range error occurs if d is –1.
log1p(d) provides more accurate calculation than log(1+d) even when d is near to 0.
Calculates the base-2 logarithm of d.
#include <math.h>
double log2(double d);
float log2f(float d);
long double log2l(long double d);
d Value of which logarithm is to be calculated
Normal: Base-2 logarithm of d
Abnormal: Domain error: Returns not-a-number.
A domain error occurs if d is a negative value.
Extracts the exponent of d in internal floating-point representation, as a floating-point value.
#include <math.h>
double logb(double d);
float logbf(float d);
long double logbl(long double d);
d Value of which exponent is to be extracted
Abnormal: Range error: Returns –HUGE_VAL, –HUGE_VALF, or –HUGE_VALL depending on the function.
A range error may occur if d is 0.
d is always assumed to be normalized.
Calculates a floating-point number multiplied by a power of radix, which is an integer.
#include <math.h>
double scalbn(double d, long e);
float scalbnf(float d, long e);
long double scalbnl(long double d, long e);
double scalbln(double d, long e);
float scalblnf(float d, long int e);
long double scalblnl(long double d, long int e);
d Value to be multiplied by FLT_RADIX raised to the power e
e Exponent used to compute a power of FLT_RADIX
Normal: Value equal to d multiplied by FLT_RADIX
Abnormal: Range error: Returns –HUGE_VAL, –HUGE_VALF, or –HUGE_VALL depending on the function.
A range error may occur if d is 0.
FLT_RADIX raised to the power e is not actually calculated.
Calculates the cube root of a floating-point number.
#include <math.h>
double cbrt(double d);
float cbrtf(float d);
long double cbrtl(long double d);
d Value for which a cube root is to be computed
Calculates the square root of the sum of floating-point numbers raised to the power 2.
#include <math.h>
double hypot(double d, double e);
float hypotf(float d, double e);
long double hypotl(long double d, double e);
d Values for which the square root of the sum of these values
e raised to the power 2 is to be computed
Normal: Square root function value of sum of d raised to the power 2 and e raised to the power 2
Abnormal: Range error: Returns HUGE_VAL, HUGE_VALF, or HUGE_VALL depending on the function.
A range error may occur if the result overflows.
Calculates the error function value of a floating-point number.
#include <math.h>
double erf(double d);
float erff(float d);
long double erfl(long double d);
d Value for which the error function value is to be computed
Calculates the complementary error function value of a floating-point number.
#include <math.h>
double erfc(double d);
float erfcf(float d);
long double erfcl(long double d);
d Value for which the complementary error function value is to be computed
Complementary error function value of d
A range error occurs if the absolute value of d is too large.
Calculates the logarithm of the gamma function of a floating-point number.
#include <math.h>
double lgamma(double d);
float lgammaf(float d);
long double lgammal(long double d);
d Value for which the logarithm of the gamma function is to be computed
Normal: Logarithm of gamma function of d
Abnormal: Domain error: Returns HUGE_VAL, HUGE_VALF, or HUGE_VALL with the mathematically correct sign.
Range error: Returns +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL.
A range error is set if the absolute value of d is too large or small.
A domain error occurs if d is a negative integer or 0 and the calculation result is not representable.
Calculates the gamma function of a floating-point number.
#include <math.h>
double tgamma(double d);
float tgammaf(float d);
long double tgammal(long double d);
d Value for which the gamma function value is to be computed
Normal: Gamma function value of d
Abnormal: Domain error: Returns HUGE_VAL, HUGE_VALF, or HUGE_VALL with the same sign as that of d.
Range error: Returns 0, or returns +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL with the mathematically correct sign depending on the function.
A range error is set if the absolute value of d is too large or small.
A domain error occurs if d is a negative integer or 0 and the calculation result is not representable.
Rounds a floating-point number to an integer in the floating-point representation according to the current rounding direction.
#include <math.h>
double nearbyint(double d);
float nearbyintf(float d);
long double nearbyintl(long double d);
d Value to be rounded to an integer in the floating-point format
d rounded to an integer in the floating-point format
The nearbyint function group does not generate "inexact" floating-point exceptions.
Rounds a floating-point number to an integer in the floating-point representation according to the current rounding direction.
#include <math.h>
double rint(double d);
float rintf(float d);
long double rintl(long double d);
d Value to be rounded to an integer in the floating-point format
d rounded to an integer in the floating-point format
The rint function group differs from the nearbyint function group only in that the ring function group may generate "inexact" floating-point exceptions.
Rounds a floating-point number to the nearest integer according to the current rounding direction.
#include <math.h>
long int lrint(double d);
long int lrintf(float d);
long int lrintl(long double d);
long long int llrint(double d);
long long int llrintf(float d);
long long int llrintl(long double d);
d Value to be rounded to an integer
Normal: d rounded to an integer
Abnormal: Range error: Returns an undetermined value.
A range error may occur if the absolute value of d is too large.
The return value is unspecified when the rounded value is not in the range of the return value type.
Rounds a floating-point number to the nearest integer.
#include <math.h>
double round(double d);
float roundf(float d);
long double roundl(long double d);
long int lround(double d);
long int lroundf(float d);
long int lroundl(long double d);
long long int llround (double d);
long long int llroundf(float d);
long long int llroundl(long double d);
d Value to be rounded to an integer
Normal: d rounded to an integer
Abnormal: Range error: Returns an undetermined value.
A range error may occur if the absolute value of d is too large.
When d is at the midpoint between two integers, the lround function group selects the integer farther from 0 regardless of the current rounding direction. The return value is unspecified when the rounded value is not in the range of the return value type.
Rounds a floating-point number to the nearest integer in the floating-point representation.
#include <math.h>
double trunc(double d);
float truncf(float d);
long double truncl(long double d);
d Value to be rounded to an integer in the floating-point representation
d truncated to an integer in the floating-point format
The trunc function group rounds d so that the absolute value after rounding is not greater than the absolute value of d.
Calculates the remainder of a division of two floating-point numbers.
#include <math.h>
double remainder(double d1, double d2);
float remainderf(float d1, float d2);
long double remainderl(long double d1, long double d2);
d1, d2 Values for which remainder of a division is to be computed (d1 / d2)
Remainder of division of d1 by d2
The remainder calculation by the remainder function group conforms to the IEEE 60559 standard.
Calculates the remainder of a division of two floating-point numbers.
#include <math.h>
double remquo(double d1, double d2, long *q);
float remquof(float d1, float d2, long *q);
long double remquol(long double d1, long double d2, long *q);
d1, d2 Values for which remainder of a division is to be computed (d1 / d2)
q Value pointing to the location to store the quotient obtained by remainder calculation
Remainder of division of d1 by d2
The value stored in the location indicated by q has the same sign as the result of x/y and the integral quotient of modulo-2n x/y (n is an implementation-defined integer equal to or greater than 3).
Generates a value consisting of the absolute value of d1 and the sign of d2.
#include <math.h>
double copysign(double d1, double d2);
float copysignf(float d1, float d2);
long double copysignl(long double d1, long double d2);
d1 Value of which absolute value is to be used in the generated value
d2 Value of which sign is to be used in the generated value
Normal: Value consisting of absolute value of d1 and sign of d2
Abnormal: Range error: Returns an undetermined value.
When d1 is a not-a-number, the copysign function group generates a not-a-number with the sign bit of d2.
#include <math.h>
double nan(const char *c);
float nanf(const char *c);
long double nanl(const char *c);
qNaN with the contents of the location indicated by c or 0 (when qNaN is not supported)
The nan("c string") call is equivalent to strtod("NAN(c string)", (char**) NULL). The nanf and nanl calls are equivalent to the corresponding strtof and strtold calls, respectively.
Calculates the next floating-point representation following d1 in the direction to d2 on the real axis.
#include <math.h>
double nextafter(double d1, double d2);
float nextafterf(float d1, float d2);
long double nextafterl(long double d1, long double d2);
d1 Floating-point value on the real axis
d2 Value indicating the direction viewed from d1, in which a representable floating-point value is to be found
Normal: Representable floating-point value
Abnormal: Range error: Returns HUGE_VAL, HUGE_VALF, or HUGE_VALL with the mathematically correct sign depending on the function.
A range error may occur if d1 is the maximum finite value that can be represented in its type and the return value is an infinity or cannot be represented in its type.
The nextafter function group returns d2 when d1 is equal to d2.
Calculates the next floating-point representation following d1 in the direction to d2 on the real axis.
#include <math.h>
double nexttoward(double d1, long double d2);
float nexttowardf(float d1, long double d2);
long double nexttowardl(long double d1, long double d2);
d1 Floating-point value on the real axis
d2 Value indicating the direction viewed from d1, in which a representable floating-point value is to be found
Normal: Representable floating-point value
Abnormal: Range error: Returns HUGE_VAL, HUGE_VALF, or HUGE_VALL with the mathematically correct sign depending on the function
A range error may occur if d1 is the maximum finite value that can be represented in its value and the return value is an infinity or cannot be represented in its type.
The nexttoward function group is equivalent to the nextafter function group except that d2 is of type long double and returns d2 after conversion depending of the function when d1 is equal to d2.
Calculates the positive difference between two arguments.
#include <math.h>
double fdim(double d1, double d2);
float fdimf(float d1, float d2);
long double fdiml(long double d1, long double d2);
d1, d2 Values of which difference is to be computed (|d1 - d2|)
Normal: Positive difference between two arguments
Abnormal: Range error: HUGE_VAL, HUGE_VALF, or HUGE_VALL
A range error may occur if the return value overflows.
Obtains the greater of two arguments.
#include <math.h>
double fmax(double d1, double d2);
float fmaxf(float d1, float d2);
long double fmaxl(long double d1, long double d2);
The fmax function group recognizes a not-a-number as a lack of data. When one argument is a not-a-number and the other is a numeric value, the function returns the numeric value.
Obtains the smaller of two arguments.
#include <math.h>
double fmin(double d1, double d2);
float fminf(float d1, float d2);
long double fminl(long double d1, long double d2);
The fmin function group recognizes a not-a-number as a lack of data. When one argument is a not-a-number and the other is a numeric value, the function returns the numeric value.
Calculates (d1 * d2) + d3 as a single ternary operation.
#include <math.h>
double fma(double d1, double d2, double d3);
float fmaf(float d1, float d2, float d3);
long double fmal(long double d1, long double d2, long double d3);
Result of (d1 * d2) + d3 calculated as ternary operation
d1, d2, d3 Floating-point values
The fma function group performs calculation as if infinite precision is available and rounds the result only one time in the rounding mode indicated by FLT_ROUNDS.