3

How to get he upper limit of a number in C?

If I divide 3 by 2 I want the answer to be 2 (ie 1.5 rounded to 2).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Eternal Learner
  • 3,800
  • 13
  • 48
  • 78
  • Essentially a duplicate of SO 2422712 (http://stackoverflow.com/questions/2422712/c-rounding-number-up). Granted that question mentions floating point, but the answers so far here do too. – Jonathan Leffler Mar 20 '10 at 04:37

8 Answers8

13

If you are just interested in dividing by 2, then just take (n + 1) / 2 to keep it in integer math. For example (3 + 1) / 2 gives 2. For a larger number x, use x - 1. For example, (3 + 7) / 8 = 1, for 3 divided by 8.

For the general case, you are looking for the ceiling function -- ceil. A quick Google search for "math ceil C" gave this page at the top of the results: http://www.elook.org/programming/c/ceil.html

Jim Flood
  • 8,144
  • 3
  • 36
  • 48
4

ceil(number)

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
3
#include <math.h>
ceil(3.0/2); //2.0

Note that one the operands should be double(or float) because 3/2 gives you 1

Lombo
  • 11,847
  • 2
  • 20
  • 27
2
int x= ceil((float)3/2);
aJ.
  • 34,624
  • 22
  • 86
  • 128
1

To round without using functions, just add half of the divisor before you divide. if the divisor is a constant, the compiler with optimize the code nicely.

int  number = 3;
int  divisor = 2;
int  result = (number + (divisor+1)/2) / divisor;
John Knoeller
  • 33,512
  • 4
  • 61
  • 92
1
int i = 3;
int j = 2;
int k = (i + j - 1) / j;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

(If this qualifies as thread necromancy, kindly notify and I'll delete this)

A quick way to return an upward-rounded quotient is to add the divisor, minus one, to the dividend, and divide.

int ceil_div(int dividend, int divisor) {
    return (dividend + divisor - 1) / divisor;
}

or alternatively,

int ceil_div(int dividend, int divisor) {
    return (dividend - 1) / divisor + 1;
}

Consequentially, you'll work by subtracting 1 from 3, dividing by 2, and adding 1.

I'll try and explain why this works. If dividend and divisor were both floats, then the truncated integer equivalent of (dividend/divisor) would be equal to the ceiling of the quotient if divisor perfectly divides dividend, or one less if it does not perfectly divide dividend. By subtracting 1, we guarantee that the new dividend, dividend - 1, when divided by divisor, will always return a value lesser than the ceiling of the floating point quotient.

All that remains now is adding 1 to this quotient, which, incidentally, is floor((float) dividend / (float) divisor).

susmits
  • 2,210
  • 2
  • 23
  • 27
  • An easier approach when dealing with positive numbers *in languages which implement integer division using floored or Euclidian arithmetic* is to negate the dividend and the result (i.e. x/y rounding up is -((-x) // y). Unfortunately, many languages implement the rather less useful truncate-toward-zero integer division, and this technique won't work with those. – supercat Nov 13 '13 at 21:25
0
int x = 3.0/2 + 0.5;
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • OP is asking for the ceiling function, not the round function. I too thought of this until I realized he *always* wants the next greatest integer regardless of th evalue. – Mahmoud Al-Qudsi Mar 20 '10 at 08:00
  • Ah. That wasn't entirely clear for me. He does use the word "rounded to.." :s – wilhelmtell Mar 20 '10 at 15:05