[Tutor] from __future__ import division
Steve Willoughby
steve at alchemy.com
Sun Mar 23 19:51:38 CET 2008
Dinesh B Vadhia wrote:
> I spent fruitless hours trying to get a (normal) division x/y to work and then saw that you have to declare:
normal division x/y works just as expected, with one caveat: remember
that if you divide two *integer* values, you will get an *integer*
division operation yielding an *integer* result. So:
1.0 / 2.0 --> 0.5
1.0 / 2 --> 0.5
1 / 2.0 --> 0.5
1 / 2 --> 0
So if you make sure at least one operand is a real number you'll get
a real result. This is now division works in many programming languages
including C and C++.
In the future, Python will switch to always yielding a real result,
and to force an integer division operation you use the special "//"
integer division operator. If you want that behavior now, just
import that "from the future":
from __future__ import division
now:
1 / 2 --> 0.5
4 / 2 --> 2.0
1 // 2 --> 0
4 // 2 --> 2
HTH HAND
>> from __future__ import division
>
> .. at the top of a module file. What is this all about?
>
> Dinesh
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list