Skip to content

Instantly share code, notes, and snippets.

@metowolf
Last active December 20, 2016 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metowolf/859e9d1b116b55c553c828ff1b99bd3e to your computer and use it in GitHub Desktop.
Save metowolf/859e9d1b116b55c553c828ff1b99bd3e to your computer and use it in GitHub Desktop.
牛顿迭代开平方
#include<cstdio>
double sqr(double x){
return x*x;
}
double sqrt(int v,double t){
double ans=v;
while(1){
if(sqr(ans-t)<=v&&sqr(ans+t)>=v)break;
ans=(ans+v/ans)/2;
}
return ans;
}
int main(){
printf("%.16f",sqrt(123456789,0.000000001));
return 0;
}
/*
output:
11111.1110605555550137
python:
>>> 123456789**0.5
11111.111060555555
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment