[Numpy-discussion] Integer cast problems

Keith Goodman kwgoodman@gmail....
Thu Feb 12 11:36:14 CST 2009


On Thu, Feb 12, 2009 at 9:21 AM, Ralph Kube <ralphkube@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