-
-
Save metowolf/859e9d1b116b55c553c828ff1b99bd3e to your computer and use it in GitHub Desktop.
牛顿迭代开平方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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