0

When I run the following code in the interpreter vs a script I receive different results. I'm trying to understand why. Initially I thought it was an issue with the numpy.mean() function.

num_friends=[29, 74, 41, 29, 81, 47, 48, 69, 33, 17, 13, 65, 14, 71, 76, 41, 22, 11, 57, 38, 78, 30, 53, 82, 59, 89, 57, 70, 16, 44, 75, 48, 35, 49, 12, 97, 85, 16, 85, 55, 64, 59, 94, 79, 91, 65, 12, 56, 33, 33, 33, 79, 46, 30, 51, 90, 84, 79, 11, 48, 56, 90, 45, 99, 57, 64, 35, 56, 84, 45, 69, 42, 56, 33, 31, 98, 97, 12, 10, 85, 96, 83, 16, 55, 36, 10, 52, 44, 43, 56, 27, 23, 95, 25, 44, 38, 17, 94, 97, 25]

print(np.sum(num_friends)/len(num_friends))
print(np.mean(num_friends))

I generated this list randomly. This is the only set I was able to create the error with. When I recreated the list with a new random.randrange(10,100) list the issue disappeared.

Results in the interpreter:

52.880000000000003

Results using a .py file:

52.88

I get the same odd results with SciPy. Thoughts?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    They are the same value: `52.88 == 52.880000000000003` is True. In an interpreter, if you simply type in the variable or expression with type `numpy.float64()`, `numpy` gives the long version. (E.g. try `>>> np.float64(52.88)`) If you display it using the print function, the short version is printed. (I'd make this a longer answer, but it feels like a duplicate question.) – Warren Weckesser Aug 11 '17 at 04:41
  • Did you use `print` in the interpreter? If you simply type an expression in the interpreter it shows the result of `repr()`, while `print()` uses `str()` for conversion, which is usually nicer formated. – MB-F Aug 11 '17 at 06:05
  • Thanks! This was exactly the answer. Upon adding print() to the interpreter the displayed results match. I figured it was something I was doing but had to ask. – BearlyThere Aug 11 '17 at 18:43
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – rayryeng Mar 06 '18 at 17:27

0 Answers0