我正在使用 python 2.6。谁能弄清楚为什么我的变量 squared 在 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