<div dir="ltr">Maybe we could make a C implementation of the Fraction module? That would be nice.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 31, 2015 at 8:28 PM,  <span dir="ltr"><<a href="mailto:python-ideas-request@python.org" target="_blank">python-ideas-request@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Send Python-ideas mailing list submissions to<br>
        <a href="mailto:python-ideas@python.org">python-ideas@python.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
        <a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
or, via email, send a message with subject or body 'help' to<br>
        <a href="mailto:python-ideas-request@python.org">python-ideas-request@python.org</a><br>
<br>
You can reach the person managing the list at<br>
        <a href="mailto:python-ideas-owner@python.org">python-ideas-owner@python.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of Python-ideas digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
   1. Re: Python Float Update (Chris Angelico)<br>
   2. Re: Python Float Update (<a href="mailto:random832@fastmail.us">random832@fastmail.us</a>)<br>
   3. Re: Python Float Update (Jim Witschey)<br>
   4. Re: Python Float Update (David Mertz)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Mon, 1 Jun 2015 12:48:12 +1000<br>
From: Chris Angelico <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>><br>
Cc: python-ideas <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>><br>
Subject: Re: [Python-ideas] Python Float Update<br>
Message-ID:<br>
        <CAPTjJmr=LurRUoKH3KVVYpMFc=<a href="mailto:W5h6etG5TscV5uU6zWhxVbgQ@mail.gmail.com">W5h6etG5TscV5uU6zWhxVbgQ@mail.gmail.com</a>><br>
Content-Type: text/plain; charset=UTF-8<br>
<br>
On Mon, Jun 1, 2015 at 12:25 PM, u8y7541 The Awesome Person<br>
<<a href="mailto:surya.subbarao1@gmail.com">surya.subbarao1@gmail.com</a>> wrote:<br>
><br>
> I will be presenting a modification to the float class, which will improve its speed and accuracy (reduce floating point errors). This is applicable because Python uses a numerator and denominator rather than a sign and mantissa to represent floats.<br>
><br>
> First, I propose that a float's integer ratio should be accurate. For example, (1 / 3).as_integer_ratio() should return (1, 3). Instead, it returns(6004799503160661, 18014398509481984).<br>
><br>
<br>
I think you're misunderstanding the as_integer_ratio method. That<br>
isn't how Python works internally; that's a service provided for<br>
parsing out float internals into something more readable. What you<br>
_actually_ are working with is IEEE 754 binary64. (Caveat: I have no<br>
idea what Python-the-language stipulates, nor what other Python<br>
implementations use, but that's what CPython uses, and you did your<br>
initial experiments with CPython. None of this discussion applies *at<br>
all* if a Python implementation doesn't use IEEE 754.) So internally,<br>
1/3 is stored as:<br>
<br>
0 <-- sign bit (positive)<br>
01111111101 <-- exponent (1021)<br>
0101010101010101010101010101010101010101010101010101 <-- mantissa (52<br>
bits, repeating)<br>
<br>
The exponent is offset by 1023, so this means 1.010101.... divided by<br>
2?; the original repeating value is exactly equal to 4/3, so this is<br>
correct, but as soon as it's squeezed into a finite-sized mantissa, it<br>
gets rounded - in this case, rounded down.<br>
<br>
That's where your result comes from. It's been rounded such that it<br>
fits inside IEEE 754, and then converted back to a fraction<br>
afterwards. You're never going to get an exact result for anything<br>
with a denominator that isn't a power of two. Fortunately, Python does<br>
offer a solution: store your number as a pair of integers, rather than<br>
as a packed floating point value, and all calculations truly will be<br>
exact (at the cost of performance):<br>
<br>
>>> one_third = fractions.Fraction(1, 3)<br>
>>> one_eighth = fractions.Fraction(1, 8)<br>
>>> one_third + one_eighth<br>
Fraction(11, 24)<br>
<br>
This is possibly more what you want to work with.<br>
<br>
ChrisA<br>
<br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Sun, 31 May 2015 23:14:06 -0400<br>
From: <a href="mailto:random832@fastmail.us">random832@fastmail.us</a><br>
To: <a href="mailto:python-ideas@python.org">python-ideas@python.org</a><br>
Subject: Re: [Python-ideas] Python Float Update<br>
Message-ID:<br>
        <<a href="mailto:1433128446.31560.283106753.6D60F98F@webmail.messagingengine.com">1433128446.31560.283106753.6D60F98F@webmail.messagingengine.com</a>><br>
Content-Type: text/plain<br>
<br>
On Sun, May 31, 2015, at 22:25, u8y7541 The Awesome Person wrote:<br>
> First, I propose that a float's integer ratio should be accurate. For<br>
> example, (1 / 3).as_integer_ratio() should return (1, 3). Instead, it<br>
> returns(6004799503160661, 18014398509481984).<br>
<br>
Even though he's mistaken about the core premise, I do think there's a<br>
kernel of a good idea here - it would be nice to have a method (maybe<br>
as_integer_ratio, maybe with some parameter added, maybe a different<br>
method) to return with the smallest denominator that would result in<br>
exactly the original float if divided out, rather than merely the<br>
smallest power of two.<br>
<br>
<br>
------------------------------<br>
<br>
Message: 3<br>
Date: Mon, 01 Jun 2015 03:21:36 +0000<br>
From: Jim Witschey <<a href="mailto:jim.witschey@gmail.com">jim.witschey@gmail.com</a>><br>
To: Chris Angelico <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>><br>
Cc: python-ideas <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>><br>
Subject: Re: [Python-ideas] Python Float Update<br>
Message-ID:<br>
        <CAF+a8-q6kbOwcWk3F47+9PXf2vKgM9ao1uh5=qBw10jqzTC=<a href="mailto:kg@mail.gmail.com">kg@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Teachable moments about the implementation of floating-point aside,<br>
something in this neighborhood has been considered and rejected before, in<br>
PEP 240. However, that was in 2001 - it was apparently created the same day<br>
as PEP 237, which introduced transparent conversion of machine ints to<br>
bignums in the int type.<br>
<br>
I think hiding hardware number implementations has been a success for<br>
integers - it's a far superior API. It could be for rationals as well.<br>
<br>
Has something like this thread's original proposal - interpeting<br>
decimal-number literals as fractional values and using fractions as the<br>
result of integer arithmetic - been seriously discussed more recently than<br>
PEP 240? If so, why haven't they been implemented? Perhaps enough has<br>
changed that it's worth reconsidering.<br>
<br>
<br>
On Sun, May 31, 2015 at 22:49 Chris Angelico <<a href="mailto:rosuav@gmail.com">rosuav@gmail.com</a>> wrote:<br>
<br>
> On Mon, Jun 1, 2015 at 12:25 PM, u8y7541 The Awesome Person<br>
> <<a href="mailto:surya.subbarao1@gmail.com">surya.subbarao1@gmail.com</a>> wrote:<br>
> ><br>
> > I will be presenting a modification to the float class, which will<br>
> improve its speed and accuracy (reduce floating point errors). This is<br>
> applicable because Python uses a numerator and denominator rather than a<br>
> sign and mantissa to represent floats.<br>
> ><br>
> > First, I propose that a float's integer ratio should be accurate. For<br>
> example, (1 / 3).as_integer_ratio() should return (1, 3). Instead, it<br>
> returns(6004799503160661, 18014398509481984).<br>
> ><br>
><br>
> I think you're misunderstanding the as_integer_ratio method. That<br>
> isn't how Python works internally; that's a service provided for<br>
> parsing out float internals into something more readable. What you<br>
> _actually_ are working with is IEEE 754 binary64. (Caveat: I have no<br>
> idea what Python-the-language stipulates, nor what other Python<br>
> implementations use, but that's what CPython uses, and you did your<br>
> initial experiments with CPython. None of this discussion applies *at<br>
> all* if a Python implementation doesn't use IEEE 754.) So internally,<br>
> 1/3 is stored as:<br>
><br>
> 0 <-- sign bit (positive)<br>
> 01111111101 <-- exponent (1021)<br>
> 0101010101010101010101010101010101010101010101010101 <-- mantissa (52<br>
> bits, repeating)<br>
><br>
> The exponent is offset by 1023, so this means 1.010101.... divided by<br>
> 2?; the original repeating value is exactly equal to 4/3, so this is<br>
> correct, but as soon as it's squeezed into a finite-sized mantissa, it<br>
> gets rounded - in this case, rounded down.<br>
><br>
> That's where your result comes from. It's been rounded such that it<br>
> fits inside IEEE 754, and then converted back to a fraction<br>
> afterwards. You're never going to get an exact result for anything<br>
> with a denominator that isn't a power of two. Fortunately, Python does<br>
> offer a solution: store your number as a pair of integers, rather than<br>
> as a packed floating point value, and all calculations truly will be<br>
> exact (at the cost of performance):<br>
><br>
> >>> one_third = fractions.Fraction(1, 3)<br>
> >>> one_eighth = fractions.Fraction(1, 8)<br>
> >>> one_third + one_eighth<br>
> Fraction(11, 24)<br>
><br>
> This is possibly more what you want to work with.<br>
><br>
> ChrisA<br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.python.org/pipermail/python-ideas/attachments/20150601/5e792f59/attachment-0001.html" target="_blank">http://mail.python.org/pipermail/python-ideas/attachments/20150601/5e792f59/attachment-0001.html</a>><br>
<br>
------------------------------<br>
<br>
Message: 4<br>
Date: Sun, 31 May 2015 20:27:47 -0700<br>
From: David Mertz <<a href="mailto:mertz@gnosis.cx">mertz@gnosis.cx</a>><br>
To: <a href="mailto:random832@fastmail.us">random832@fastmail.us</a><br>
Cc: python-ideas <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>><br>
Subject: Re: [Python-ideas] Python Float Update<br>
Message-ID:<br>
        <<a href="mailto:CAEbHw4biWOxjYR0vtu8ykwSt63y9dcsR2-FtLPGfyUvqx0GgsQ@mail.gmail.com">CAEbHw4biWOxjYR0vtu8ykwSt63y9dcsR2-FtLPGfyUvqx0GgsQ@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
On Sun, May 31, 2015 at 8:14 PM, <<a href="mailto:random832@fastmail.us">random832@fastmail.us</a>> wrote:<br>
<br>
> Even though he's mistaken about the core premise, I do think there's a<br>
> kernel of a good idea here - it would be nice to have a method (maybe<br>
> as_integer_ratio, maybe with some parameter added, maybe a different<br>
> method) to return with the smallest denominator that would result in<br>
> exactly the original float if divided out, rather than merely the<br>
> smallest power of two.<br>
><br>
<br>
What is the computational complexity of a hypothetical<br>
float.as_simplest_integer_ratio() method?  How hard that is to find is not<br>
obvious to me (probably it should be, but I'm not sure).<br>
<br>
--<br>
Keeping medicines from the bloodstreams of the sick; food<br>
from the bellies of the hungry; books from the hands of the<br>
uneducated; technology from the underdeveloped; and putting<br>
advocates of freedom in prisons.  Intellectual property is<br>
to the 21st century what the slave trade was to the 16th.<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://mail.python.org/pipermail/python-ideas/attachments/20150531/49264b3d/attachment.html" target="_blank">http://mail.python.org/pipermail/python-ideas/attachments/20150531/49264b3d/attachment.html</a>><br>
<br>
------------------------------<br>
<br>
Subject: Digest Footer<br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
<br>
<br>
------------------------------<br>
<br>
End of Python-ideas Digest, Vol 103, Issue 3<br>
********************************************<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><font face="garamond, serif" size="6">-Surya Subbarao</font></div></div>
</div>