"Byte" wrote:
> That dosnt work either:
>
> sum = 0.1+1/4
> print sum
>
> Just returns 0.1
division has higher precedence than addition, so 1/4 is calculated first,
and the result is then added to 0.1.
and as I've already explained, 1/4 is an integer division, so the result
is rounded down to th
Byte wrote:
> That dosnt work either:
>
> sum = 0.1+1/4
> print sum
>
> Just returns 0.1
You get precedence right? Your equation does not evaluate from left to
right. 1/4 happens first, and since there are no floats you get 0.
in that equation you basically are doing this:
sum = 1/4
print sum
0
On Sat, 08 Apr 2006 08:21:06 -0700, Byte wrote:
> How come:
>
> sum = 1/4
> print sum
>
> returns 0? 1/4=0.25, not 0. How do I fix this?
By default, / does integer division, not floating point. In integer
division, 1/4 is 0, exactly as calculated.
(How many fours go into one? Zero fours go in
Fredrik Lundh's way works: thank a million!
--
http://mail.python.org/mailman/listinfo/python-list
Byte wrote:
> That dosnt work either:
>
> sum = 0.1+1/4
> print sum
>
> Just returns 0.1
That's because the 1/4 is executed first, and the problem mentioned
still applies (i.e. you get a 0, then add it to 0.1).
The best fix for you might be simply to place this line at the start
(before all o
"Byte" wrote:
> How come:
>
> sum = 1/4
> print sum
>
> returns 0?
because 1 and 4 are integer objects, so 1/4 is an integer division, which
rounds down to the nearest integer.
> 1/4=0.25, not 0. How do I fix this?
use floating point numbers:
1.0/4.0 = 0.25
or convert one of the numbers t
That dosnt work either:
sum = 0.1+1/4
print sum
Just returns 0.1
--
http://mail.python.org/mailman/listinfo/python-list
Byte wrote:
> How come:
>
> sum = 1/4
> print sum
>
> returns 0? 1/4=0.25, not 0. How do I fix this?
Make sure there is at least one float in your equation. In your example
Python is doing interger math for you and returing the floor. You need
to give it a hint that you would like to do floating p