Python 3.0 new integer division

Jonathan Gardner jgardner at jonathangardner.net
Tue Apr 8 12:24:57 EDT 2008


On Apr 8, 9:13 am, "Hutch" <A... at MCHSI.COM> wrote:
> We now have a float result when two integers are divided in the same mannor
> as 2.4 or 2.5.
> I can handle that and use the Floor division but a simple question.
>
> Why in the world would you round down the last presented digit to a 6
> instead of just leaving it along as an 8.
> For some reason rounding down for a float in Python does not seem correct.
>
> IDLE 3.0a4
>
> >>> 12345678901234567890123456789012345678901234567890/345
>
> 3.5784576525317586e+46
>
> >>> 12345678901234567890123456789012345678901234567890//345
>
> 35784576525317588087314367504383610663481839327
>                                 ^
>                                 ^|
> 35784576525317586000000000000000000000000000000  == 3.5784576525317586e+46

Floats are weird that way. Part of the problem is you are representing
a decimal number (base 10) in binary (base 2). The other part is that
you can't increment floats by a very tiny amount. In other words, a +
b = a for floats when b is sufficiently small but not zero.

Blame floats, not Python.

If you want precision with fractions, you should be using the Decimal
type, which uses a rational. A rational, if you recall from your math
classes, is one integer divided by another.



More information about the Python-list mailing list