Rounding a math calculation up, without math.ceil()?
Asked Answered
C

7

6

I'm working on a system which doesn't have the math module available. All "Math" functions installed (math.ceil(), math.round(), etc all produce errors).

I have even tried using import math which yields:

<type 'ImportError'>
__import__ not found

Current issue that is stumping me: How can I make a math calculation round up to a whole number without math.ceil?

Calen answered 19/7, 2019 at 15:54 Comment(4)
check if x - int(x) is greater than 0? So something like int(x) + (1 if x - int(x) > 0 else 0)Worldly
How about int(x//1) + 1 ?Renshaw
@D.LaRocque that doesn't work if x is already an integer.Worldly
True, use x if isinstance(x, int) else int(x//1) + 1 or use the answer from @Rory DaultonRenshaw
J
14

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.

Jugendstil answered 19/7, 2019 at 15:56 Comment(3)
It looks like math.ceil returns int everytime, so we can use int(- (-x // 1)) to avoid negative zeros.Renshaw
@D.LaRocque: Many thanks, I have corrected my answer. I guess I was confused by C or Pascal, which I believe return an integer but in floating-point type.Jugendstil
oh, and what I didn't realize until the conversation about integer vs float... one of my numbers in the calculation was stored as an integer.Calen
W
1

The ceiling of x is the smallest integer greater than or equal to x. So just add 1 if the decimal part of x is non-zero.

One simply way would be:

def myCeil(x):
    return int(x) + int((x>0) and (x - int(x)) > 0)

Examples:

print([myCeil(i) for i in [myCeil(i) for i in [-2, -1.1, -0.0, 0, 1, 1.2, 3]])
#[-2, -1, 0, 0, 1, 2, 3]
Worldly answered 19/7, 2019 at 16:7 Comment(0)
F
1

Here's one way to do it. I think this should work in most versions of python.

def ceil(n):
    q, r = divmod(n, 1)
    return int(q) + bool(r)
Formality answered 19/7, 2019 at 16:22 Comment(0)
S
0

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
Staton answered 27/9, 2022 at 11:27 Comment(0)
N
0

This is an easy and intuitive way to do it if x isn't a negative number:

rounded = round(x)
if rounded < x:
    rounded_up_x = rounded + 1
else:
    rounded_up_x = rounded
Nashoma answered 23/10, 2022 at 11:20 Comment(0)
S
-1

it can be simply done by the following code (this is how I always do). No math library needed

y = x if x==x//1 else round(x+0.5)

Swiger answered 19/7, 2019 at 16:13 Comment(5)
This does not work for 0, or whole numbers in general. round(3+0.5) = 4 when the correct result should be 3Worldly
@pault: In my Python 3.7.3 it does work for zero and any even integer value but not for any odd integer value. This is due to round's banker's rounding, also called symmetric rounding--it rounds to the nearest even integer if the fractional part is 0.5.Jugendstil
It may not work in python 2.7.x, but will work in python 3.x.xSwiger
@KrishnaRao it does not work for odd numbers in python 3.6.5 or 2.7Worldly
@Worldly Right you are .. I have checked it now .. this is the old method I used in some of my previous tools ... but in python Rory 's code is good . Thanks for pointing it out.Swiger
L
-4

welcome to Stack.

As far as I've implemented in my codes, you don't need to import math to use round().

Because, round() is a standalone function in python and not an extension of math package.

So, I suggest you go on and simply use round() instead of math.round() and you'll be fine.

Refer this doc to find out more about how to use round() function.

Lutestring answered 19/7, 2019 at 16:5 Comment(2)
What about ceil? round() doesn't round upRenshaw
round() does not round up, which is what the questioner wants.Jugendstil

© 2022 - 2024 — McMap. All rights reserved.