-->
当前位置:首页 > 题库

函数题:Newton's Square Root (Py)

Luz4年前 (2022-05-10)题库543
How does one compute square roots? The most common way is to use Newton's method of successive approximations, which says that whenever we have a guess $y$ for the value of the square root of a number $x$, we can perform a simple manipulation to get a better guess (one closer to the actual square root) by averaging $y$ with $\frac{x}{y}$. To make the process simpler, we can always begin to guess $\frac{x}{2}$ as square root of any $x$.

You are going to write a function to calculate the square root。

### Function Declaration
Python
def f(x, guess, eps):


Where x is the value to get square root from; guess is $\frac{x}{2}$ always; eps is a very small number that when the distance between guess's square and x is less than eps, we say guess is the square root of x and return that guess.

Beaware, submit the whole function, including the head.

### Example of Judge Program

Python
#def f(x, guess, eps):
# Your code goes here

x, eps = map(float, input().split())
print(f'{f(x,x/2,eps):.3f}')


### Sample Input

in
2 0.0001



### Sample Output

out
1.414







答案:若无答案欢迎评论