[Numpy-discussion] Integer cast problems

Keith Goodman kwgoodman at gmail.com
Thu Feb 12 12:36:14 EST 2009


On Thu, Feb 12, 2009 at 9:21 AM, Ralph Kube <ralphkube at googlemail.com> wrote:
> The same happens on the ipython prompt:
>
> 0.145 * 0.005 = 28.999999999999996
> N.int32(0.145 * 0.005) = 28
>
> Any ideas how to deal with this?

Do you want the answer to be 29? N.int32 truncates. If you want to
round instead, you could use that standard trick of adding 0.5:

>> np.int32(0.5 + 0.145 / 0.005)
   29

or

>> np.round(0.145 / 0.005)
   29.0



More information about the NumPy-Discussion mailing list