rvictordelta
rvictordelta

Reputation: 668

Getting around floating point rounding issue

Df.round() is returning frustrating results.

In the following example, I am trying to round a decimal to five digits. Each decimal has a 5 in the sixth decimal position. When I round, I expect the fifth decimal value to round up. This only happens sometimes..

df2 = pd.DataFrame([0.703125, 0.831215])
df2
Out[4]: 
          0
0  0.703125
1  0.831215
df2.round(5)
Out[5]: 
         0
0  0.70312
1  0.83122

After some googling, it sounds like the issue relates to how floating point numbers are represented by the computer. What's a practical way to get around this issue?

Upvotes: 0

Views: 74

Answers (1)

John Zwinck
John Zwinck

Reputation: 249113

You say "I expect the fifth decimal value to round up". OK, but in what circumstances? Given that your example inputs only have 6 significant digits, and a Python float (64-bit) supports about 15 digits, you can safely add a tiny amount to get the behavior you want:

(df2 + 1e-12).round(5)

It gives:

         0
0  0.70313
1  0.83122

This assumes the largest value in df2 is under 1000 or so, and the smallest value is less than about 1e-6 assuming 6 sigfigs. The addition of a small amount makes sure any trailing "5"s are rounded up, so long as your inputs are within some known range where the small addition won't make the results incorrect.

Upvotes: 4

Related Questions