[英]Same code for python 2 and 3 gives different results
I have the issue that I run python 3 on my client and the server where I execute the programs run python 2.我有一个问题,我在我的客户端上运行 python 3,而我执行程序的服务器运行 python 2。
So I set up the following script:所以我设置了以下脚本:
from math import radians, cos, sin, asin, sqrt, exp
from datetime import datetime
def dateSmoother(a, b):
#Format the date
a = datetime.strptime(a, "%Y-%m-%d")
b = datetime.strptime(b, "%Y-%m-%d")
diff = (a-b).days
return exp(-(diff/h_date)**2)
def timeSmoother(a, b):
# Since we only got readings from two different times
# We first check to see if they are the same
if (a==b):
return exp(-(0/h_time)**2)
else:
return exp(-(12/h_time)**2)
h_date = 30
h_time = 12
a = "2013-11-01"
b = "2013-11-13"
print(dateSmoother(a, b))
print(timeSmoother("06:00:00", "06:00:00"))
print(timeSmoother("06:00:00", "18:00:00"))
When I run it locally with python 3 I get the following output:当我使用 python 3 在本地运行它时,我得到以下输出:
0.8521437889662113
1.0
0.36787944117144233
However, when I run it on the server I get:但是,当我在服务器上运行它时,我得到:
0.367879441171
1.0
0.367879441171
The problem lies in the division here diff/h_date
问题在于这里的划分
diff/h_date
From the details mentioned in this answer here or this answer here从这里的答案或这里的答案中提到的细节
>>> -12/30
-1
>>> -12/30
-0.4
So depending on what you want所以取决于你想要什么
from __future__ import division
in Python2.7,from __future__ import division
,>>> from __future__ import division
>>> -12/30
-0.4
//
in Python3//
>>> -12//30
-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.