0

I try to compute x = (2**83 + 1)/3 which is mathematically an integer and a float in python-x. In python 2, I get :

x = 2**83 + 1
x = 9671406556917033397649409L

then

y = x/3 = 3223802185639011132549803L

In python 3, I get :

x = 2**83 + 1
x = 9671406556917033397649409 --> OK

then

y = x/3 = 3.223802185639011e+24

To compare the 2 results, I use a format string instruction in python 3: z = '%25d' % y and I get z = '3223802185639010953592832' and z = '3223802185639011132549803' in python 2. (%i gives the same results, quite normal). The strange thing is that when I compute 3*Z, I get the good result in python2 and a wrong one in python3. I can't see where the problem is with my test (computing, formatting, ...). I'd like to use python 3 and to display x = (2**83 + 1)/3 with no 'e+24' but with all numbers.

Does anybody have an idea?

I have to add thet the problem remains the same when using / or // in python2. We get the same result sinc it is mathematically an integer. I should say that the problem is rather with python 3. How can i get the good result (the whole dispaly of (2**83 + 1)/3 in python 3) ?

Chichourne
  • 11
  • 4

1 Answers1

0

You seem to be looking for integer division as opposed to the floating point one.

/ operator returns a floating point number in Python3. To perform integer division in Python3, use //.

So, I guess all you need is

(2**83 + 1)//3 

which gives

3223802185639011132549803

instead of the (2**83 + 1)/3.

In Python2.7, both / and // are effectively the same unless you do something like from __future__ import division.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • Thank you but the problem remains the same when using / or // in python2. We get the same result sinc it is mathematically an integer. I should say that the problem is rather with python 3. How can i get the good result (the whole dispaly of (2**83 + 1)/3 in python 3) ? – Chichourne Jan 17 '19 at 09:37
  • @Chichourne What do you mean? I think using `//` instead of `/` would be enough. – J...S Jan 17 '19 at 09:38