math.h
The math.h
header file provides a number of mathematical functions.
Many math functions return values of type double, but offer both float and long double versions, for example the pow()` function has both
powf() and ``powl()
versions.
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
For brevity, the f
suffixed (float type) and l
suffixed (long double) versions of the functions are omitted below.
Types and macros
Two new type aliases are defined in math.h.
- float_t: the type that (on the current system) most efficiently performs float operations, and is at least as wide as float.
- double_t: the type that (on the current system) most efficiently performs double operations, and is at least as wide as double.
Their specific types can be found with the macro FLT_EVAL_METHOD
.
The value of FLT_EVAL_METHOD | The type corresponding to float_t | The type corresponding to double_t |
---|---|---|
0 | float | double |
1 | double | double |
2 | long double | long double |
other | determined by implementation | determined by implementation |
math.h also defines a number of macros.
INFINITY
: means positive infinity, returns a float type value.NAN
: indicates a non-number (Not-A-Number) and returns a float type value.
Error types
The following types of errors are reported for mathematical functions.
- Range errors: The result of an operation cannot be represented by the function return type.
- Domain errors: the function argument does not apply to the current function.
- Pole errors: The argument causes the limit of the function to become infinite.
- Overflow errors: The result of an operation is too large, resulting in an overflow.
- Underflow errors: The result of an operation is too small, resulting in an overflow.
The variable math_errhandling
indicates how the current system handles mathematical errors.
value of math_errhandling | description |
---|---|
MATH_ERRNO | The system uses errno to indicate a mathematical error. |
MATH_ERREXCEPT | The system uses exceptions to indicate math errors |
MATH_ERREXCEPT | The system uses both to indicate a maths error |
Numeric Types
The arguments to mathematical functions can be divided into the following categories: normal, infinite, finite and non-numeric.
The following function is used to determine the type of a value.
- fpclassify(): returns the classification of the given floating point number.
- isfinite(): true if the argument is not infinite or NaN.
- isinf(): true if the argument is infinite.
- isnan(): true if the argument is not a number.
- isnormal(): true if the argument is a normal number.
Here is an example.
isfinite(1.23) // 1
isinf(1/tan(0)) // 1
isnan(sqrt(-1)) // 1
isnormal(1e-310)) // 0
signbit()
signbit()
determines if the argument is signed. Returns 1 if the argument is negative, 0 otherwise.
signbit(3490.0) // 0
signbit(-37.0) // 1
Trigonometric functions
The following are trigonometric functions, with arguments in radians.
- acos():反余弦。
- asin():反正弦。
- atan():反正切
- atan2():反正切。
- cos():余弦。
- sin():正弦。
- tan():正切。
不要忘了,上面所有函数都有 float 版本(函数名加上 f 后缀)和 long double 版本(函数名加上 l 后缀)。
下面是一个例子。
cos(PI/4) // 0.707107
双曲函数
以下是双曲函数,参数都为浮点数。
- acosh():反双曲余弦。
- asinh():反双曲正弦。
- atanh():反双曲正切。
- cosh():双曲余弦。
- tanh():双曲正切。
- sinh():双曲正弦。
指数函数和对数函数
以下是指数函数和对数函数,它们的返回值都是 double 类型。
- exp():计算欧拉数 e 的乘方,即 ex。
- exp2():计算 2 的乘方,即 2x。
- expm1():计算 ex - 1。
- log():计算自然对数,
exp()
的逆运算。 - log2():计算以2为底的对数。
- log10():计算以10为底的对数。
- logp1():计算一个数加 1 的自然对数,即
ln(x + 1)
。 - logb():计算以宏
FLT_RADIX
(一般为2)为底的对数,但只返回整数部分。
下面是一些例子。
exp(3.0) // 20.085500
log(20.0855) // 3.000000
log10(10000) // 3.000000
如果结果值超出了 C 语言可以表示的最大值,函数将返回HUGE_VAL
,它是一个在math.h
中定义的 double 类型的值。
如果结果值太小,无法用 double 值表示,函数将返回0。以上这两种情况都属于出错。
frexp()
frexp()
将参数分解成浮点数和指数部分(2为底数),比如 1234.56 可以写成 0.6028125 * 211,这个函数就能分解出 0.6028125 和 11。
double frexp(double value, int* exp);
它接受两个参数,第一个参数是用来分解的浮点数,第二个参数是一个整数变量指针。
它返回小数部分,并将指数部分放入变量exp
。 如果参数为0
,则返回的小数部分和指数部分都为0
。
下面是一个例子。
double frac;
int expt;
// expt 的值是 11
frac = frexp(1234.56, &expt);
// 输出 1234.56 = 0.6028125 x 2^11
printf("1234.56 = %.7f x 2^%d\n", frac, expt);
ilogb()
ilogb()
返回一个浮点数的指数部分,指数的基数是宏FLT_RADIX
(一般是2
)。
int ilogb(double x);
它的参数为x
,返回值是 logr|x|,其中r
为宏FLT_RADIX
。
下面是用法示例。
ilogb(257) // 8
ilogb(256) // 8
ilogb(255) // 7
ldexp()
ldexp()
将一个数乘以2的乘方。 它可以看成是frexp()
的逆运算,将小数部分和指数部分合成一个f * 2^n
形式的浮点数。
double ldexp(double x, int exp);
It takes two arguments, the first being the multiplier x
and the second being the exponential part of 2, exp
, and returns "x * 2exp".
ldexp(1, 10) // 1024.000000
ldexp(3, 2) // 12.000000
ldexp(0.75, 4) // 12.000000
ldexp(0.5, -1) // 0.250000
modf()
modf()
函数提取一个数的整数部分和小数部分。
double modf(double value, double* iptr);
它接受两个参数,第一个参数value
表示待分解的数值,第二个参数是浮点数变量iptr
。 返回值是value
的小数部分,整数部分放入变量double
。
下面是一个例子。
// int_part 的值是 3.0
modf(3.14159, &int_part); // 返回 0.14159
scalbn()
scalbn()
用来计算“x * rn”,其中r
是宏FLT_RADIX
。
double scalbn(double x, int n);
它接受两个参数,第一个参数x
是乘数部分,第二个参数n
是指数部分,返回值是“x * rn”。
下面是一些例子。
scalbn(2, 8) // 512.000000
这个函数有多个版本。
- scalbn():指数 n 是 int 类型。
- scalbnf():float 版本的 scalbn()。
- scalbnl():long double 版本的 scalbn()。
- scalbln():指数 n 是 long int 类型。
- scalblnf():float 版本的 scalbln()。
- scalblnl(): the long double version of scalbln().
round()
The round()` function rounds in the traditional way, e.g.
1.5` rounds to 2
and -1.5
rounds to -2
.
``c double round(double x);
It returns a floating point number.
Here are some examples.
```c
round(3.14) // 3.000000
round(3.5) // 4.000000
round(-1.5) // -2.000000
round(-1.14) // -1.000000
It also has some other versions.
- lround(): the return value is of type long int.
- llround(): the return value is of type long long int.
trunc()
`trunc()
is used to truncate the fractional part of a floating point number and return the remaining integer part as a floating point number.
double trunc(double x);
Here are some examples.
trunc(3.14) // 3.000000
trunc(3.8) // 3.000000
trunc(-1.5) // -1.000000
trunc(-1.14) // -1.000000
ceil()
`ceil()
returns the smallest integer (of type double) that is not less than its argument, which is "rounded up".
double ceil(double x);
Here are some examples.
ceil(7.1) // 8.0
ceil(7.9) // 8.0
ceil(-7.1) // -7.0
ceil(-7.9) // -7.0
floor()
`floor()
returns the largest integer that is not greater than its argument, which is "rounded down".
double floor(double x);
Here are some examples.
floor(7.1) // 7.0
floor(7.9) // 7.0
floor(-7.1) // -8.0
floor(-7.9) // -8.0
The following function does the "rounding".
double round_nearest(double x) {
return x < 0.0 ? ceil(x - 0.5) : floor(x + 0.5);
}
fmod()
fmod()` returns the remainder of the first argument divided by the second argument, which is the floating point version of the remainder operator
%, since ``%
can only be used for integer operations.
double fmod(double x, double y);
The calculation it performs behind the scenes is x - trunc(x / y) * y
, and the return value has the same sign as x
.
fmod(5.5, 2.2) // 1.100000
fmod(-9.2, 5.1) // -4.100000
fmod(9.2, 5.1) // 4.100000
Floating point comparison functions
The following functions are used to compare two floating point numbers, with the return value being of type integer.
- isgreater(): returns the result of
x > y
. - isgreaterequal(): returns
x >= y
. - isless(): returns
x < y
. - islessequal(): returns
x <= y
. - islessgreater(): returns the result of
(x < y) || (x > y)
.
Here are some examples.
isgreater(10.0, 3.0) // 1
isgreaterequal(10.0, 10.0) // 1
isless(10.0, 3.0) // 0
islessequal(10.0, 3.0) // 0
islessgreater(10.0, 3.0) // 1
islessgreater(10.0, 30.0) // 1
islessgreater(10.0, 10.0) // 0
isunordered()
`isunordered()
returns whether NAN is present in one of the two arguments.
int isunordered(any_floating_type x, any_floating_type y);
Here are some examples.
isunordered(1.0, 2.0) // 0
isunordered(1.0, sqrt(-1)) // 1
isunordered(NAN, 30.0) // 1
isunordered(NAN, NAN) // 1
Other functions
The following are the other functions included in math.h.
- pow(): computes the
y
th power of the argumentx
. - sqrt(): calculates the square root of a number.
- cbrt(): calculates the cube root.
- fabs(): calculates the absolute value.
- hypot(): calculates the hypotenuse, based on the two right-angled sides of a right triangle.
- fmax(): returns the maximum of the two arguments.
- fmin(): returns the minimum value of the two arguments.
- remainder(): returns the remainder of the IEC 60559 standard, similar to
fmod()
, but the remainder ranges from-y/2
toy/2
rather than from0
toy
. - remquo(): returns both the remainder and the quotient, the remainder is calculated in the same way as
remainder()
. - copysign(): returns a value whose size is equal to the first argument and whose sign is equal to the second argument.
- nan(): returns NAN.
- nextafter(): gets the next (or previous, depending on the direction of the second argument,
y
) floating point value that the current system can represent. - nextoward(): same as
nextafter()
, except that the second argument is of type long double. - fdim(): returns the difference if the first argument minus the second argument is greater than
0
, otherwise it returns0
. - fma(): returns the result of
x * y + z
in a quick calculation. - nearbyint(): rounds to the nearest integer in the current rounding direction. The current rounding direction can be set using the
fesetround()
function. - rint(): rounds to the nearest integer in the current rounding direction, same as
nearbyint()
. The difference is that it throws anINEXACT
exception for floating point numbers. - lrint(): rounds to the nearest integer in the current rounding direction, same as
rint()
. The difference is that the return value is an integer, not a floating point number. - erf(): error function to calculate a value.
- erfc(): computes a complementary error function for a value.
- tgamma(): calculates the Gamma function.
- lgamma(): calculates the natural logarithm of the absolute value of the Gamma function.
Here are some examples.
pow(3, 4) // 81.000000
sqrt(3.0) // 1.73205
cbrt(1729.03) // 12.002384
fabs(-3490.0) // 3490.000000
hypot(3, 4) // 5.000000
fmax(3.0, 10.0) // 10.000000
fmin(10.0, 3.0) // 3.000000
```''