Take the 2-minute tour ×

Answering one question Why does scipy.stats.nanmean give different result from numpy.nansum?, I realized, multiplying numpy.int32 by a float results in different float result compared to a Python POD int with a float.

Is there a reason to cause a float approximation when using numpy.int32

>>> numpy.int32(1) * 0.2
>>> 1 * 0.2
share|improve this question
While it doesn't answer the question, it appears you are concerned about the precision of your output, you may want to mix similar types and then perform operations, like numpy.float32(1)*numpy.float32(0.2). –  michaelt Jan 23 '13 at 9:17
add comment

1 Answer

up vote 5 down vote accepted

The two expressions give results that are identical in value but have different types:

In [17]: numpy.int32(1) * 0.2 == 1 * 0.2
Out[17]: True

In [18]: type(numpy.int32(1) * 0.2)
Out[18]: numpy.float64

In [19]: type(1 * 0.2)
Out[19]: float

The different output is purely due to the difference in default formatting between numpy.float64 and float.

If we reverse the types, the output also reverses:

In [12]: float(numpy.int32(1) * 0.2)
Out[12]: 0.2

In [13]: numpy.float64(1 * 0.2)
Out[13]: 0.20000000000000001

It's purely a display issue. There is no numerical difference here.

share|improve this answer
Isn't it because 0.20000000000000001 == 0.2 is True. The difference as you see is noticeable in the linked question. –  Abhijit Jan 23 '13 at 9:13
If 0.20000000000000001 and 0.2 compare equal (which they do), this means they have identical 64-bit IEEE 754 representation after rounding, which means they are represented by exactly the same floating-point number. There is no numerical difference here, it's a red herring. –  NPE Jan 23 '13 at 9:23
There is no numerical difference here. Good to learn –  Abhijit Jan 23 '13 at 9:47
add comment

Your Answer


