Ask Your Question
0

please explain math.sqrt(n)

asked 2021-07-12 12:36:41 +0100

Miroslaw gravatar image

Hello Dear all

I tried understand :

import math
n=115792089237316195423570985008687907852837564279074904382605163141518161494337
d= math.sqrt(n)
print(d)
print(d==2**128)
print(d*d==n)
u=d*d
print(u)
print(n-u)

result: how it is possibility that sqrt(n) *sqrt(n) is equal n if it is not?:)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2021-07-12 13:42:49 +0100

Sébastien gravatar image

Your n is not the square of 2^128:

sage: n=115792089237316195423570985008687907852837564279074904382605163141518161494337                                                                    
sage: (2^128)^2 - n                                                             
432420386565659656852420866394968145599

But, if you use float, then the precision of floats (53 bits of precision) is not enough to see a difference:

sage: float((2^128)^2) - float(n)                                               
0.0

So, don't use float and don't use math.sqrt which returns float to compute such equalities or differences:

sage: type(math.sqrt(n))                                                        
<class 'float'>
edit flag offensive delete link more

Comments

so how to take sqrt from n?

Miroslaw gravatar imageMiroslaw ( 2021-07-12 14:11:23 +0100 )edit

sqrt(n) or sqrt(n).numerical_approx() or its shorcut sqrt(n).n() or sqrt(n).n(digits=10000) for more precision

Sébastien gravatar imageSébastien ( 2021-07-12 15:25:33 +0100 )edit

o thank you

Miroslaw gravatar imageMiroslaw ( 2021-07-12 16:12:14 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-07-12 12:36:41 +0100

Seen: 224 times

Last updated: Jul 12 '21