TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow
 Sign up for e-mail newsletters from DevX

Language: Web
Expertise: All
Apr 18, 2000

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# A Smart Ceiling for a Ratio of Two Integers

I often use the following trick in my code.

Let "a" and "b" be real positive numbers. You want to find the smallest integer greater than or equal to "a/b". Perhaps your code looks like:
``````
#include <math.h>
double a, b;
long c;
c = (long) ceil( a / b );
``````
Now, the question remains the same, but your numbers "a" and "b" happen to be integers. You can write:
``````
#include <math.h>
long a, b, c;
c = (long) ceil( (double)a/(double)b );
``````
However, there is a more elegant and efficient solution:
``````
long a, b, c;
c = (a - 1) / b + 1;
``````
Eugene Zak

 Submit a Tip Browse "Application Design" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date