0

When I run the following code in Jupyter 4.3.1 (Python 2.7.13)

import numpy as np
for i in np.arange(5, 6, 0.1):
    print(i)

I got the following output

5.0
5.1
5.199999999999999
5.299999999999999
5.399999999999999
5.499999999999998
5.599999999999998
5.6999999999999975
5.799999999999997
5.899999999999997

However, when I run the code in the console, I got the following output

5.0
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9

Why does the Jupyter output differ from the console output?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
com
  • 2,606
  • 6
  • 29
  • 44
  • `0.1` in code is not exactly 0.1 – chux - Reinstate Monica Apr 26 '18 at 19:45
  • @ThierryLathuille, I've edited the question, for some reason the output is different in console and in jupyter – com Apr 26 '18 at 19:46
  • 1
    I don't know what 'console' you're using. In the REPL with Python 3.6, I get exactly what you get with Jupyter. Your console prints rounded values of your numbers. – Thierry Lathuille Apr 26 '18 at 19:50
  • @ThierryLathuille: The question specifies Python 2.7.13. When I try with 2.7.10, I get the output shown for the “console” output. – Eric Postpischil Apr 26 '18 at 19:52
  • @com Please change `print` line to: `print type(i), str(i), repr(i)`, and show results for both environments. – Netch Apr 27 '18 at 04:24
  • @ThierryLathuille: I reopened this question because the problem it asks about is a difference between two pieces of software. It is not a question about how floating point works. Please do not promiscuously close questions as duplicates of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Eric Postpischil Apr 27 '18 at 09:38
  • @roganjosh: I reopened this question because the problem it asks about is a difference between two pieces of software. It is not a question about how floating point works. Please do not promiscuously close questions as duplicates of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Eric Postpischil Apr 27 '18 at 09:39
  • @PatrickArtner: I reopened this question because the problem it asks about is a difference between two pieces of software. It is not a question about how floating point works. Please do not promiscuously close questions as duplicates of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Eric Postpischil Apr 27 '18 at 09:40
  • @MichalTurczyn: I reopened this question because the problem it asks about is a difference between two pieces of software. It is not a question about how floating point works. Please do not promiscuously close questions as duplicates of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Eric Postpischil Apr 27 '18 at 09:41
  • @GillesGouaillardet: I reopened this question because the problem it asks about is a difference between two pieces of software. It is not a question about how floating point works. Please do not promiscuously close questions as duplicates of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Eric Postpischil Apr 27 '18 at 09:42
  • Are you absolutely sure that you're using the same versions of Python and NumPy in both environments? What do `np.__version__` and `sys.version_info` show in both cases? I suspect that your Jupyter session is actually using Python 3. – Mark Dickinson Apr 27 '18 at 18:28

0 Answers0