Essentially, float32
is numpy
's dtype
. The reason why you see some difference in the precision when converting float64
to float32
is because 123456789.0
cannot be accurately represented using float32
which is a 32-bit dtype (1 sign bit, 8 bits exponent, 23 bits mantissa).
In general, float32
requires half of the memory that float64
requires to represent a numerical value , however float32
can represent numbers less accurately compared to float64
.
Note there is no workaround for this. If you need to represent particular numbers that cannot be represented using a 32-bit dtype like float32
, then go for higher precision dtypes (float64
).