C++中怎么表示根号下的数字(用cmath中的sqrt()可以开根号)

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
 } 
  • 22
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值