指数不正常

时间:2013-08-22 20:24:41

标签: python math operators exponent

我在python 2.6中工作。任何人都可以弄清楚为什么我的变量平方在min()函数内部时不能正确计算? 为了说明,我展示了自己执行的变量** 2,工作正常。 这是代码,后面是一些结果:

from __future__ import with_statement
import csv, re, time
timestr = time.strftime("%Y%m%d_%H%M%S")

fileinput = 'mp_20130822.csv'
out = open('output2.csv',"wb")
writer = csv.writer(out, delimiter = ',')

ESPmax = 100.00
dynmax = "enabled"

def config():
    global argument
    if z>30:
        argument = "T"
    else:
        argument = "F"

with open(fileinput,'rU') as inputfile:
    reader = csv.reader(inputfile, delimiter = ';')
    for line in reader:
            line = re.split(",| ",line[0])
            side = str(line[4]) #is "Bid" or "Ask"
            e = float(line[5])
            z = float(line[6])
            t = float(line[33])
            FW = float(line[34])
            FS = max(float(line[35]),200)
            if FW == 0:
                continue
            if (FS == 0) or (FS == 1):
                continue
            if side == "Ask":
                LE = t-e
            else:
                LE = e-t
            LEP = LE/(FW/2)
            ESP = z/(FS/2)
            if dynmax == "enabled":
                ESPmax = min(LEP**2,ESPmax)

    config()
    if (argument == "T"):
        print ('side, e, z, t, FW, FS')
        print ('LEP,LEP,ESPmax')
        print (side, '%.2f'%e, '%.2f'%z, '%.2f'%t, '%.2f'%FW, '%.2f'%FS)
        print ('%.3f'%LEP,'%.3f'%LEP,'%.5f'%ESPmax)
        print '%.5f'%(LEP*LEP)

结果:

side, e, z, t, FW, FS
LEP,LEP,ESPmax
('Ask', '1.90', '50.00', '1.95', '0.24', '651.00')
('0.423', '0.423', '0.00130')
0.17880
side, e, z, t, FW, FS
LEP,LEP,ESPmax
('Ask', '8.40', '40.00', '8.43', '0.17', '4933.00')
('0.348', '0.348', '0.00130')
0.12145
side, e, z, t, FW, FS
LEP,LEP,ESPmax
('Ask', '8.40', '40.00', '8.43', '0.17', '4919.00')
('0.370', '0.370', '0.00130')
0.13667

1 个答案:

答案 0 :(得分:0)

缩进显然是Python中的SyntaxError,因此不清楚代码实际上做了什么。虽然我很确定你以某种方式设法将0.00130分配给ESPmax,然后一旦你到达那里

ESPmax = min(LEP**2,ESPmax)
对于ESPmax的值,

行不会更改LEP