Home > Back-end >  values subtracted while iterating through list has random miscalculations
values subtracted while iterating through list has random miscalculations

Time:10-20

I am trying to get the difference in price between two closing values. The idea is to take the following days closing price and subtract it from the previous days closing price. To find the difference. At random intervals, it seams to have a response that is inaccurate to what it should be!

Here is the output of the first 40 entries!

Date           Close Price            Difference
2017-10-18  0.033458707800705645    -0.033458707800706
2017-10-19  0.03348667581587972 -2.79680151740735E-05
2017-10-20  0.03826082774957325 -0.004774151933694
2017-10-21  0.03609659102616786 0.002164236723405
2017-10-22  0.03512779945845114 0.000968791567717
2017-10-23  0.03292234137795422 0.002205458080497
2017-10-24  0.03474344279088958 -0.001821101412935
2017-10-25  0.034920358595241154    -0.000176915804352
2017-10-26  0.03416255709673565 0.000757801498506
2017-10-27  0.0339633317691615  0.000199225327574
2017-10-28  0.033280200744110526    0.000683131025051
2017-10-29  0.03637673884863127 -0.003096538104521
2017-10-30  0.03641274396704358 -3.60051184123133E-05
2017-10-31  0.03900364469856915 -0.002590900731526
2017-11-01  0.029734991869035093    0.009268652829534
2017-11-02  0.0273150283760348  0.002419963493
2017-11-03  0.029087283751758824    -0.001772255375724
2017-11-04  0.027827236041855268    0.001260047709904
2017-11-05  0.02811306792657447 -0.000285831884719
2017-11-06  0.028356975188679513    -0.000243907262105
2017-11-07  0.028445804219687537    -8.88290310080246E-05
2017-11-08  0.0320082838822097  -0.003562479662522
2017-11-09  0.04155217850194522 -0.009543894619736
2017-11-10  0.0346107168985187  0.006941461603427
2017-11-11  0.03487128406789406 -0.000260567169375
2017-11-12  0.031119201082441282    0.003752082985453
2017-11-13  0.03198844455483081 -0.00086924347239
2017-11-14  0.0334056810250295  -0.001417236470199
2017-11-15  0.0332199601874297  0.0001857208376
2017-11-16  0.0345875759896623  -0.001367615802233
2017-11-17  0.0330889554385292  0.001498620551133
2017-11-18  0.0338798779076586  -0.000790922469129
2017-11-19  0.0354451442173631  -0.001565266309705
2017-11-20  0.0384655176125493  -0.003020373395186
2017-11-21  0.0365546680645302  0.001910849548019
2017-11-22  0.036492917532763   6.1750531767199E-05
2017-11-23  0.0355301059972517  0.000962811535511
2017-11-24  0.0362489367912106  -0.000718830793959
2017-11-25  0.0389196610309137  -0.002670724239703

The code for handling this is:

price_data = cg.get_coin_market_chart_by_id(user_crypto_answer,vs_currency=user_fiat_answer,days="max")["prices"]

pd_prices = [i[1] for i in price_data]

    x = [0]
    for line in pd_prices:
        a =  x[0] - float(line)
        x.clear()
        x.append(float(line))
        print(a,x[0],line)

Im not sure why its causing this but I have an idea that it has to to do with the x variable and how it is set up

If there is a better way to perform this action then; I am open to suggestions!

CodePudding user response:

It's not inaccurate, it's just so small that it's being represented in scientific notation. For example, the second value is -2.79680151740735E-05. That E-05 means "times 10 to the -5th power", which is equivalent to -0.0000279680151740735, just a shorter representation.

If you don't want to see scientific notation, explicitly format it to a desired precision, e.g.:

 >>> floatval = -0.0000279680151740735
 >>> print(floatval)   # Gets scientific notation
 -2.79680151740735E-05
 >>> print('{:.19f}'.format(floatval))  # Gets precisely 19 digits after the decimal point
 -0.0000279680151740735

It's often a good idea to do something like this for all your float outputs, to align the fields nicely for human readability. If all you care about is programmatic reuse, the natural output format of float is fine (and minimal; it won't add extra digits unless they're necessary to reproduce the value), but humans often like less precise/minimalist outputs that are more consistently formatted.

  • Related