为什么我从Python解释器(2.7.6)和计算器得到不同的结果?

2024-09-30 18:27:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下公式:

SZT = SZ0 + (((SZ1 - SZ0) / (WMZ1 - WMZ0)) * (WMZT - WMZ0))

示例:

86266 + (((168480 - 86266) / (703786 - 510531)) * (703765.0 - 510531))

当我使用python解释器(2.7.6)进行此计算时,得到以下结果:

86266

当我使用计算器(例如谷歌)时,我得到以下结果:

168471.066239

我假设第二个是正确的结果。你知道吗

Python中的计算有什么问题?你知道吗


Tags: 示例解释器计算器公式sz1sztwmztsz0
2条回答

基本上Python 2.73.3的计算是不同的。你知道吗

python3.3为1/10返回0.1,而python2.7返回0。可以使用__future__启用新的除法运算符

>>> from __future__ import division
>>> print(86266 + (((168480 - 86266) / (703786 - 510531)) * (703765.0 - 510531)))
168471.066239

它与python版本和division操作符有关

示例:

使用2.7:

Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

x = 86266 + (((168480 - 86266) / (703786 - 510531)) * (703765.0 - 510531))

print(x)


86266.0

使用python 3.3:

x = 86266 + (((168480 - 86266) / (703786 - 510531)) * (703765.0 - 510531))

print(x)

168471.066239

相关问题 更多 >