0

i have created this dataframe: ones_df = pd.DataFrame(data=1, index= myscores.index, columns=['tracker'])

i have a loop that subtracts .05 from the values( starting at 1) until it reaches zero 0. then stop subtracting. BUT once it gets to .05 and tries to subtract .05 again. i get the value -3.191891e-16

IPdb [27]:
tracker_df.iloc[0]
tracker    0.05
Name: 4155.0, dtype: float64

IPdb [28]:
tracker_df.iloc[0] -.05
tracker   -3.191891e-16
Name: 4155.0, dtype: float64

i am not sure how, if i start with data value 1 and continue to just subtract .05 this happens. when I use float() and it shows the "true value" which is less than then .05 i was expecting, so technically this will never get to 0 and be True

IPdb [29]: float(tracker_df.iloc[0])
0.049999999999999684

so my questions are, how can i fix this from the start (so i dont need to do a round(2) ) in order to get to my zero value, and also why does this happen. thanks

jon rios
  • 388
  • 1
  • 3
  • 11
  • It's because float numbers are **always** rounded, since computer precision is finite (differently from real numbers). Try reading [this thread](https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python) to better understand how to resolve the problem – crissal May 16 '21 at 19:48
  • i attached .round(2) to the DataFrame constructor and it still wont test True for 2 decimals place, .90 == tracker_df[0], False. float(tracker_df[0]) value shows as .899999999 – jon rios May 16 '21 at 20:04
  • The point is, you need to compare with `math.isclose`, not `==` – crissal May 16 '21 at 20:16
  • Briefly speaking: this is not related to python at all, but to floating-point arithmetic, and in particular to the fact 0.05 is *not* a binary floating-point number (in a similar way as 1/3≈0.333333… is *not* a decimal number), so rounding occurs (in a very early stage here: just for storing this litteral value; even before doing the subtraction operations). – ErikMD May 16 '21 at 20:26
  • the discussion of floating-point arithmatic is above my pay-grade. i did the cond comparison using round(2), which (for me) was easier/faster. thanks – jon rios May 16 '21 at 22:28
  • Or maybe another option would just be to use a loop over the integers; then multiplying the integer values by .05 (?) this way, no rounding/termination issue should be expected. – ErikMD May 18 '21 at 17:57

0 Answers0