0

Here's my code:

x = 20
for i in range(1,7):
    y = x/i
    print(i)    #  These lines are just to show what
    print(y)    #  the code is doing. I know it's not elegant.
    print(i*y)  #  And is not my final solution
    print('\n')

Here's my output:

1
20.0
20.0

2
10.0
20.0

3
6.666666666666667
20.0

4
5.0
20.0

5
4.0
20.0

6
3.3333333333333335
20.0

So why is every answer to the equation the same (20)? When i clearly changes? Say for the third iteration, when i=3, going to the interpreter and performing 3*(20/3) yields 18 correctly and not 20. So I'm not sure what's going on.

This is answered below. The difference between floats in python2.7 and python3

  • 2
    Try with `y = x // i` which performs integer division. – R2RT Dec 20 '17 at 23:01
  • https://stackoverflow.com/a/2986161/7505395 - the short answer is;: floating point arythmetic is frought with rounding errors and what you see is not always what you get even if you see it due to the resolution of how floats are stored... – Patrick Artner Dec 20 '17 at 23:02
  • Possible duplicate of [python floating number](https://stackoverflow.com/questions/2986150/python-floating-number) – Patrick Artner Dec 20 '17 at 23:02
  • 4
    Because i*(x/i), the i cancel out and the result is x – Allen W Dec 20 '17 at 23:02

2 Answers2

1

In Python 2.x / performs integer division, leading to what you expect:

$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x=20
>>> [x/i for i in range(1,7)]
[20, 10, 6, 5, 4, 3]
>>> [x/i*i for i in range(1,7)]
[20, 20, 18, 20, 20, 18]

But in Python 3, / performs a proper float division, such that the results are mathematically correct:

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x=20
>>> [x/i for i in range(1,7)]
[20.0, 10.0, 6.666666666666667, 5.0, 4.0, 3.3333333333333335]
>>> [x/i*i for i in range(1,7)]
[20.0, 20.0, 20.0, 20.0, 20.0, 20.0]

Just make sure you pick the interpreter you want and perform the operation you need. Remember: explicit is better than implicit! So, as others pointed out, just call // to do integer division.

Pavel
  • 7,436
  • 2
  • 29
  • 42
  • Yup! That was it. I guess I just didn't realize i typed python and not python3 at the terminal. So I was in fact using 2 different versions. I know about //, just didn't know why I was seeing the discrepancy. Thanks all! -John – trimonkeytri Dec 20 '17 at 23:55
1

The short answer: the key is that in python3 they seem to cast your division of two ints into floats instead of leaving them as ints.

Here's my justification:

I would guess that you are running two different versions of python: python3 when running the .py file, and python2 as your interpreter.

Here's my output when I use python3:

/usr/local/bin$ python3
Python 3.6.4
>>> print(3 * (20/3))
20.0

And here's my output when i use python2:

/usr/local/bin$ python
Python 2.7.13
>>> print(3 * (20/3))
18