24th Jan 2023, 10:20 AM
Reniel Galang
Reniel Galang - avatar
5 Answers
+ 4
In programming there is a concept called flooring, which means rounding a decimal number to the smaller integer, which means that for example flooring value -4.1 would yield -5.0, which might surprise beginners who may think that floor division is same than integer division. In math module there is a function called floor: from math import floor which can be used to floor decimal numbers: floor(9.81) -> 9 floor(4.00) -> 4 floor(0.99) -> 0 ...continuing soon... finished. floor(-2.55) -> -3 floor(-8.00) -> -8 Python's floor division is *about* equal to flooring normal division: x//y == floor(x/y) and why I say about equal, is because x//y and floor(x/y) don't always produce number of same type. x//y will be float if either x and y is float, but floor(x/y) is always integer.
24th Jan 2023, 10:53 AM
Seb TheS
Seb TheS - avatar
+ 3
ceil points to positive infinity. floor points to negative infinity. That's why floor of a negative number becomes a bigger negative number. ceil ^ positive num v floor ---------0--------- zero ceil ^ negative num v floor
24th Jan 2023, 2:13 PM
Bob_Li
Bob_Li - avatar
+ 2
This operator will divide the first argument by the second and round the result down to the nearest whole number, -4 is lower than -3
24th Jan 2023, 10:44 AM
Shmuel Kroizer
+ 1
Thanks Shmuel Kroizer and Seb TheS . I learned something new today from you. thank you
24th Jan 2023, 11:03 AM
Reniel Galang
Reniel Galang - avatar
+ 1
CYBER-B Are you saying that -11 // 3 yields -4, because -3.66666... is closer to -4 than -3? Then why -10 // 9 yields -2?
26th Jan 2023, 7:35 AM
Seb TheS
Seb TheS - avatar