Integer math question

John Roth newsgroups at jhrothjr.com
Sat Jan 3 12:45:34 EST 2004


"Frank" <mersmann at szut.uni-bremen.de> wrote in message
news:3987e01c.0401030832.114c6f2a at posting.google.com...
> Hi,
>
> can anybody help with the following problem?
>
> In C++
>
> i = 5 / 10 and
> i = -5 / 10 both have the same result 0.
>
> In python
>
> i = 5 / 10 gives me 0 as expected, but
> i = -5 / 10 gives -1 as result.
>
> Is this a feature or a bug? I remember Delphi gave me the same result as
> C++.

That's a feature. Integer division is explicitly defined in
Python to do exactly that.

The basic thing to remember is that the *correct*
mathematical result of dividing one integer by
another integer is a rational. Python does not
have a rational data type, so it has to pick one
of the multitude of possible ways of rounding a
non-integral result to an integer.

There is no universally right answer to this process:
the "right" answer to any rounding problem is
what the customer wants it to be.

John Roth

>
> TIA,
> Frank





More information about the Python-list mailing list