E
Eric Sosman
I need to get a ceil value from two integers
[...]
What is the "cleanest" but most compatible way? My program in this instance
always uses integers greater than zero and less than 128. Is there another
(speedier?) way other than using ceil()?
i = (i5 + i2 - 1) / i2;
Now there's a trick .... I learned it in a long-ago PPOE and think
it's kind of a clever hack.
It never struck me as either clever or hackish, but only as a
fairly obvious transformation. (Decades ago I thought it up myself,
so it can't be *that* abstruse!)
But I don't know that I've observed
anyone other than the long-ago colleague (and myself) using it.
Essentially the same thing is often done with multiples
of powers of two:
unsigned int nextEven = (value + 1u) & ~1u;
unsigned int nextSixteen = (value + 15u) & ~15u;
unsigned int nextK = (value + 1023u) & ~1023u;
// ... etc.
(`unsigned' because bit-twiddling's safer that way).
But apparently some others in this thread know about it, and also
know that it *can* run up against problems with overflow. "Hm!" ?
If it encounters overflow with "integers greater than zero
and less than 128" it's being run on a non-conforming or non-C
implementation.