+ 6
For this one: print(1.0//.1) # 9.0 ... expecting 10.0 Take the numbers one at a time at 21 float precision digits: print("{:.21f}".format(1.0)) print("{:.21f}".format(0.1)) 1.00000000000000000000 # fine 0.10000000000000000551 # whoops You can mentally multiply both by 10 so it's: 10.0/1.00...551 = 9 dot something Two slashes // is floor division and always goes down (I've substituted the Python magic method here): print("{:.21f}".format(1.0.__floordiv__(0.1))) == 9 and change....down 9.0 To clarify "down" as less, try math.floor(-7.1): -8.0 So your negatives, in your other examples: (-1.0//.1 = -10) like: -10.0/1.000...551 -9.something... down is -10.0 (-1.1//.1 = -12) -1.10000000000000008881... -11 and change 0.10000000000000000551... 1 and less change -11 and any change floors down: -12.0
25th Sep 2017, 4:07 AM
Kirk Schafer
Kirk Schafer - avatar