I have been set a challenge to calculate a number to 100 decimal points in python without using any imported modules e.g. to calculate square root of 2
As it is an academic challenge it means that I cannot import decimal, sympy, evalf, gmpy2 and other suggestions that would work from responses here how can i show an irrational number to 100 decimal places in python?
I have also tried to do calculations using range in steps of 0.0000000000001 etc., but that would require use of Numpy How to use a decimal range() step value?
When I do calculate square root of 2, Python only shows me 16 decimal places. 1.4142135623746899
I understand that there are reasons behind why Python won't do more decimal places https://docs.python.org/3.4/tutorial/floatingpoint.html
I have tried to think creatively and calculate the square root of 20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
but again Python does not return a complete number, I get an answer like this
1.414213562373095e+50
A solution that shows potential promise is using format
def mySqrt(x):
r = x
precision = 10 ** (-10)
while abs(x - r * r) > precision:
r = (r + x / r) / 2
return r
find_square_root_of = 2
answer = format(mySqrt(find_square_root_of), ',.100f')
print(answer)
This gives me the answer of
1.4142135623746898698271934335934929549694061279296875000000000000000000000000000000000000000000000000
But I need the rest of the zeros calculated. Any suggestions on what needs fixing in the code?