-4

I tried to run this code on different python platforms got different output for same python code here are the outputs:

Microsoft V.S Code=7654321.0 

And

python official ide=77316373.73737083

My code is,

import math
a=1234567
i=len(str(a))
number=0
while a>0:
    digit=a%10
    number=digit*math.pow(10,i)+number
    a=a/10
    i=i-1;
print(number)
ashraful16
  • 2,122
  • 1
  • 5
  • 25
  • 1
    Please read [ask]. Your question has some mysteries built-in: What's the third python platform? And what versions of Python on each platform? And what output do you get? – barny Jan 08 '21 at 08:00
  • 5
    First answer is with `python 2.x`, second with `python 3.x`. `a=a/10` makes the difference. – Michael Szczesny Jan 08 '21 at 08:03
  • Changed tags because the result of 'n/m' has nothing to do with the editor/IDE used to write that expression. – Terry Jan Reedy Jan 09 '21 at 02:18

3 Answers3

3

It yields different outputs in python 2 and python 3, as there are different behavior of / operator.

In Python 2 / is a "floordiv" - result from dividing integers is rounded to the nearest integer less than result.

In Python 3 / is a "truediv" - result is a floating point number whether you divide integers or floats.

Your IDEs use different python versions, so when you run python code you get different results. VS Code calls default system python which in your case is Python 2, and python ide (IDLE?) uses python which it was installed with - Python 3.

Som-1
  • 191
  • 1
  • 8
0

I am getting 77316373.73737083 only. There might be some other problem. Check your value of a and all the 10 in VS

coderGtm
  • 73
  • 7
0

Looks you want to invert the integer, so use the integer operator ** // rather than float operators. As for why float calculation is different, common float calculation depends on the precision of the python runtime, if precision matters, a good practice is to use professional math libraries.

import math

a = 1234567
i = len(str(a))
number = 0
while a > 0:
    digit = a % 10
    number = digit * (10 ** i) + number
    a = a // 10
    i = i - 1;
print(number)
Wilson.F
  • 39
  • 3