Fwd: Using fractions instead of floats

Stargaming stargaming at gmail.com
Mon Oct 1 10:21:52 EDT 2007


On Sun, 30 Sep 2007 20:10:05 -0700, Andres Riofrio wrote:

[snip]
>>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...
[snip]

You could make up an example implementation by using fractionizing_int
(1) / fractionizing_int(3) (or whatever name you come up with).

I don't know of any particularly nice way to Just Let It Work anywhere in 
python (perhaps some ugly byte code hacks, automatically wrapping ints). 
So, you would have to implement it in C if you want to propose this for 
CPython. And that's no 5 minute task, I guess.

Still, having this `int` behaviour working globally would include those 
previously mentioned irrational numbers (but Pi is rational in any 
visualization anyways, so FWIW that wouldn't be too much of a blockade). 
But where would you put normal integer division then? 
Introducing a new type seems best to me.

You could, as well, try to implement this in PyPy. If this would hit the 
broad masses is another question, though. ;)

Regards,
stargaming



More information about the Python-list mailing list