Integer division

Shack Toms jst at abic.UUCP
Fri Feb 14 02:02:54 AEST 1986


> In article <731 at abic.UUCP> jst at abic.UUCP (Shack Toms) writes:
> >However:  One might use a%b<0 iff a<0 in an algorithm which printed
> >the value of an integer in a given radix.  The least significant
> >digit of a in radix b would then be |a%b|.  :-)
> 
> So would |a|%b, and it works under either convention.  :-)

Except that |a| is not available for the full range of a.  In
particular, on a 16 bit computer |-32768| is not expressible.

The real point [of the :-)] is that it is just as easy to
correct the result of a%b in the range [0..b) as it is to 
perform the absolute value function.  That is:

    result = a%b;
    if (result < 0) result = -result;

is no easier than

    result = a%b;
    if (a < 0) result = b - result;

Shack Toms



More information about the Comp.lang.c mailing list