- **Python**
(*http://www.velocityreviews.com/forums/f43-python.html*)

- - **Using fractions instead of floats**
(*http://www.velocityreviews.com/forums/t541074-using-fractions-instead-of-floats.html*)

Using fractions instead of floatsI 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. |

Re: Using fractions instead of floatsandresj <andres.j.new@gmail.com> 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 |

Re: Using fractions instead of floatsOn Sep 30, 6:48 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote: > andresj <andres.j....@gmail.com> 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.) |

Re: Using fractions instead of floatsOn Sep 30, 8:35?pm, andresj <andres.j....@gmail.com> 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. |

Re: Using fractions instead of floatsOn Sep 30, 9:35 pm, andresj <andres.j....@gmail.com> 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 |

Fwd: Using fractions instead of floatsPS: 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 <george.sakkis@gmail.com> wrote: > On Sep 30, 9:35 pm, andresj <andres.j....@gmail.com> 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/ |

Re: Using fractions instead of floatsEn Sun, 30 Sep 2007 23:36:23 -0300, George Sakkis
<george.sakkis@gmail.com> escribiï¿½: > On Sep 30, 9:35 pm, andresj <andres.j....@gmail.com> 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 |

Re: Using fractions instead of floats"Andres Riofrio" <andres.j.new@gmail.com> 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 |

Re: Fwd: Using fractions instead of floatsEn Mon, 01 Oct 2007 00:10:05 -0300, Andres Riofrio
<andres.j.new@gmail.com> 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 |

Re: Fwd: Using fractions instead of floatsAndres 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 |

All times are GMT. The time now is 12:56 AM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.