0

My professor wrote the second code snippet during a demo. Their output was a float. For me, only the first code snippet is a float. Any idea why there is a difference? The second code snippet seems to have integers divided by integers, added to integers so I'm not sure why it was converted to a float.

n = 4
total = 0
for i in range(n+1):
  total = total + 1 / float(2**i)
return total
n = 4
total = 0
for i in range(n+1):
  total = total + 1 / 2**i
return total
4
  • 2
    obviously, the float is the difference :-) what python version do you use? Commented Sep 23, 2020 at 19:40
  • 13
    In Python 2, int/int=int. In Python 3, int/int=float Commented Sep 23, 2020 at 19:42
  • 2
    But in the Python2 case, take care of from __future__ import division :-) Commented Sep 23, 2020 at 19:44
  • 1
    Sorry for the confusion, I meant that the second code snippet produced a different result for me than it did during the demo. Not what is the difference between the two code snippets. From these answers I think that the difference might have been that I was using Python2 and the teacher was not Commented Sep 23, 2020 at 21:06

2 Answers 2

5

As khelwood has mentioned, In Python 2 when you divide an integer with an integer, the result is also an integer, But in Python 3, the result becomes a float. Let me demonstrate

Python 2

# 10/6 == 1
# 10/3 == 3

Much like C++'s division operator, it drops any fractional value and returns an integer. However this changes for Python 3

Python 3

#10/6 == 1.6666666666666667
#10/3 == 3.3333333333333335

In python 3, the division operator will return float(dividing int/int)

If you want float division in Python 2, you need to from __future__ import division like Jan mentioned.

This will let you divide to get a float value

Sign up to request clarification or add additional context in comments.

3 Comments

And integer division is // in both, so 3 // 2 is 1 int in Python 3 but was also in 2, explicit integer division. the old way for float div there was 3 / 2.0 which is 1.5 also in Python 2 of course. but the future import is nice for 3 compatibility.
@antont Is there something I missed in the answer? if so, you can edit it to add the missing point 😁
Nothing is missing really, but it might be useful complementary information to tell about the integer division operator there. As the topic of float vs int divisions is already up. But it's not really relevant to the actual question where the professor is getting float divs and that's what folks usually want.
2

In Python 2, division of two ints produces an int. In Python 3, it produces a float. I think you are using python 2, and you Professor python 3.

1 Comment

from __future__ import division should be mentioned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.