Re: MATH_MOD: Include in GLib?

On Sun, 2004-07-25 at 23:39, Daniel Brockman wrote:
> Hi,
> I've been using a macro called MOD as a modulus function with
> different semantics than %.  Specifically, I need -1 mod 3 to equal 2.
> #define MOD(x, m) ((x) >= 0 ? (x) % (m) : (m) + (x) % (m))
> I haven't really seen this used anywhere else, although I assume that
> it is pretty common since it is needed for normalizing any wrapping
> values -- such as angles for which, e.g., -90° = 270°.
> My suggestion is that it be put in GLib right next to its more
> generally applicable fellows MIN, MAX, and CLAMP.  What do you think?

While I agree that this interpretation of modulo is frequently useful,
I'm not really sure it belongs in GLib. Code is probably going to
be clearer if the definition is next to the use.

Certainly calling it something like G_MOD() is not appropriate as much
as I'd agree that the C standard is screwed up in this choice. I'm
not sure what you would call it. You also usually want the equivalent
division at the same time:

 DIV(x, d) (((x) >= 0 ? (x) / (m) : ((x) - (m) + 1) / (m))

It's worth noting that powers of two can be optimized for this:

 MOD(x, 1024) = x >> 10
 DIV(x, 1024) = x & 1023

(One nice consequence of the change of PANGO_SCALE from 1000 to 1024)
Which is perhaps one reason not to have a macro.


Attachment: signature.asc
Description: This is a digitally signed message part

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]