当前位置:网站首页>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
边栏推荐
- Redis6 notes 04 master-slave replication, cluster, application problems, new redis6 features
- Common DOS commands
- Lumiprobe丨Lumizol RNA 提取试剂解决方案
- How to manage interface documents efficiently and gracefully
- 软件测试的三个沟通技巧
- use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
- Operations research note
- PCB线路板布局和布线都有哪些设计要求?
- Dnslog injection
- Sharing-JDBC分布式事务之Seata实现
猜你喜欢

Small program graduation project based on wechat recruitment small program graduation project opening report function reference

Lumiprobe非荧光叠丨氮化物研究丨3-叠丨氮丙醇

324. 摆动排序 II

Lumiprobe丨Lumizol RNA 提取试剂解决方案

Konva series tutorial 3: Customizing drawings

电子商务盛行,怎么提高商店转换率?

Redis6 notes 04 master-slave replication, cluster, application problems, new redis6 features

Lumiprobe 蛋白质标记研究方案

Kubeadm create kubernetes cluster

双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
随机推荐
Small program graduation project based on wechat milk tea takeout mall small program graduation project opening report function reference
Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence
Can tongdaxin open an account for stock trading? Is it safe?
【译】clickhouse 22.4和22.5核心特性一览
Does face recognition test involve privacy and security issues? A foreign company was urgently stopped
SMARCA2抗体研究:Abnova SMARCA2 单克隆抗体方案
注意!PMP紧急缓考今天就截止了!
Graphic system - 1 Layout loading
SqlTransaction
How to design a business high performance and high availability computing architecture - job
Cann media data processing V2, jpegd interface introduction
EasyExcel 学习笔记
Architecture autonomy service: building data-driven architecture insight
面部识别试验涉及隐私安全问题?国外一公司被紧急叫停
How to use the current conversion function in SAP CDs view
kubeadm创建kubernetes集群
Learning notes: how to time 10ms for 51 single chip microcomputer (STC89C52)
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
Small program graduation project based on wechat agricultural and sideline products agricultural products mall small program graduation project opening report function reference
How to upgrade from RHEL 8 to RHEL 9