0

This code

np.uint32( 1.13*100 )

returns 112

It should be 113.

How can I get around the strange rounding issue and make it return 113?

I am on numpy 1.9.1

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
Ginger
  • 8,320
  • 12
  • 56
  • 99
  • start with `1.13*100` you'll see it is not `1.12`. (there has to be a question here that explains what is wrong with floats, but I can't find it for now) – njzk2 Feb 02 '15 at 20:22
  • possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) There it is. – njzk2 Feb 02 '15 at 20:23
  • `np.uint32` truncates. Try `np.uint32(1.99)` – dawg Feb 02 '15 at 20:57

1 Answers1

4

If you can avoid it, don't cast the result of a floating point multiplication directly to an integer. Casting doesn't round the number to the nearest integer, it merely drops the decimal part of the float.

The problem is that floating point numbers are often only close approximations of real numbers. Arithmetical operations can exacerbate discrepancies. In your example:

>>> 1.13*100
112.99999999999999

The decimal part is dropped upon casting to an integer, leaving 112.

It would be better to round the number to the nearest integer first (e.g. with np.round) and then cast it to an integer type:

>>> np.int32(np.round(1.13*100))
113
Alex Riley
  • 169,130
  • 45
  • 262
  • 238