-2

I have the following formula:

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

Example:

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

When I use the python interpreter (2.7.6) for this calculation, I got this result:

86266

When I use a calculator (Google for example) I got this result:

168471.066239

I assume the second is the correct result.

What's wrong about the calculation in Python?

akash karothiya
  • 5,736
  • 1
  • 19
  • 29
Asharad
  • 13
  • 2
  • 3
    Don't post links to screenshots of numbers. Just write the numbers in your question. – khelwood Jul 28 '17 at 09:45
  • Make it clear in python questions what version you're using. – TomServo Jul 28 '17 at 09:53
  • Python 2 does an integer division on your `((SZ1 - SZ0) / (WMZ1 - WMZ0))` calculation instead of a float division, results in zero so it only prints the left hand side of summation. Check this [question](https://stackoverflow.com/q/1267869/826970) for the solution. – umutto Jul 28 '17 at 09:58

2 Answers2

7

Basically Python 2.7 and 3.3 calculations are different to each other.

Python 3.3 return 0.1 for 1/10, while Python 2.7 return 0. You can enable the new division operator by using __future__

>>> from __future__ import division
>>> print(86266 + (((168480 - 86266) / (703786 - 510531)) * (703765.0 - 510531)))
168471.066239
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
-1

It has to do with the python version and the devision operator

Examples:

Using 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

Using python 3.3:

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

print(x)

168471.066239
seralouk
  • 30,938
  • 9
  • 118
  • 133