Home > database >  python3 is generating two different results using division and floor division
python3 is generating two different results using division and floor division

Time:11-20

When using the normal division

9999999999999999 / 2

returned value: 5000000000000000.0

and when using the floor division

9999999999999999 // 2

returned value: 4999999999999999

I am using python 3.8.1 can anyone explain this ? Thanks in advance

CodePudding user response:

The / operator on int operands returns a float. Assuming an IEEE 754 compatible system, a float has 53 bits of precision. Thus, it cannot exactly represent the quotient 4999999999999999.5, which would require 54 bits (10001110000110111100100110111111000000111111111111111.1). So this value gets rounded up to 5000000000000000.0.

The // operator on int operands returns an int. The Python 3 int type has unlimited range, so can represent 9999999999999999 exactly. Halving this number gives 4999999999999999 (with a discarded remainder of 1).

  • Related