Modulus Question
Ben Davis
bnd25@cam.ac.uk
Thu Jun 12 02:09:00 GMT 2003
On Thursday 12 June 2003 2:38 am, Tang wrote:
> This printed result is -1. I think the result should be 9.
> I dont have any other gcc to test this. Is this a bug?
No. It's how modulo is implemented on 99% of hardware, and the general trend
in C is for stuff to use the raw hardware operations and therefore be fast.
I'm afraid you have to work around this annoyance yourself (yes, I find it
annoying too). You could add 10 or 20 or whatever will be sufficient before
computing a modulo, or test if it's negative and add the divisor on
afterwards if necessary. Try to avoid extra tests though, since these can
really slow a program down if you do a lot of them.
Incidentally, don't confuse modulo with modulus. The modulus is the absolute
value of a scalar, or the magnitude of a vector. % is the modulo operator.
Ben
More information about the Gcc-help
mailing list