Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between floor function and floor division

Why is the answer not same for the below operations, and also, since // is essentially floor division, then why is the output different when a floor function is used.

I ran the following code:

import math
x = 2**64 -1
print("Original value:", x)
print("Floor division:", x//1)
print("Floor function:", math.floor(x/1))
print("Trunc function:", math.trunc(x/1))
print("Type conversion:", int((x/1)))

Output:

Original value: 18446744073709551615
Floor division: 18446744073709551615
Floor function: 18446744073709551616
Trunc function: 18446744073709551616
Type conversion: 18446744073709551616

Now, why is the answer not equal to the original value since all i did was divide by 1?

like image 849
anantdark Avatar asked Apr 08 '26 04:04

anantdark


2 Answers

float is a 64 bit IEEE-754 binary floating point value; it only has 53 bits of integer precision (beyond which it's stuck with imprecise approximations based on multiplying an integer value by a power of 2), and you put a 64 bit value in there when you divided by 1 (which coerced to a float result before the eventual call to round/trunc). Basically, it made the float value as close to the int you used as possible, which was unfortunately not equal to the int (because that's impossible), then rounded/truncated it (which, given the value had no decimal component, just meant converting back to the equivalent int value).

Floor division with // never has the problem, because it's a purely int-based division (nothing is ever represented as a float), and ints are (to the limits of computer memory) effectively infinite precision.

like image 99
ShadowRanger Avatar answered Apr 10 '26 16:04

ShadowRanger


/ converts to float. And float arithmetic has inherent numerical errors. As soon as you input x/1 you potentially introduce errors on the original value of x. Once the error is there you basically can't recover it.

This is broader than just an integer flooring issue:

x = 2**70 + 123
print(x-int(x/1))

123
like image 34
Julien Avatar answered Apr 10 '26 18:04

Julien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!