andyvand / gmpy

Automatically exported from code.google.com/p/gmpy
GNU Lesser General Public License v3.0
0 stars 0 forks source link

Problem with ceiling function? #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

ctx=gmpy2.get_context()
ctx.precision=8192
# This also fails if A is declared as an mpz
A=gmpy2.mpfr('432037358248410255167738661315350243002850670884399905095991834789
14981341411300267425404519851259836309110515899641073825011444748141212646469307
92091129675990873854551977478171381019441858715044569768091147838247311432796381
03538218806002132976211011979665944715304287637565688518449158381315650825443632
7574.0',8192)
B=gmpy2.sqrt(A)
C=gmpy2.ceil(B)
D=C**2 # Also fails if using D=gmpy2.mul(C,C) or D=C*C
E=gmpy2.sub(D,A)

What is the expected output? What do you see instead?
E should be small, but isn't: it's 
mpfr('65953612079233123984021203765746782057212592806846318214461803258500179256
29186499584086820215927438018452012289613644940916529517387394341972720724026345
1.0',8192)
Given that the difference between B and C is approx 0.5 this is incorrect.

What version of the product are you using? On what operating system?
gmpy2-2.0.0b1 and Python 2.7.3 on Ubuntu Linux 12.04 (3.2.0-31)

Original issue reported on code.google.com by fur...@gmail.com on 11 Oct 2012 at 8:57

GoogleCodeExporter commented 9 years ago
I stand corrected, this is expected behaviour. Sorry!

Original comment by fur...@gmail.com on 11 Oct 2012 at 9:02

GoogleCodeExporter commented 9 years ago
Thanks for letting me know that it is working as expected.

BTW, if your actual goal is to calculate ceil(sqrt(integer)), the following 
sequence will be faster:

x,y = gmpy2.isqrt("432....")
x += bool(y)

If that was just an example, you can ignore my comment. :)

Original comment by casevh on 12 Oct 2012 at 4:55