Let's say I have a dataframe with 2 columns of floats with 4 or 5 digits of pricision as below:
dt = pd.DataFrame({"OPEN": [-0.00011,-0.0114, 0.0066,-0.0044,-0.0012,-0.0005,
0.0005,-0.0037, -0.0029, 0.0034, 0.0003, 0.0001 ],
"CLOSE": [-1.20011,-3.0114, 0.4066,-0.0074,-0.0016,-5.0005,
0.0225,-0.0027, -0.0026, 0.0064, 0.0043, 4.0001 ],})
and I would like to obtain dt["delta"]=dt["OPEN"] - dt["CLOSE"]
correctly. However, as these columns are floats
then the result value is not precise! so, for example 0.0003-0.0002
should be 0.0001
however it gives me 0.0000999999999999999
Here is my curruent solution, but it does not work!
from decimal import *
getcontext().prec = 6
delta = []
for i in np.arange((dt.size)-1):
print("CLOSE: ",Decimal(dt.loc[i,"Close"]))
delta.append(Decimal(dt.loc[i,"Close"]) - Decimal(dt.loc[i,"Open"]))
print("delta",delta)
My question is, how can i subtract the "CLOSE" and "OPEN" columns correctly ?
Point:
When i run my code, i got the following as print. i am wondering, why the Decimal values still have 52 digits, meanwhile i speicified the prec==6
`CLOSE: 1.0578000000000000735411731511703692376613616943359375
CLOSE: 1.046300000000000007815970093361102044582366943359375
CLOSE: 1.052799999999999958077978590154089033603668212890625
CLOSE: 1.0484999999999999875655021241982467472553253173828125
CLOSE: 1.0471999999999999086952584548271261155605316162109375
CLOSE: 1.0464999999999999857891452847979962825775146484375
CLOSE: 1.047099999999999919708670859108678996562957763671875`