当前位置:网站首页>Application of binary search -- finding the square root sqrt of a number
Application of binary search -- finding the square root sqrt of a number
2022-06-12 13:24:00 【shlyyy】
Find the square root of a number sqrt
One 、 Two points search
Two 、 Code
#include <stdio.h>
#include <math.h>
#define PRECISION 0.0000001
double Sqrt(double n)
{
double dblRet = 0.0;
double dblLow = 0.0;
double dblHigh = n;
double dblMiddle = 0.0;
// Determine the minimum integer range of the current number
/*double i = 0; while (i <= n) { if (i * i == n) { return i; } if (i * i > n) { break; } i = i + 1.0; } dblHigh = i;*/
while (dblLow <= dblHigh)
{
dblMiddle = dblLow + (dblHigh - dblLow) / 2;
// The judgment that two floating-point numbers are equal , Here is judgment (dblMiddle^2 == n)
if ((dblMiddle * dblMiddle - n < (PRECISION))
&& (dblMiddle * dblMiddle - n > -(PRECISION)))
{
dblRet = dblMiddle;
break;
}
else if (dblMiddle * dblMiddle > n)
{
dblHigh = dblMiddle - PRECISION / 1000; // The smaller the value added or decreased here, the more accurate
}
else if (dblMiddle * dblMiddle < n)
{
dblLow = dblMiddle + PRECISION / 1000;
}
}
return dblRet;
}
int main()
{
double n = 0.0;
int i = 0;
for (i = 0; i < 100; i++)
{
printf("Sqrt(%-02d)=%0.6lf \t math:sqrt(%-02d)=%0.6lf\r\n", i, Sqrt(i), i, sqrt(i));
}
getchar();
return 0;
}
3、 ... and 、 Running results

边栏推荐
- Implementing tensorflow deep learning framework similarflow with numpy
- Deploy opengauss database based on Huawei cloud Kunpeng elastic ECS [Gauss is not a mathematician this time]
- list和dict的应用
- Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
- IC chip scheme fs4062b for lithium battery charging with 5V boost to 12.6V
- Tensorrt, onnx to tensorrt in mmclas
- 高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法
- Pytoch official fast r-cnn source code analysis (I) -- feature extraction
- Informatics Olympiad all in one 1000: introductory test questions
- Embedded system hardware composition - embedded system hardware architecture
猜你喜欢

Octopus network progress monthly report | may 1-May 31, 2022

leetcode 47. Permutations II full permutations II (medium)

深度学习的多个 loss 是如何平衡的?

【刷题篇】超级洗衣机

创新实训(十一)开发过程中的一些bug汇总

unittest框架

Overview of embedded system 2- composition and application of embedded system
![[wechat applet development] Part 1: development tool installation and program configuration](/img/a8/f4dcbde295ba7cf738d878464b3af0.png)
[wechat applet development] Part 1: development tool installation and program configuration

Further understanding of the network

创新实训(十)高级界面美化
随机推荐
达梦数据库DM8 windows环境安装
微信web开发者工具使用教程,web开发问题
智能垃圾桶语音芯片应用设计方案介绍,WT588F02B-8S
Embedded driver design
嵌入式系统概述2-嵌入式系统组成和应用
leetcode 47. Permutations II 全排列 II(中等)
hudi 键的生成(Key Generation)
[Title brushing] Super washing machine
Successfully rated Tencent t3-2, 10000 word parsing
Wechat web developer tools tutorial, web development issues
关于#SQLite写注册功能时,数据表查询出错#的问题,如何解决?
镜像扫描工具预研
Implementing tensorflow deep learning framework similarflow with numpy
嵌入式系统概述1-嵌入式系统定义、特点和发展历程
成功定级腾讯T3-2,万字解析
import torch_geometric 加载一些常见数据集
Improve pipeline efficiency: you need to know how to identify the main obstacles in ci/cd pipeline
How to solve the problem of data table query error when SQLite writes the registration function?
import torch_ Data view of geometric
It is enough to read this article. Web Chinese development