求平方根C++
求平方根,正根.曾经都不会.昨天看数学,看到了,写了出来.自己又小优化了一下,非常不错.
// 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++的更多相关文章
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- ytu 1041: 迭代法求平方根(水题)
1041: 迭代法求平方根 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 227 Solved: 146[Submit][Status][Web Bo ...
- 19. 求平方根序列前N项和
求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...
- [转载]求平方根sqrt()函数的底层算法效率问题
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- note 5 二分法求平方根,素数,回文数
+二分法求平方根 x = float(raw_input('Enter the number')) low = 0 high = x guess = (low + high ) / 2 if x &l ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- JustOj 1036: 习题6.11 迭代法求平方根
题目描述 用迭代法求 .求平方根的迭代公式为: X[n+1]=1/2(X[n]+a/X[n]) 要求前后两次求出的得差的绝对值少于0.00001. 输出保留3位小数 输入 X 输出 X的平方根 样例输 ...
- 二分法求平方根(Python实现)
使用二分法(Bisection Method)求平方根. def sqrtBI(x, epsilon): assert x>0, 'X must be non-nagtive, not ' + ...
- 141. Sqrt(x)【牛顿迭代法求平方根 by java】
Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...
随机推荐
- ioctl()函数获取本机IP、MAC
#include <sys/ioctl.h> int ioctl(int d, int request, ...); /* Socket configuration controls. * ...
- 使用dos命令创建多模块Maven项目
好吧,咱们接着上一篇博客继续用另一种方式来创建Maven项目.不过在创建之前我们应该先熟悉一些相关dos命令. 创建web项目命令: mvn archetype:generate -DgroupId= ...
- AdvStringGrid 复选框、goRowSelect
var I: Integer; begin do begin AdvStringGrid1.AddCheckBox(, I, True, True); AdvStringGrid1.Cells[,I] ...
- 开启nginx目录文件列表功能
ngx_http_autoindex_module 此模块用于自动生成目录列表,ngx_http_autoindex_module只在 ngx_http_index_module模块未找到索引文件时 ...
- MEF实现设计上的“松耦合”(三)
1.面向接口编程:有一定编程经验的博友应该都熟悉或者了解这种编程思想,层和层之间通过接口依赖,下层不是直接给上层提供服务,而是定义一组接口供上层调用.至于具体的业务实现,那是开发中需要做的事情,在项目 ...
- 《HBase实战》学习笔记
第二章 入门 HBase写路径: 增加新行和修改已有的行,内部机制是一样的. 写入的时候,会写到预写日志(WAL)和MemStore中. MenmStore是内存里的写入缓冲区.填满后,会将数据刷写 ...
- java 基础类库之 SQLFun
package com.exjor.webdemo; import java.sql.Timestamp; import java.util.Date; public class SQLFun { / ...
- poj2492 A Bug's Life(带权并查集)
题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...
- mysql 解除正在死锁的状态
转自:http://blog.csdn.net/hotdust/article/details/51524469 from: http://www.2cto.com/database/201303/1 ...
- html5+css3 手机屏幕的适配css
*{ margin:0;padding:0;outline:0}a{ text-decoration:none}body,html{ font-size:20px;font-family:'Micro ...