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

3
  • 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) Commented Feb 2, 2015 at 20:22
  • possible duplicate of Is floating point math broken? There it is. Commented Feb 2, 2015 at 20:23
  • np.uint32 truncates. Try np.uint32(1.99) Commented Feb 2, 2015 at 20:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.