andyvand / gmpy

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

Change in behavior of mpq + float? #44

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I've been using gmpy 1.01 for quite a long time (thanks!). In that version, 
mpq(0.999)+0.1 yields mpq(1099,1000). In gmpy 1.14 (which I just tried out 
now), mpq(0.999)+0.1 yields mpf('1.09900000000000000553e0'), which was 
surprising to me. mpq(0.999)+mpq(0.1) still yields mpq(1099,1000). I tried to 
find the change responsible for this; I can see in changes.txt that there have 
been some alterations to number conversion rules, but I don't see specific 
mention of this one. Did I miss it somewhere?

Thanks!
Chris

Original issue reported on code.google.com by ceb...@gmail.com on 24 Feb 2011 at 3:14

GoogleCodeExporter commented 9 years ago
Hi Chris,

Your particular use case didn't make into changes.txt. Sorry about that!

I tried to mimic the behavior of the Fraction type introduced in Python 2.6. I 
use  the latest version of Python as a reference so the following examples use 
3.2. (Fraction in Python 2.6 won't directly convert a float to a Fraction; you 
need to use the from_float() method.)

>>> fractions.Fraction(0.999)
Fraction(8998192055486251, 9007199254740992)
>>> fractions.Fraction(0.999) + 0.1
1.099
>>> fractions.Fraction('0.999')
Fraction(999, 1000)
>>> fractions.Fraction('0.999') + fractions.Fraction(0.1)
Fraction(4949455990480175129, 4503599627370496000)
>>> fractions.Fraction('0.999') + fractions.Fraction('0.1')
Fraction(1099, 1000)
>>> 

Several behaviors changed. First, Fraction plus float returns a float.

Second, conversion from a float to a rational are now exact in Fraction. gmpy 
used to return an user-friendly approximation when converting a float to an 
mpq. This change occurred in gmpy2.

>>> import gmpy,gmpy2
>>> gmpy.mpq(0.999)
mpq(999,1000)
>>> gmpy2.mpq(0.999)
mpq(8998192055486251,9007199254740992)
>>> 

Conversion from a string still works.

>>> gmpy.mpq('0.999')
mpq(999,1000)
>>> gmpy2.mpq('0.999')
mpq(999,1000)
>>> 

I'm also working towards support for better floating point numbers in gmpy2. 
The MPFR library provides correctly rounded base-2 arithmetic and I wanted to 
support exact float to mpfr conversions so it made sense to do exact float to 
mpq conversions.

After gmpy 1.11, I realized the changes I wanted to do where becoming more and 
more invasive, so I renamed the development branch to gmpy2.

I think 1.04 was the last version to support the old-style conversion rules but 
it doesn't support Python 3.x.

Case

Original comment by casevh on 25 Feb 2011 at 4:28

GoogleCodeExporter commented 9 years ago
Closing.

Original comment by casevh on 9 Mar 2011 at 10:42

GoogleCodeExporter commented 9 years ago
Sorry, I forgot to comment to say thank you for the detailed reply. It's 
unfortunate from my point of view (as an author of software that depends on 
gmpy) that these behaviors changed in a minor release, although I understand 
why you made the changes.

Thanks for all the work on gmpy.

Chris 

P.S. Sorry if this re-opens the bug - that's not the point of this comment.

Original comment by ceb...@gmail.com on 10 Mar 2011 at 8:51

GoogleCodeExporter commented 9 years ago
Chris,
Is your software generally available? If so, and if it has a test suite that 
exercises a gmpy, I'd be willing to test newer releases against it. I currently 
test against mpmath and I'm looking for more real-world code to use for testing.

Case

Original comment by casevh on 11 Mar 2011 at 3:12