EdwardB
EdwardB

Reputation: 83

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

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?

Upvotes: 8

Views: 19973

Answers (8)

Chen Hock Yew
Chen Hock Yew

Reputation: 1

Here's another way to do it though it may not work in languages where 0^0 does not equal 1.

def roundup(num): 
  integer = num//1 #Get number without the decimal places
  deci = num%1 #Get the decimal places
  rounding = deci**(0**deci) #x^(0^x), where x is the decimal value
  result = integer + rounding #add the rounding to the integer of the number
  return result

Simplified:

def roundup(num): 
  result = num//1 + (num%1)**(0**(num%1))
  return result 

Test Cases:

roundup(5.09)
6.0
roundup(4)
4
roundup(-8.7)
-8.0

Upvotes: 0

user20314038
user20314038

Reputation:

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

Upvotes: 0

Nikhil Saxena
Nikhil Saxena

Reputation: 31

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

Upvotes: 1

Krishna Rao
Krishna Rao

Reputation: 99

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)

Upvotes: -1

H&#229;ken Lid
H&#229;ken Lid

Reputation: 23074

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)

Upvotes: 1

Rory Daulton
Rory Daulton

Reputation: 22564

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.

Upvotes: 15

pault
pault

Reputation: 43544

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]

Upvotes: 1

Parthik B.
Parthik B.

Reputation: 502

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.

Upvotes: -4

Related Questions