implement of sqrt without using stdlib
来源:互联网 发布:go语言http json数据 编辑:程序博客网 时间:2024/06/11 13:42
/** binary search using */// sqrt.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <cstdlib>#include <cassert>using namespace std;double mySqrt(double num, double delta){assert(num >= 1.0);double start = 1.0;double end = num;while(1){double mid = (start + end) / 2;double square = mid * mid;double tmp = (square>num) ? (square-num) : (num-square);if(tmp < delta) return mid;else if(square > num)end = mid;elsestart = mid;}}int main(int argc, char* argv[]){double num, delta = 0.001;while(true){cin>>num;if(num == 0)break;cout<<mySqrt(num, delta)<<endl;}//printf("Hello World!\n");return 0;}
/** Using Newton's method to implement sqrt() method.** Only process the case: input num > 0,* and resturn the positive square root.*/#include <iostream>#include <cmath>using namespace std;int main(){ double num; cin >> num; double res = 1.0; while(abs(res*res - num) > 1e-5) { res = (res + num/res) / 2; } cout << fabs(res) << endl; //cout << "Hello world!" << endl; return 0;}
- implement of sqrt without using stdlib
- implement of power(double base, int expo), without using math library.
- LeeCode-Sqrt(x)Implement int sqrt(int x). Compute and return the square root of x.
- How to implement Quality Of Service using Floodlight
- Implement int sqrt(int x).
- Implement Stack Using Queue
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Stack Using LinkedList
- Implement Stack using Queues
- Implement Queue using Stacks
- Implement Stack using Queues
- Implement Stack using Queues
- Implement Queue using Stacks
- Implement Stack using Queues
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- MSP430F2618写TLV5638
- Adobe Edge Animate –弹性的方块
- I2C总线被占用
- 程序设计竞赛网站集锦
- BUAA-SCSE Training day3 5 6 7
- implement of sqrt without using stdlib
- 23个漂亮的jQuery文字动画效果教程示例
- JSTL 表达式与 EL 语言
- MingQQ v1.0高仿版开源了,使用WebQQ协议实现了QQ客户端基本的聊天功能...
- [C][socket]:setsockopt + raw_socket
- Standford机器学习 聚类算法(clustering)和非监督学习(unsupervised Learning)
- OpenGL屏幕二维坐标转化成三维模型坐标
- C++ 顺序栈的实现
- 汇编语言学习-基础篇2