9

I'm having a problem with np.round, np.around where it is not rounding properly. I can't include code, because when I do it manually set the value (as opposed to use the my data), the return works, but here is the output:

In [177]: a
Out[177]: 0.0099999998

In [178]: np.round(a,2)
Out[178]: 0.0099999998


In [179]: np.round(a,1)
Out[179]: 0.0

What am I missing? The dtype of a is float32, do I need to change this?

2
  • 4
    Here. read all that Commented Aug 31, 2011 at 4:41
  • 2
    What is your expected output? Maybe have a look at python's decimal type if you really must have an "exact" value. Commented Aug 31, 2011 at 4:48

3 Answers 3

6

Try creating np.float32(0.01) and you will see your answer. You are getting the precision you can already.

>>> import numpy as np
>>> x = 0.01
>>> epsilon = 0.01 - np.float32(0.01)
>>> for n in np.arange(x - 10*epsilon, x + 10*epsilon, epsilon):
...     print(repr(np.float32(n)))
...     
0.0099999979
0.0099999979
0.0099999979
0.0099999988
0.0099999988
0.0099999988
0.0099999988
0.0099999998
0.0099999998
0.0099999998
0.0099999998
0.0099999998
0.010000001
0.010000001
0.010000001
0.010000001
0.010000002
0.010000002
0.010000002
0.010000002
Sign up to request clarification or add additional context in comments.

6 Comments

I see what you mean, but is there a different type I can change it to in order to avert this problem?
yes, for example np.float64 .. but note that it doesn't avert the "problem", it only makes the error smaller.
classic leaky abstraction (joelonsoftware.com/articles/LeakyAbstractions.html). Welcome to floats.
Sorry, the "problem" that I'm having is that when I'm rounding the value, it does not round. numpy.round(.0099999999, decimals=2) is still returning .0099999999999. Thoughts?
@mike it depends what you want to use the rounded number for. with x = numpy.round(0.0099999999, decimals=2) use float(x) to convert to 64-bit precision, or '{0}'.format(x, precision=2) if you need to write the number to two decimal places.
|
3

Note there seems to be an issue with python's round function and numpy.float64 types. See example below:

In [88]: round(np.float64(16.259766999999947), 4)
Out[88]: 16.259799999999998

The only way I could fix this is to convert the numpy.float64 to a float before using the round function as below:

In [89]: round(float(np.float64(16.259766999999947)), 4)
Out[89]: 16.2598

1 Comment

This method doesn't seem to work for me. If x = 5672.1001, then np.round(float(np.float64(x)),1) yields 5672.1000000000004.
0

It does not work for me in cases the number is python's float or np.float64.

once I change the number to np.float32(), it works!

type(85.10000000000001), np.round(85.10000000000001,decimals=2), np.round(np.float32(85.10000000000001),decimals=2)

Results: (float, 85.10000000000001, 85.1)

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.