5

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?

pault
  • 41,343
  • 15
  • 107
  • 149
EdwardB
  • 53
  • 1
  • 1
  • 3

7 Answers7

13

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.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • It looks like `math.ceil` returns `int` everytime, so we can use `int(- (-x // 1))` to avoid negative zeros. – D. LaRocque Jul 19 '19 at 16:10
  • @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. – Rory Daulton Jul 19 '19 at 16:15
  • 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. – EdwardB Jul 19 '19 at 17:05
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]
pault
  • 41,343
  • 15
  • 107
  • 149
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)
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
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
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
daeltro
  • 1
  • 1
-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)

  • 1
    This does not work for `0`, or whole numbers in general. `round(3+0.5) = 4` when the correct result should be `3` – pault Jul 19 '19 at 16:14
  • 1
    @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. – Rory Daulton Jul 19 '19 at 16:16
  • It may not work in python 2.7.x, but will work in python 3.x.x – Krishna Rao Jul 19 '19 at 16:17
  • @KrishnaRao it does not work for odd numbers in python 3.6.5 or 2.7 – pault Jul 19 '19 at 16:19
  • @pault 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. – Krishna Rao Jul 19 '19 at 16:27
-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.

Parthik B.
  • 472
  • 1
  • 5
  • 15