Hack request: rational numbers

Michael Hudson mwh21 at cam.ac.uk
Thu Jan 25 15:59:22 EST 2001


Pearu Peterson <pearu at cens.ioc.ee> writes:

> Hi!
> 
> I am using rational numbers from the gmpy module and I find that creating
> one in Python using
> 
> >>> mpq(3,4)
> 
> is rather clumsy. Clearly, minimal representation of this rational number
> is
> 
>     3/4
> 
> but in Python this expression has different meaning.

Here's an idea: subclass code.InteractiveConsole.  Use bytecodehacks
to find bytecodes like

LOAD_CONST <integer1>
LOAD_CONST <integer2>
BINARY_DIVIDE

to

LOAD_GLOBAL "mpq"
LOAD_CONST <integer1>
LOAD_CONST <integer2>
CALL_FUNCTION 2

This isn't actually very difficult, but I'm not going to do it.

> 3) What are changes that these hooks will be accepted to Python core?

Can't speak for the people whose opinions really count, but my guess: none.

> 4) Is anybody else working with builtin rational number support for
> Python?

Maybe.  I suspect not enough to make a difference.  It's mooted that 

1/2

will produce something other than "0" in Py3K, but I can't remember
whether a rational or a floating point result seemed most likely last
time this went around.

Cheers,
M.

-- 
  Good? Bad? Strap him into the IETF-approved witch-dunking 
  apparatus immediately!                        -- NTK now, 21/07/2000



More information about the Python-list mailing list