Using floor division operator (//)
The // operator in Python performs floor division, which always rounds down to the nearest integer, regardless of the sign of the number. This is equivalent to math.floor() for both positive and negative numbers.
num = 5.7
floor_val = num // 1 # Output: 5.0
num = -5.7
floor_val = num // 1 # Output: -6.0 (correct floor behavior)Using int() with adjustment for negative numbers
The int() function truncates toward zero, which matches floor only for positive numbers. For negative numbers, it does not behave like math.floor(). To fix this, subtract 1 if the number is negative and not an integer:
def floor_without_math(x):
if x == int(x) or x > 0:
return int(x)
else:
return int(x) - 1
# Example usage
print(floor_without_math(5.7)) # Output: 5
print(floor_without_math(-5.7)) # Output: -6Key takeaway:
x // 1is the most reliable and direct alternative tomath.floor(x)without importing the math module.int(x)alone is insufficient for negative numbers; it must be adjusted for correct floor behavior.
>>> 3/2
1.5
>>> 3//2 # floor
1
>>> -(-3//2) # ceil
2
Answer from bakar on Stack Overflow>>> 3/2
1.5
>>> 3//2 # floor
1
>>> -(-3//2) # ceil
2
Try
def ceil(n):
return int(-1 * n // 1 * -1)
def floor(n):
return int(n // 1)
I used int() to make the values integer. As ceiling and floor are a type of rounding, I thought integer is the appropriate type to return.
The integer division //, goes to the next whole number to the left on the number line. Therefore by using -1, I switch the direction around to get the ceiling, then use another * -1 to return to the original sign. The math is done from left to right.
Videos
I'm just starting to learn and I wanted to try out the floor() function but it just keeps giving me "NameError: name 'floor' is not defined. Did you mean: 'float'?"
for example I'd tell it to floor(69.69696969) and it would just give me the error above.
Am I doing something wrong?
In python, I am tasked with finding the floor square root of a number without using any built-in operators or functions. I have no idea how to start this, since using a for loop can only check the integer roots.
ex.
in : 8 out : 2 exp: sqrt(8) = 2.82842..., but floor(2.82842..) = 2
As long as your numbers are positive, you can simply convert to an int to round down to the next integer:
>>> int(3.1415)
3
For negative integers, this will round up, though.
You can call int() on the float to cast to the lower int (not obviously the floor but more elegant)
int(3.745) #3
Alternatively call int on the floor result.
from math import floor
f1 = 3.1415
f2 = 3.7415
print floor(f1) # 3.0
print int(floor(f1)) # 3
print int(f1) # 3
print int(f2) # 3 (some people may expect 4 here)
print int(floor(f2)) # 3
http://docs.python.org/library/functions.html#int
If x is a float number that you want to round up to an integer, and you want an integer type result, you could use
rounded_up_x = int(-(-x // 1))
This works because integer division by one rounds down, but using the negative sign before and after doing the division rounds the opposite direction. The int here converts the float result to an integer. Remove that int if you want a floating point value that equals an integer, which is what some programming languages do.
Hat-tip to @D.LaRocque for pointing out that Python's ceil() function returns an integer type.
in Python 3, we have object.__ceil__() that is even called by math.ceil internally,
num = 12.4 / 3.3
print(num)
3.757575757575758
num.__ceil__()
4
Or one can always negate the result of a negated number's floor division (and create a new int object unless a float would do),
int(-(-12.4 // 3.3))
4