赞
踩
<cmath>
是 C++ 标准库中的头文件,它提供了一系列数学函数和常量,用于执行各种数学计算。在 C++ 中,<cmath>
头文件中定义的函数和常量都位于 std
命名空间中。
以下是 <cmath>
头文件中常用的一些函数和常量:
三角函数:
sin
, cos
, tan
:求正弦、余弦、正切值。asin
, acos
, atan
:求反正弦、反余弦、反正切值。atan2
:求两个参数的反正切值。双曲函数:
sinh
, cosh
, tanh
:求双曲正弦、双曲余弦、双曲正切值。asinh
, acosh
, atanh
:求反双曲正弦、反双曲余弦、反双曲正切值。指数和对数函数:
exp
, log
, log10
:求指数、自然对数、常用对数。log2
:求以2为底的对数。pow
:求幂函数。取整函数:
ceil
:向上取整。floor
:向下取整。round
:四舍五入取整。其他函数:
sqrt
:求平方根。fabs
:求绝对值。fmod
:求浮点数取模。M_PI
:π(圆周率)。M_E
:e(自然对数的底)。除了上述函数和常量,<cmath>
还包含了其他一些数学函数和常量,可根据具体需求进行查阅和使用。这些函数和常量能够帮助你在 C++ 中进行各种数学计算。
- #include <iostream>
- #include <cmath>
-
- int main() {
- // 计算平方根
- double x = 16.0;
- double squareRoot = std::sqrt(x);
- std::cout << "Square root of " << x << " is: " << squareRoot << std::endl;
-
- // 计算正弦值
- double angle = 45.0; // 角度
- double radians = angle * M_PI / 180.0; // 将角度转换为弧度
- double sineValue = std::sin(radians);
- std::cout << "Sine of " << angle << " degrees is: " << sineValue << std::endl;
-
- // 计算自然对数
- double num = 2.0;
- double naturalLog = std::log(num);
- std::cout << "Natural logarithm of " << num << " is: " << naturalLog << std::endl;
-
- // 计算指数
- double base = 2.0;
- double exponent = 3.0;
- double result = std::pow(base, exponent);
- std::cout << base << " raised to the power of " << exponent << " is: " << result << std::endl;
-
-
- // 向上取整
- double y = 4.3;
- double ceilValue = std::ceil(y);
- std::cout << "Ceil value of " << y << " is: " << ceilValue << std::endl;
-
- return 0;
- }

参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。