本篇介绍C++语言中一些常用的数学运算API,这是系统自己封装好的。
/*
* Author:W;
* 常用的数学运算
*/
//引入头文件:头文件包含了程序中必需的或有用的信息【单行注释】
#include <iostream>
//引入数学计算库文件
#include <cmath>
//命名空间使用
using namespace std;
//main程序执行入口函数
int main()
{
double angle = 235.55f;
double v1 = 2.5f;
double v2 = 3.5f;
int num = -199;
int num2 = 255;
//余弦
cout << "cos(" << angle << ") = "<< cos(angle) << endl;
//正弦
cout << "sin(" << angle << ") = " << sin(angle) << endl;
//正切
cout << "tan(" << angle << ") = " << tan(angle) << endl;
//自然对数
cout << "log(" << v1 << ") = " << log(v1) << endl;
//x的y次方
cout << "pow(" << num << "," << v1 << ") = " << pow(num, v1) << endl;
//平方总和的平方根
cout << "hypot(" << v1 << "," << v2 << ") = " << hypot(v1,v2) << endl;
//平方根
cout << "sqrt(" << num2 << ") = " << sqrt(num2) << endl;
//整数的绝对值
cout << "abs(" << num << ") = " << abs(num) << endl;
//浮点数的绝对值
cout << "fabs(" << v1 << ") = " << fabs(v1) << endl;
//小于或等于传入值的最大整数
cout << "floor(" << v2 << ") = " << floor(v2) << endl;
}
运行结果如下