C++中的 sqrt、sqrtl 和 sqrtf

文章介绍了C++中的三种数学函数sqrt(),sqrtf()和sqrtl(),分别处理double,float和longdouble类型的平方根计算,展示了它们在精度上的区别和使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、sqrt()函数

此功能用于双精度数据。因此,这将返回double类型的平方根。

语法如下:

double sqrt(double argument)

例:

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
 double x = 144.0;
 double y = 180.0;
 cout << fixed << setprecision(12) << sqrt(x) << endl;
 cout << fixed << setprecision(12) << sqrt(y) << endl;
}

输出:

12.000000000000
13.416407864999

二、sqrtf()函数

该功能用于浮点型数据。因此,这将返回float类型的平方根。

语法如下:

float sqrtf(float argument)

例:

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
 float x = 144.0;
 float y = 180.0;
 cout << fixed << setprecision(6) << sqrtf(x) << endl;
 cout << fixed << setprecision(6) << sqrtf(y) << endl;
}

输出:

12.000000
13.416408

三、sqrtl()函数

此功能用于长双精度型数据。因此,这将返回long double类型的平方根。这是精度更高的两倍。当我们使用长整数时,此函数很有用。

语法如下:

long double sqrtl(long double argument)

例:

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
 long long int x = 5000000000000000000;
 long long int y = 999999999999999999;
 cout << fixed << setprecision(12) << sqrtl(x) << endl;
 cout << fixed << setprecision(12) << sqrtl(y) << endl;
}

输出:

2236067977.499789696420
999999999.999999999476

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hiOoo.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值