0

I am writing a script for a software "namely Abaqus" which uses an old version of python. I am having a problem with the following operation, python 3.7 gives the correct answer while python 2.x gives a bad result neglecting everything between the parenthesis.

x = 33
y = 21500 * ((0.1 * x) ** (1/3))
print(y)

As I mentioned, I tried to verify the calculation on a python 3.7 script and it worked giving the following output:

32009.31938850729

while the result appearing in the software is simply 21500. I am really confused. Should this be written in a different way in older python?

Aziz
  • 102
  • 8
  • python2 `/` is int division and 1/3 => 0 so you have `21500 * ((0.1 * x) ** (0))` => `21500 * 1` – azro Jun 03 '20 at 07:27
  • In this case, should I change the assignment to variables in case they are to be used in division operations later? I mean should I write: `x = 33.`? – Aziz Jun 03 '20 at 07:32
  • The problem comes from 1/3, not 33*0.1 (problem is on division not multiplication AND 0.1 is already float) you may do 1.0/3 – azro Jun 03 '20 at 07:34
  • Understood, but I mean in case I have `x/2` later, will python interpret x as a float if it is written as an integer? because I have a lot of variables and tons of calculations in the script. – Aziz Jun 03 '20 at 07:36
  • 1
    Hum in this case yes, if x is involved in division, make it float first – azro Jun 03 '20 at 07:37

0 Answers0