当前位置:网站首页>sqrt()函数的详解和用法「建议收藏」
sqrt()函数的详解和用法「建议收藏」
2022-06-28 18:02:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
sqrt()函数是我们经常使用的一个函数。下面我就详细的介绍它的一些用法和使用规范
所需的头文件
#include<math.h>函数原型
double sqrt(double x);作用: sqrt() 用来求给定值的平方根
常见的使用错误 输出 36的开根号
忽略了sqrt()函数的返回值是 double型。导致出错
解决办法如下:
常见的使用sqrt()函数的规范写法 例如: 我们要判断一个数是不是质数,只需要判断 2 ~ n开根号 之间有没有可以整除的数就可以了 错误的写法:
bool find(int n)
{
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
return false;
}
return true;
}上面的写法,其实并不推荐这样写。虽然我也经常这样写。 但是在一次做题的过程中,就因为这样写程序出现了bug。 我就改用下面的这种稳妥的写法了 正确的写法:
bool find(int n)
{
int sql=(int)sqrt(1.0*n);//1.0*n的目的是 隐式转换成浮点数,开根号后再强制转换成整型
for(int i=2;i<=sql;i++)
{
if(n%i==0)
return false;
}
return true;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/132640.html原文链接:https://javaforall.cn
边栏推荐
- Halcon knowledge: matrix topic [01]
- How to use the current conversion function in SAP CDs view
- Advanced C language
- February 15, 2022 learning summary
- io模型初探
- 正版ST-link/V2 J-LINK JTAG/SWD引脚定义和注意事项
- Lumiprobe ProteOrange 蛋白质凝胶染料说明书
- curl: (56) Recv failure: Connection reset by peer
- The MySQL installed in Alibaba cloud server is version 8. Is it because the MySQL driver version of dataworks does not support it? Now mention
- kubernetes可视化界面dashboard
猜你喜欢

How to upgrade from RHEL 8 to RHEL 9

Does face recognition test involve privacy and security issues? A foreign company was urgently stopped

使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据

独立站卖家如何高效管理复杂的Facebook主页?

Small program graduation project based on wechat milk tea takeout mall small program graduation project opening report function reference

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

【软件测试】2022年普通高等学校招生全国统一考试

如何高效优雅地管理接口文档

An error is reported when ActiveMQ is started. The 1883 port occupation problem is solved

Graphic system - 1 Layout loading
随机推荐
2022年6月27日-2022年7月3日(ue4视频教程)
Does face recognition test involve privacy and security issues? A foreign company was urgently stopped
基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
How to use the current conversion function in SAP CDs view
Sword finger offer 11 Minimum number of rotation array
Introduction to apifox
杂记:数据库go,begin,end,for,after,instead of
如何从RHEL 8升级到RHEL 9
启牛学堂的vip证券账户是真的安全正规吗?怎么说
Is it reliable to open an account for the shares of Oriental Wealth software? Where is safe to open an account
架构自治服务:构建数据驱动的架构洞察
HTNL introduction
Kubernetes visual interface dashboard
PCB线路板布局和布线都有哪些设计要求?
Graphic system - 1 Layout loading
Applet graduation design based on wechat beauty salon technician appointment applet graduation design opening report function reference
Industrial digitalization and new generation digitalization system design platform -- Lecture
Small program graduation project based on wechat campus lost and found graduation project opening report function reference
图形系统——2.view绘制
【译】clickhouse 22.4和22.5核心特性一览