Re: MATH_MOD: Include in GLib?



Hi,

On Mon, 26 Jul 2004 12:41:47 +0200, Sven Neumann wrote:
> Jan Kratochvil <lace jankratochvil net> writes:
> > On Mon, 26 Jul 2004 10:43:21 +0200, Sven Neumann wrote:
> > > Daniel Brockman <drlion deepwood net> writes:
> > ...
> > > > #define MOD(x, m) ((x) >= 0 ? (x) % (m) : (m) + (x) % (m))
...
> The result of "x%m" will be 0..(m-1) provided that you respect the
> fact that the modulo operator is undefined on negative operands. The
> behaviour is machine-dependent and you should simply avoid to use
> modulo with signed variables.

I was not aware the result is undefined, I still consider it a bug.


> Also note that your suggest macro won't work if x < m.

Thanks for notice, I did not check the macro (it was not mine...):

#define G_MOD(x, m) ((x) >= 0 ? (x) % (m) : (m) - 1 - ((-(x) - 1) % (m)))



Regards,
Lace

-- 
Jan Kratochvil; Captive: free r/w NTFS Filesystem; http://www.jankratochvil.net/



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