Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using fractions instead of floats

Reply
Thread Tools

Using fractions instead of floats

 
 
andresj
Guest
Posts: n/a
 
      10-01-2007
I was doing some programming in Python, and the idea came to my mind:
using fractions instead of floats when doing 2/5.

The problem arises when you try to represent some number, like 0.4 in
a float. It will tell you that it's equal to 0.40000000000000002.
"This is easy to fix", you may say. "You just use the decimal.Decimal
class!". Well, firsly, there would be an excess of typing I would need
to do to calculate 0.4+0.6:

from decimal import Decimal
print Decimal("0.4")+Decimal("0.6")

Secondly, what happens if I need to sum 1/3 and 0.4? I could use
Decimal to represent 0.4 precisely, but what about 1/3? Sure, I could
use _another_ class which works in a base (binary, decimal, octal,
hexadecimal) in which 1/3 can be represented exactly... Not to mention
the problem of operating with those two different classes...

So the solution I think is using a fraction type/class, similar to the
one found in Common Lisp. If you have used CLisp before, you only need
to type:
(+ 1/3 6/10)
to get the exact result. (Yes, I also hate the (operator arg1 arg2)
syntax, but it's just an example). I would like to have something
similar in Python, in which dividing two numbers gives you a fraction,
instead of an integer (python 2.x) or a float (decided for python
3.x).

an implementation could be like this:
class frac(object): # PS: This (object) thing will be removed in
python 3.0, right?
def __init__(self, numerator, denominator):
pass
def __add__(self, other):
pass
#...

(I have an implementation of the frac class done (this meaning, it
works for me), and although it's pretty dirty, I'd be happy to post it
here if you want it.)

My idea, in summary would be that this python shell session is true:
>>> 5/2

2 1/2
>>> f = 5/2
>>> f.__class__

<type 'frac'>
>>> # or <class 'frac'>
>>> (0.6).__class__ # Treat decimal literals as fractions, so that...

<type 'frac'>
>>> 1/3+0.6

14/15
>>> # That was easy.


I would like to get some feedback on this idea. Has this been posted
before? If so, was it rejected? and for what?
Also, I would like to know if you have improvements on the initial
design, and if it would be appropiate to send it as a PEP.

 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      10-01-2007
andresj <(E-Mail Removed)> writes:

> The problem arises when you try to represent some number, like 0.4 in
> a float.


Which is really a specific case of the general problem that, for any
given number base, some non-integer numbers cannot be exactly
represented as fractions.

> Secondly, what happens if I need to sum 1/3 and 0.4? I could use
> Decimal to represent 0.4 precisely, but what about 1/3?


What about the sum of π (pi) and √2 (sqrt(2))?

> So the solution I think is using a fraction type/class


As explained above, a fractional-number class only shifts the "exact
representation" problem, it doesn't solve it.

--
\ "Facts do not cease to exist because they are ignored." -- |
`\ Aldous Huxley |
_o__) |
Ben Finney
 
Reply With Quote
 
 
 
 
andresj
Guest
Posts: n/a
 
      10-01-2007
On Sep 30, 6:48 pm, Ben Finney <(E-Mail Removed)>
wrote:
> andresj <(E-Mail Removed)> writes:
> > The problem arises when you try to represent some number, like 0.4 in
> > a float.

>
> Which is really a specific case of the general problem that, for any
> given number base, some non-integer numbers cannot be exactly
> represented as fractions.


Yes. That's what I meant to say, by giving an example.

> > Secondly, what happens if I need to sum 1/3 and 0.4? I could use
> > Decimal to represent 0.4 precisely, but what about 1/3?

>
> What about the sum of (pi) and 2 (sqrt(2))?


Mm... To be honest I hadn't thought of that in the moment I wrote the
last post...
But I think then, that I'll be more specific, so that my proposal only
deals with *rational numbers*, not irrationals.

>
> > So the solution I think is using a fraction type/class

>
> As explained above, a fractional-number class only shifts the "exact
> representation" problem, it doesn't solve it.


I don't understand completely what you said, but I think you are
saying that it doesn't solve the problem with irrational numbers.
Which is exactly what this proposal doesn't solve. (Mainly because I
do not know of any way of doing it in "normal" math).

(While I write this, my mind reminds me of operating with roots " 2
(sqrt(2))", which I think could be solved in another similar way,
although it has some more specific use cases... But in any case,
that's not in the scope of this proposal, as they are irrational
numbers.)



 
Reply With Quote
 
mensanator@aol.com
Guest
Posts: n/a
 
      10-01-2007
On Sep 30, 8:35?pm, andresj <(E-Mail Removed)> wrote:
> I was doing some programming in Python, and the idea came to my mind:
> using fractions instead of floats when doing 2/5.
>
> The problem arises when you try to represent some number, like 0.4 in
> a float. It will tell you that it's equal to 0.40000000000000002.
> "This is easy to fix", you may say. "You just use the decimal.Decimal
> class!". Well, firsly, there would be an excess of typing I would need
> to do to calculate 0.4+0.6:
>
> from decimal import Decimal
> print Decimal("0.4")+Decimal("0.6")
>
> Secondly, what happens if I need to sum 1/3 and 0.4? I could use
> Decimal to represent 0.4 precisely, but what about 1/3? Sure, I could
> use _another_ class which works in a base (binary, decimal, octal,
> hexadecimal) in which 1/3 can be represented exactly... Not to mention
> the problem of operating with those two different classes...
>
> So the solution I think is using a fraction type/class, similar to the
> one found in Common Lisp. If you have used CLisp before, you only need
> to type:
> (+ 1/3 6/10)
> to get the exact result. (Yes, I also hate the (operator arg1 arg2)
> syntax, but it's just an example). I would like to have something
> similar in Python, in which dividing two numbers gives you a fraction,
> instead of an integer (python 2.x) or a float (decided for python
> 3.x).
>
> an implementation could be like this:
> class frac(object): # PS: This (object) thing will be removed in
> python 3.0, right?
> def __init__(self, numerator, denominator):
> pass
> def __add__(self, other):
> pass
> #...
>
> (I have an implementation of the frac class done (this meaning, it
> works for me), and although it's pretty dirty, I'd be happy to post it
> here if you want it.)
>
> My idea, in summary would be that this python shell session is true:
>
> >>> 5/2

> 2 1/2
> >>> f = 5/2
> >>> f.__class__

> <type 'frac'>
> >>> # or <class 'frac'>
> >>> (0.6).__class__ # Treat decimal literals as fractions, so that...

> <type 'frac'>
> >>> 1/3+0.6

> 14/15
> >>> # That was easy.

>
> I would like to get some feedback on this idea. Has this been posted
> before? If so, was it rejected? and for what?
> Also, I would like to know if you have improvements on the initial
> design, and if it would be appropiate to send it as a PEP.


The gmpy module has unlimited precision rationals.
Works pretty good, too.

 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      10-01-2007
On Sep 30, 9:35 pm, andresj <(E-Mail Removed)> wrote:
> I was doing some programming in Python, and the idea came to my mind: using fractions instead of floats when doing 2/5.
> (...)
> I would like to get some feedback on this idea. Has this been posted
> before? If so, was it rejected? and for what?


Is internet down today ?

http://pypi.python.org/pypi/clnum/1.2
http://www.python.org/dev/peps/pep-0239/

George

 
Reply With Quote
 
Andres Riofrio
Guest
Posts: n/a
 
      10-01-2007
PS: Sorry, George Sakkis, for the double emailing... I forgot to add
python-list in the To: field the first time.

Haha. Ok. Thank you for pointing me to those links . I hadn't
thought of searching for the word 'rational' instead of 'decimal'...

>From what I've read, seems that the principal reason for rejecting the

PEP is that there was not much need (enthusiasm)... Well, then I have
a question: Is there a way to make 5/2 return something other than an
integer? I can do:
class int(int):
def __add__(self, other):
pass
but that will only work if I do int(5)/int(2)...

(setting __builtin__.int=int doesn't work, either)

What I'd like is to be able to implement what I put in the proposal,
as I don't think it's a really big language change...

On 9/30/07, George Sakkis <(E-Mail Removed)> wrote:
> On Sep 30, 9:35 pm, andresj <(E-Mail Removed)> wrote:
> > I was doing some programming in Python, and the idea came to my mind: using fractions instead of floats when doing 2/5.
> > (...)
> > I would like to get some feedback on this idea. Has this been posted
> > before? If so, was it rejected? and for what?

>
> Is internet down today ?
>
> http://pypi.python.org/pypi/clnum/1.2
> http://www.python.org/dev/peps/pep-0239/

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-01-2007
En Sun, 30 Sep 2007 23:36:23 -0300, George Sakkis
<(E-Mail Removed)> escribi�:

> On Sep 30, 9:35 pm, andresj <(E-Mail Removed)> wrote:
>> I was doing some programming in Python, and the idea came to my mind:
>> using fractions instead of floats when doing 2/5.
>> (...)
>> I would like to get some feedback on this idea. Has this been posted
>> before? If so, was it rejected? and for what?

>
> Is internet down today ?
>
> http://pypi.python.org/pypi/clnum/1.2
> http://www.python.org/dev/peps/pep-0239/


And gmpy: http://www.aleax.it/gmpy.html

--
Gabriel Genellina

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-01-2007

"Andres Riofrio" <(E-Mail Removed)> wrote in message
news:52c50880709302010n7cbc070fn6223bd94a67542dd@m ail.gmail.com...
| a question: Is there a way to make 5/2 return something other than an
| integer?

>>> from __future__ import division
>>> 1/2

0.5
>>> 5/2

2.5

tjr



 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-01-2007
En Mon, 01 Oct 2007 00:10:05 -0300, Andres Riofrio
<(E-Mail Removed)> escribi�:

> From what I've read, seems that the principal reason for rejecting the
> PEP is that there was not much need (enthusiasm)... Well, then I have
> a question: Is there a way to make 5/2 return something other than an
> integer? I can do:
> class int(int):
> def __add__(self, other):
> pass
> but that will only work if I do int(5)/int(2)...


I'm afraid not. But if you are crazy enough you can preprocess your source
using something similar to the tokenize example
<http://docs.python.org/lib/module-tokenize.html>

--
Gabriel Genellina

 
Reply With Quote
 
Laurent Pointal
Guest
Posts: n/a
 
      10-01-2007
Andres Riofrio a crit :
<zip>
> Well, then I have
> a question: Is there a way to make 5/2 return something other than an
> integer?


>>> from __future__ import division
>>> 5/2

2.5


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Fractions in Windows 7 Calculator Nathan Sokalski Windows 64bit 9 02-11-2010 03:35 PM
Font with fractions JP Computer Support 3 02-08-2009 02:15 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
XSL-FO...Fractions? Lee Jackson XML 3 12-17-2004 12:18 PM
Decimal places and huge fractions. Eoin Mcloughlin Perl 5 02-13-2004 08:33 PM



Advertisments