Is There a Ceiling Equivalent of // Operator in Python

Is there a ceiling equivalent of // operator in Python?

There is no operator which divides with ceil. You need to import math and use math.ceil

How to perform ceiling-division in integer arithmetic?

For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division:

items = 102
boxsize = 10
num_boxes = (items + boxsize - 1) // boxsize

Alternatively, use negation to convert floor division to ceiling division:

num_boxes = -(items // -boxsize)

Ceil and floor equivalent in Python 3 without Math module?

>>> 3/2
1.5
>>> 3//2 # floor
1
>>> -(-3//2) # ceil
2

Excel-like ceiling function in python?

I don't know of any python function to do so, but you can easily code one :

import math

def ceil(x, s):
return s * math.ceil(float(x)/s)

The conversion to float is necessary in python 2 to avoid the integer division if both arguments are integers. You can also use from __future__ import division. This is not needed with python 3.

Why is there a difference between `0--3//2` and `--3//2`?

Python uses the symbol - as both a unary (-x) and a binary (x-y) operator. These have different operator precedence.

In specific, the ordering wrt // is:

  • unary -
  • binary //
  • binary -

By introducing a 0 as 0--3//2, the first - is a binary - and is applied last. Without a leading 0 as --3//2, both - are unary and applied together.

The corresponding evaluation/syntax tree is roughly like this, evaluating nodes at the bottom first to use them in the parent node:

 ---------------- ---------------- 
| --3//2 | 0--3//2 |
|================|================|
| | ------- |
| | | 0 - z | |
| | -----+- |
| | | |
| -------- | ----+--- |
| | x // y | | | x // y | |
| -+----+- | -+----+- |
| | | | | | |
| ----+ +-- | ---+ +-- |
| | --3 | | 2 | | | -3 | | 2 | |
| ----- --- | ---- --- |
---------------- ----------------

Because the unary - are applied together, they cancel out. In contrast, the unary and binary - are applied before and after the division, respectively.

How do you round UP a number?

The math.ceil (ceiling) function returns the smallest integer higher or equal to x.

For Python 3:

import math
print(math.ceil(4.2))

For Python 2:

import math
print(int(math.ceil(4.2)))

Why does Python 2.7 report incorrect ceiling value?

It's because the division operator / is integer division in Python 2.7. Note that the function arguments are fully evaluated before the function call, so math.ceil was actually an innocent bystander here.

math.ceil(5/2)  # same as math.ceil(2)!

To get the Python 3 behaviour in 2.7, use a future statement:

from __future__ import division

Floor or ceiling of a pandas series in python?

You can use NumPy's built in methods to do this: np.ceil(series) or np.floor(series).

Both return a Series object (not an array) so the index information is preserved.

Why do Python's math.ceil() and math.floor() operations return floats instead of integers?

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.



Related Topics



Leave a reply



Submit