>>> 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
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
No, but you can use upside-down floor division:¹
def ceildiv(a, b):
return -(a // -b)
This works because Python's division operator does floor division (unlike in C, where integer division truncates the fractional part).
Here's a demonstration:
>>> from __future__ import division # for Python 2.x compatibility
>>> import math
>>> def ceildiv(a, b):
... return -(a // -b)
...
>>> b = 3
>>> for a in range(-7, 8):
... q1 = math.ceil(a / b) # a/b is float division
... q2 = ceildiv(a, b)
... print("%2d/%d %2d %2d" % (a, b, q1, q2))
...
-7/3 -2 -2
-6/3 -2 -2
-5/3 -1 -1
-4/3 -1 -1
-3/3 -1 -1
-2/3 0 0
-1/3 0 0
0/3 0 0
1/3 1 1
2/3 1 1
3/3 1 1
4/3 2 2
5/3 2 2
6/3 2 2
7/3 3 3
Why this instead of math.ceil?
math.ceil(a / b) can quietly produce incorrect results, because it introduces floating-point error. For example:
>>> from __future__ import division # Python 2.x compat
>>> import math
>>> def ceildiv(a, b):
... return -(a // -b)
...
>>> x = 2**64
>>> y = 2**48
>>> ceildiv(x, y)
65536
>>> ceildiv(x + 1, y)
65537 # Correct
>>> math.ceil(x / y)
65536
>>> math.ceil((x + 1) / y)
65536 # Incorrect!
In general, it's considered good practice to avoid floating-point arithmetic altogether unless you specifically need it. Floating-point math has several tricky edge cases, which tends to introduce bugs if you're not paying close attention. It can also be computationally expensive on small/low-power devices that do not have a hardware FPU.
¹In a previous version of this answer, ceildiv was implemented as return -(-a // b) but it was changed to return -(a // -b) after commenters reported that the latter performs slightly better in benchmarks. That makes sense, because the dividend (a) is typically larger than the divisor (b). Since Python uses arbitrary-precision arithmetic to perform these calculations, computing the unary negation -a would almost always involve equal-or-more work than computing -b.
Solution 1: Convert floor to ceiling with negation
def ceiling_division(n, d):
return -(n // -d)
Reminiscent of the Penn & Teller levitation trick, this "turns the world upside down (with negation), uses plain floor division (where the ceiling and floor have been swapped), and then turns the world right-side up (with negation again)"
Solution 2: Let divmod() do the work
def ceiling_division(n, d):
q, r = divmod(n, d)
return q + bool(r)
The divmod() function gives (a // b, a % b) for integers (this may be less reliable with floats due to round-off error). The step with bool(r) adds one to the quotient whenever there is a non-zero remainder.
Solution 3: Adjust the numerator before the division
def ceiling_division(n, d):
return (n + d - 1) // d
Translate the numerator upwards so that floor division rounds down to the intended ceiling. Note, this only works for integers.
Solution 4: Convert to floats to use math.ceil()
def ceiling_division(n, d):
return math.ceil(n / d)
The math.ceil() code is easy to understand, but it converts from ints to floats and back. This isn't very fast and it may have rounding issues. Also, it relies on Python 3 semantics where "true division" produces a float and where the ceil() function returns an integer.
As pointed out by other answers, in python they return floats probably because of historical reasons to prevent overflow problems. However, they return integers in python 3.
>>> import math
>>> type(math.floor(3.1))
<class 'int'>
>>> type(math.ceil(3.1))
<class 'int'>
You can find more information in PEP 3141.
The range of floating point numbers usually exceeds the range of integers. By returning a floating point value, the functions can return a sensible value for input values that lie outside the representable range of integers.
Consider: If floor() returned an integer, what should floor(1.0e30) return?
Now, while Python's integers are now arbitrary precision, it wasn't always this way. The standard library functions are thin wrappers around the equivalent C library functions.
I'm building a calculator that allows users to input through a form with all the input types being number. I'm using flask to "connect" my html and python file together to allow values submitted from the form to be calculated and displayed via POST and GET methods. However, when I input a number of 154.456 it refuses to round up to 154.46 but instead gives out the follow error message:
"Please enter a valid value. The two nearest valid values are 154.45 and 154.46"
I've checked stackexchange, etc and the general advice is to change the step to "any" but I would still like to allow the input to be incremented by 0.01.
https://pastebin.com/4yJ5Fv9Y <--This is my python file and the function necessary for rounding up is
def getFormValue(form, nameOfInput, defaultValue): if (form.get(nameOfInput)): return math.ceil(float(form[nameOfInput])*100) / 100 else: return defaultValue
https://pastebin.com/Hr586BMb <-- This is my HTML file
Please help me fix this issue. Feel free to ask me for any extra information.
Simplest would be.
a//b + bool(a%b)
And just for safety,
b and (a//b + bool(a%b))
Cheers.
-(-a//b)
Perhaps the simplest?
========================
*Edited per @Gilles's comment:
for an integer n,
floor(x)=n for x in [n, n+1)
ceil(y)=n+1 for y in (n, n+1]
So, floor(-y)=-n-1 for -y in [-n-1, -n),
and ceil(y)=-floor(-y)=n+1 for y in (n, n+1]
In Python, floor(a/b) = a//b.
Thus ceil(a/b) = -(-a//b)