简体   繁体   English

python 2和3的相同代码给出了不同的结果

[英]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从这里的答案或这里的答案中提到的细节

  • In Python2.7, division of two ints produces an int在 Python2.7 中,两个 int 相除产生一个 int
>>> -12/30
-1
  • In Python3, division of two ints produces an float在 Python3 中,两个整数的除法产生一个浮点数
>>> -12/30
-0.4

So depending on what you want所以取决于你想要什么

  • If you want a float for both cases, import from __future__ import division in Python2.7,如果你想在这两种情况下都浮动,在 Python2.7 中from __future__ import division
>>> from __future__ import division
>>> -12/30
-0.4
  • If you want a int in both cases, perform integer division // in Python3如果在这两种情况下都需要 int,请在 Python3 中执行整数除法//
>>> -12//30
-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用JAVA和Python编写的相同代码给出不同的结果(粒子过滤器) - Same code written in JAVA and Python gives different results (particle filter) 不同缩进的相同代码给出不同的结果? - Same code with different indentation gives different results? Python 3.x和Python 2.x中的相同代码给出不同的结果 - Same code in Python 3.x and Python 2.x gives different results Python来自同一代码的不同结果 - Python different results from same code 相同的python代码块在不同的时间给出不同的输出 - Same python code block gives different outputs at different time 相同的Python代码,相同的数据,不同机器上的结果不同 - Same Python code, same data, different results on different machines 相同的Python代码为同一输入字符串返回不同的结果 - Same Python code returns different results for same input string 相同的Python代码对于相同的输入字典返回不同的结果 - Same Python code returns different results for same input dictionary python:从不同目录运行相同代码时,结果不同 - python: different results when running the same code from different directories 相同代码在python3和python2之间的运行结果不同 - different run results between python3 and python2 for the same code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM