RE: Generating Modulo and Remainder Operations

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

 



Hello John,

> The % operator is well-defined for unsigned integer numbers.  

This is news to me, but I've never used C/C++ for any kind of modular
arithmetic which would involve negatives. I shall have to be wary if I do
:-)

> It is
> platform dependent when dealing with negatively signed integer numbers.

Yikes, that sucks.

> Rather depends on if you want Knuth modulus, or Euclidean modulus.

Never heard of Knuth modulus, but there again I don't know Knuth's
works/ideas much beyond the TeX Book.  Also, presumably by Euclidean modulus
you mean the Gaussian modulus, such as a = b (mod c) is equivalent to the
statement that c|(a-b) (where = is equivalent [i.e. three equal marks] and |
means 'divides without remainder').

> int r = abs(n) % abs(d);
> return ((n < 0) != (d < 0)) ? ((abs(d)-r)%abs(d)) : r;

I see the intention.

> There is probably a more efficient way of doing this same thing in
> Hacker's Delight by Henry S. Warren
> <http://www.amazon.com/dp/0201914654>.  I don't have it at my
> fingertips right now.

Thanks - added to my wishlist - one for a rainy day.

> Or you can use the Standard C Library modf() function.

So, you're saying modf() will work for signed and unsigned, floating and
integer types?

Thanks,
David.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux