cmath
头文件
cmath是c++语言中的标准库头文件。其中的 “c” 表示其中的函数是来自 C标准库,“math”表示为数学常用库函数。此文件原作为<math.h>
存在C标准库中。
基本运算
函数 | 作用 | 示例 |
---|---|---|
abs(float) | 取绝对值 | cout<<abs(-3.21)<<endl |
fmod ( x, y ); | 此函数计算的除法 x/y 的浮点余数是 x - n*y 的准确值,其中 n 是截断小数部分的 x/y | cout<<fmod(+6.21,+2.1)<<endl |
remainder(x,y) | 此函数所计算的除法运算 x/y为值 x - n*y ,其中值 n 是最接近 x/y 准确值的整数值。 | cout << remainder(+5.1, +3.0) <<endl; |
exp()、expf()、expl() | 返回 e 的给定次幂 | cout<<exp(2)<<endl; |
exp2()、exp2f()、exp2l() | 计算 2 的给定 n 次幂 | cout<<exp2(2)<<endl; |
log()、logf()、logl() | 计算 arg 的自然(底 e )对数 | cout<<log(1)<<endl; |
log10()、log10f()、log10l() | 计算常用(以 10 为底)对数 | cout<<log10(100)<<endl; |
log1p(float arg)、log1pf(float arg)、log1pl(float arg) | 计算 1+arg 的自然(底 e )对数 | cout<<log1p(3)<<endl; |
pow(float x,float y) | 计算x的y次方 | cout<<pow(2,10)<<endl; |
sqrt(float x) | 计算x的平方根 | cout<<sqrt(4)<<endl; |
cbrt(float x) | 计算x的立方根 | cout<<cbrt(8)<<endl; |
hypot(float x,float y) | 计算 x 与 y 平方和的平方根 | cout<<hypot(3,4)<<endl; |
sin(float x)、cos(float x)、tan(float x)、asin(float x)、acos(float x)、atan(float x) | 计算正弦(sin x)、余弦(cos x)、正切(tanx)、反正弦(arcsin x)、反余弦(arccos x)、反正切(arctan x) | cout<<arcsin(-1)<<endl; |