sqrt(x) 就是对X去根号的值,需要引入#include “math.h”;
sqrt():
功 能: 一个非负实数的平方根
函数原型: 在VC6.0中的math.h头文件的函数原型为double sqrt(double);
说明:sqrt系Square Root Calculations(平方根计算),通过这种运算可以考验CPU的浮点能力。
例如:
#include <iostream>
//这里的cmath等价于C的math.h
#include <cmath>
using namespace std;
int main()
{
double x=4.0,result;
result=sqrt(x);
cout<<"4的平方根是"<<result<<endl;
return 0;
}
#include
//这里的cmath等价于C的math.h
#include
using namespace std;
int main()
{
double x=4.0,result;
result=sqrt(x);
cout<<“4的平方根是”<<result<<endl;
return 0;
}
//cmath等价于math.h,其就是using math.h的函数
//VC 2008后为重载函数,原型为 float sqrt (float),double sqrt (double),double long sqrt(double long)
//注意没有sqrt (int),但是返回值可以为int
经过实验,发现sqrt(int)也是可以的,应该是自动把int转为double了
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a=2;
double b=sqrt(a);
cout<<b<<endl; //输出1.41421
}
本文详细介绍了C++中sqrt()函数的使用方法,包括如何计算非负实数的平方根,以及在不同编译器环境下sqrt()函数的原型变化。通过示例代码展示了如何在程序中调用sqrt()函数,并解释了sqrt()函数在评估CPU浮点能力方面的作用。
可以开根号)&spm=1001.2101.3001.5002&articleId=101014956&d=1&t=3&u=0ec818b24e794eae84548e1c9ef0593a)
3210

被折叠的 条评论
为什么被折叠?



