How about adding rational fraction to Python?

Arnaud Delobelle arnodel at googlemail.com
Thu Feb 28 18:59:47 EST 2008


On Feb 28, 10:41 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
[...]
> The interesting case is -1/2. According to the argument that "2 doesn't
> go into 1", -1/2 should also return 0. But that's not what Python
> returns, so it looks like the "int division" camp is screwed no matter
> whether Python keeps the status quo or the other status quo.

I'm in the "int division camp" and I like that -1 / 2 is -1.  It's
nice because I can be sure that for any p and q:

* 0 <= p%q < q
* p = q*(p/q) + p%q

In other words, p/q is the largest r such that rq <= p.

It ensures that / and % are well behaved in a lot of situations, e.g:

* (x - y)%n == 0 if and only if x%n == y%n
* if a/n == b/n then abs(a - b) < n
...

So that doesn't screw me, on the contrary I find it a mathematically
very sound decision.  What screws me is that I'm going to have to type
p//q in the future.

--
Arnaud



More information about the Python-list mailing list