Skip to content Skip to sidebar Skip to footer

How To Perform Ceiling-division In Integer Arithmetic?

It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes. Is there a way to divide that

Solution 1:

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

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

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

num_boxes = -(items // -boxsize)

Solution 2:

Negate before and after?

>>>-(-102 // 10)
11

Solution 3:

from math import ceilprint(ceil(10.3))

11

Solution 4:

You can try :

import mathmath.ceil( x )

Post a Comment for "How To Perform Ceiling-division In Integer Arithmetic?"