C++ Math库函数介绍与使用示例
C++标准库中的<cmath>
头文件提供了许多常用的数学函数。这些函数主要用于执行基本数学运算、三角函数、指数和对数运算等。
1. 基本数学运算函数
abs() - 绝对值
#include <iostream>
#include <cmath>
int main() {
int x = -5;
double y = -3.14;
std::cout << "abs(" << x << ") = " << abs(x) << std::endl;
std::cout << "abs(" << y << ") = " << abs(y) << std::endl;
return 0;
}
fabs() - 浮点数绝对值
#include <iostream>
#include <cmath>
int main() {
double x = -7.25;
std::cout << "fabs(" << x << ") = " << fabs(x) << std::endl;
return 0;
}
fmod() - 浮点数取模
#include <iostream>
#include <cmath>
int main() {
double x = 10.5;
double y = 3.2;
std::cout << "fmod(" << x << ", " << y << ") = " << fmod(x, y) << std::endl;
return 0;
}
remainder() - 带符号的余数
#include <iostream>
#include <cmath>
int main() {
double x = 10.5;
double y = 3.2;
std::cout << "remainder(" << x << ", " << y << ") = " << remainder(x, y) << std::endl;
return 0;
}
2. 幂函数与对数函数
pow() - 幂运算
#include <iostream>
#include <cmath>
int main() {
double base = 2.0;
double exponent = 3.0;
std::cout << base << " raised to the power of " << exponent << " is " << pow(base, exponent) << std::endl;
return 0;
}
sqrt() - 平方根
#include <iostream>
#include <cmath>
int main() {
double x = 25.0;
std::cout << "Square root of " << x << " is " << sqrt(x) << std::endl;
return 0;
}
cbrt() - 立方根
#include <iostream>
#include <cmath>
int main() {
double x = 27.0;
std::cout << "Cube root of " << x << " is " << cbrt(x) << std::endl;
return 0;
}
exp() - 自然指数函数
#include <iostream>
#include <cmath>
int main() {
double x = 1.0;
std::cout << "exp(" << x << ") = " << exp(x) << std::endl;
return 0;
}
log() - 自然对数
#include <iostream>
#include <cmath>
int main() {
double x = 2.71828;
std::cout << "log(" << x << ") = " << log(x) << std::endl;
return 0;
}
log10() - 常用对数(以10为底)
#include <iostream>
#include <cmath>
int main() {
double x = 100.0;
std::cout << "log10(" << x << ") = " << log10(x) << std::endl;
return 0;
}
3. 三角函数
sin(), cos(), tan() - 正弦、余弦、正切
#include <iostream>
#include <cmath>
int main() {
double angle = 45.0; // 角度
double radians = angle * M_PI / 180.0; // 转换为弧度
std::cout << "sin(" << angle << "°) = " << sin(radians) << std::endl;
std::cout << "cos(" << angle << "°) = " << cos(radians) << std::endl;
std::cout << "tan(" << angle << "°) = " << tan(radians) << std::endl;
return 0;
}
asin(), acos(), atan() - 反正弦、反余弦、反正切
#include <iostream>
#include <cmath>
int main() {
double value = 0.5;
std::cout << "asin(" << value << ") = " << asin(value) * 180.0 / M_PI << "°" << std::endl;
std::cout << "acos(" << value << ") = " << acos(value) * 180.0 / M_PI << "°" << std::endl;
std::cout << "atan(" << value << ") = " << atan(value) * 180.0 / M_PI << "°" << std::endl;
return 0;
}
atan2() - 两个参数的反正切
#include <iostream>
#include <cmath>
int main() {
double y = 1.0;
double x = 1.0;
std::cout << "atan2(" << y << ", " << x << ") = " << atan2(y, x) * 180.0 / M_PI << "°" << std::endl;
return 0;
}
4. 双曲函数
sinh(), cosh(), tanh() - 双曲正弦、双曲余弦、双曲正切
#include <iostream>
#include <cmath>
int main() {
double x = 1.0;
std::cout << "sinh(" << x << ") = " << sinh(x) << std::endl;
std::cout << "cosh(" << x << ") = " << cosh(x) << std::endl;
std::cout << "tanh(" << x << ") = " << tanh(x) << std::endl;
return 0;
}
5. 舍入与取整函数
ceil() - 向上取整
#include <iostream>
#include <cmath>
int main() {
double x = 2.3;
std::cout << "ceil(" << x << ") = " << ceil(x) << std::endl;
return 0;
}
floor() - 向下取整
#include <iostream>
#include <cmath>
int main() {
double x = 2.7;
std::cout << "floor(" << x << ") = " << floor(x) << std::endl;
return 0;
}
round() - 四舍五入
#include <iostream>
#include <cmath>
int main() {
double x1 = 2.3;
double x2 = 2.7;
std::cout << "round(" << x1 << ") = " << round(x1) << std::endl;
std::cout << "round(" << x2 << ") = " << round(x2) << std::endl;
return 0;
}
trunc() - 截断小数部分
#include <iostream>
#include <cmath>
int main() {
double x = 3.7;
std::cout << "trunc(" << x << ") = " << trunc(x) << std::endl;
return 0;
}
6. 其他函数
hypot() - 计算直角三角形的斜边
#include <iostream>
#include <cmath>
int main() {
double x = 3.0;
double y = 4.0;
std::cout << "hypot(" << x << ", " << y << ") = " << hypot(x, y) << std::endl;
return 0;
}
copysign() - 复制符号位
#include <iostream>
#include <cmath>
int main() {
double x = 5.0;
double y = -2.0;
std::cout << "copysign(" << x << ", " << y << ") = " << copysign(x, y) << std::endl;
return 0;
}
nextafter() - 获取下一个可表示的值
#include <iostream>
#include <cmath>
#include <iomanip>
int main() {
double from = 1.0;
double to = 2.0;
std::cout << std::setprecision(20);
std::cout << "nextafter(" << from << ", " << to << ") = " << nextafter(from, to) << std::endl;
return 0;
}
注意事项
- 大多数数学函数都有针对float和long double的版本,如
sinf()
,sinl()
等。 - 使用三角函数时,注意参数是以弧度为单位,不是角度。
- 某些函数在特定输入下可能返回NaN(非数字)或无穷大,使用时应注意错误处理。
- 对于整数运算,C++还提供了
<cstdlib>
中的abs()函数,专门用于整数类型。