J
Joe Wright
Does anybody know why div() was implemented?
Does anybody know why div() was implemented?
Does anybody know why div() was implemented?
Joe Wright said:Does anybody know why div() was implemented?
The comment atPeter said:No, but other posts notwithstanding, it's worth noting that
the division and remainder returned by div need not be the
same as that returned by / and % under C89 (according to
the draft I have which may have changed).
Richard Heathfield said:Joe Wright said:
Those who have implemented their own arbitrary precision
arithmetic library will be particularly aware of the fact that performing
a division gives you a remainder for free. So if you need both the
result of a division *and* a remainder, instead of writing w = x / y;
r = x % y; on at least some machines you could have them both for
the cost of only one... if only you had the syntax for it. The div
function provides that syntax. (Under the hood, doubtless it will do
something nastily machine-specific.)
Any decent compiler should be able to notice you're dividing and modding
the same value by the same other value and combine the expressions into
one operation at a lower level...
Joe said:Does anybody know why div() was implemented?
Stephen said:Any decent compiler should be able to notice you're dividing and
modding the same value by the same other value and combine the
expressions into one operation at a lower level...
[/QUOTE]Any decent compiler should be able to notice you're dividing and
modding the same value by the same other value and combine the
expressions into one operation at a lower level...
Why do people assume that such 'decent' compilers are always
available? Many machines operate with a minimal compiler, no
optimization, no this, no that. The code functions.
Richard said:Joe Wright said:
Those who have implemented their own arbitrary precision arithmetic library
will be particularly aware of the fact that performing a division gives
you a remainder for free. So if you need both the result of a division
*and* a remainder, instead of writing w = x / y; r = x % y; on at least
some machines you could have them both for the cost of only one... if only
you had the syntax for it. The div function provides that syntax. (Under
the hood, doubtless it will do something nastily machine-specific.)
On my kit (DJGPP circa 2002) I get..Eric said:Another reason is that the original ANSI C Standard gave
the implementation some freedom when negative numbers appeared
in divisions. For example, either of
-5 / 3 == -2, -5 % 3 == 1
or
-5 / 3 == -1, -5 % 3 == -2
was allowed. (Note that the two pieces of each pair agree:
3*(-2) + 1 == -5 in the first, and 3*(-1) + (-2) == -5 in
the second.) This implementation-specific variation could be
troublesome in some circumstances, so div() and ldiv() were
defined to produce results according to the second scheme, even
if the underlying machine's hardware used the first.
The 1999 edition of the Standard took away the freedom of
choice for / and %, requiring them to use the second method,
so that part of the reason for div() and ldiv() disappeared.
But it's still relevant for programs compiled under the older
rules -- in theory, anyhow, because the second scheme was
already the Law for other important programming languages, and
therefore most machines implemented the second scheme anyhow.
Joe said:On my kit (DJGPP circa 2002) I get..
num = 40, den = -3, quot -13, rem -1, d*q+r 38
from the div() function and..
num = 40, den = -3, quot -13, rem 1, d*q+r 40
when I do it myself. It would seem the div() version is simply wrong.
The comment at
http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/div.c#49
is interesting.
Chris Torek said:I recognize that comment....
Now that I actually look at it in detail, I agree!
You cater for rounding to -Inf (floored division), but
I honestly can't recall ever seeing that in a machine
instruction. [Perhaps ones that use floating point to
support integers?]
Is your interpretation of C89 that this is the only
other kind of rounding supported?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.