Strange separation leads to python 3

I think there is some inconsistency in the division operation, but I'm not sure.

In the following code, I would expect either // c to be 100.0, or b // c to be -99.0.

a = 1.0 b = -1.0 c = 0.01 print (a/c) print (a//c) print (b/c) print (b//c) 

gives:

 100.0 99.0 -100.0 -100.0 

thanks

+7
division
source share
1 answer

This is because floating point numbers are represented . It is not true that 1.0 exactly 100 times 0.01 (since floating points are represented internally). The // operator performs division and aligns the result, so it may be that the internal number is slightly less than 100.0 , and this will cause it to be overlapped to 99.0 .

In addition, Python 3.x takes a different approach to show you a floating point number compared to Python 2.x. This means that the result of 1.0 / 0.01 , although internally slightly less than 100.0 , will appear to you as 100.0 , because the algorithm determined that the number was close enough so that 100.0 was considered equal to 100.0 . This is why 1.0 / 0.01 shown to you as 100.0 , although it cannot be represented internally how exactly this number is.

+3
source share

All Articles