遍历列表时减去的值有随机错误计算

values subtracted while iterating through list has random miscalculations

我正在尝试获取两个收盘价之间的价格差异。这个想法是将接下来几天的收盘价从前几天的收盘价中减去。找出不同之处。在随机的时间间隔内,它似乎有一个不准确的响应!

这是前 40 个条目的输出!

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

处理这个的代码是:

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)

我不确定为什么会导致这种情况,但我知道它与 x 变量及其设置方式有关

如果有更好的方法来执行此操作,那么;我愿意接受建议!

它不是不准确,只是小以至于它用科学记数法表示。例如,第二个值为 -2.79680151740735E-05。那E-05的意思是“乘以10的-5次方”,相当于-0.0000279680151740735,只是一个较短的表示。

如果您不想看到科学记数法,请将其明确格式化为所需的精度,例如:

 >>> 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

为所有 float 输出做这样的事情通常是个好主意,以便很好地对齐字段以提高人类的可读性。如果您只关心程序重用,那么 float 的自然输出格式很好(而且是最小的;它不会添加额外的数字,除非它们是重现该值所必需的),但人们通常喜欢 less precise/minimalist 格式更一致的输出。