C++中的math库提供了一些数学函数和常量,可以用来进行数学运算和计算。使用math库需要包含头文件
以下是一些常用的math库函数和常量:
数学函数:sqrt(x):计算x的平方根pow(x, y):计算x的y次幂sin(x)、cos(x)、tan(x):计算x的正弦、余弦、正切值log(x)、log10(x):计算x的自然对数和以10为底的对数exp(x):计算e的x次幂ceil(x)、floor(x):对x进行向上取整和向下取整常量:M_PI:圆周率π的值M_E:自然对数的底e的值示例代码:
#include <iostream>#include <cmath>int main() { double x = 2.5; std::cout << "Square root of " << x << " is " << sqrt(x) << std::endl; std::cout << "2 to the power of 3 is " << pow(2, 3) << std::endl; std::cout << "sin(PI/2) is " << sin(M_PI/2) << std::endl; return 0;}以上是math库的基本使用方法,更多详细的函数和常量可以查阅C++标准库的文档。


