求平方根,正根.曾经都不会.昨天看数学,看到了,写了出来.自己又小优化了一下,非常不错.

// squareRoot.cpp -- 2011-08-29-01.04
#include "stdafx.h"
#include <iostream> double squareRoot (double radicand, double precision) ; int _tmain(int argc, _TCHAR* argv[])
{
std ::cout << squareRoot(9, 0.000001) << std ::endl ; return 0;
} double squareRoot (double radicand, double precision)
{
if (radicand > 0)
{
double squareRoot = radicand / 10 ;
while (squareRoot * squareRoot > radicand)
squareRoot /= 2 ;
double fakePrecision = 0.1;
while (1)
{
while ((squareRoot + fakePrecision) * (squareRoot + fakePrecision) <= radicand)
{
squareRoot += fakePrecision ;
}
if (fakePrecision > precision)
{
fakePrecision /= 10 ;
}
else
{
return squareRoot ;
}
}
}
else
{
std ::cerr << "Radicand must > 0" << std ::endl ;
return 0 ;
}
}

求平方根C++的更多相关文章

  1. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  2. ytu 1041: 迭代法求平方根(水题)

    1041: 迭代法求平方根 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 227  Solved: 146[Submit][Status][Web Bo ...

  3. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...

  4. [转载]求平方根sqrt()函数的底层算法效率问题

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

  5. note 5 二分法求平方根,素数,回文数

    +二分法求平方根 x = float(raw_input('Enter the number')) low = 0 high = x guess = (low + high ) / 2 if x &l ...

  6. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  7. JustOj 1036: 习题6.11 迭代法求平方根

    题目描述 用迭代法求 .求平方根的迭代公式为: X[n+1]=1/2(X[n]+a/X[n]) 要求前后两次求出的得差的绝对值少于0.00001. 输出保留3位小数 输入 X 输出 X的平方根 样例输 ...

  8. 二分法求平方根(Python实现)

    使用二分法(Bisection Method)求平方根. def sqrtBI(x, epsilon): assert x>0, 'X must be non-nagtive, not ' + ...

  9. 141. Sqrt(x)【牛顿迭代法求平方根 by java】

    Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...

随机推荐

  1. maven学习--生命周期

    clean --清理项目 default --构建项目(最核心)  ===========compile , test , package , install site --生成项目站点

  2. git —— 远程仓库(操作)

    运行目录:本地仓库目录 1.本地关联远程仓库 $ git remote add origin 你的远程库地址(SSH和HTTP都可以) 2.远程仓库为空,可选择合并远程仓库和本地仓库,远程库不为空时, ...

  3. Java 中判断字符串是否为空

    public class TestString { public static void main(String[] args) { String abc = null; //先判断是否为null再判 ...

  4. c语言快速排序算法(转)

    原文链接http://blog.csdn.net/morewindows/article/details/6684558 快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常 ...

  5. 一键复制功能 - Vue

    经常遇到一键复制功能,简单记录一下.这里使用的是clipboard插件:https://clipboardjs.com/ 第一步 安装:npm install clipboard --save 第二步 ...

  6. 安装VM虚拟机提示 尝试创建目录 C:\Public\documents\SharedVirtual Machines 时发生错误解决方法

    把Windows Defender安全中心的“受控制文件夹的访问”给关闭了,然后就可以顺利安装上了. 作者:耑新新,发布于  博客园 转载请注明出处,欢迎邮件交流:zhuanxinxin@foxmai ...

  7. windows svn post-commit 报错解决 error resolving case

    在svn仓库目录下有个hooks目录,下面建一个 post-commit.cmd 文件,有代码提交到仓库,自动checkout到指定目录.   @echo onSET REPOS=%1SET USER ...

  8. Thymeleaf(Java模板引擎)

    一.概念 1.Thymeleaf是Web和独立环境的开源的Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本:2.Thymeleaf可以在Web(基于Servlet)和 ...

  9. curl之采集QQ空间留言

    目录 主要流程解析 注意事项 扩展 完整代码示例 采集效果一览 主要流程解析 首先,打开浏览器登录QQ空间并访问留言列表 由于QQ空间的链接是https,curl方式请求https链接需要突破http ...

  10. lnmp 一键安装包

    系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian Linux系统 需要5GB以上硬盘剩余空间 需要128MB以上内存(如果为128MB的小内存VPS,Xe ...