Why is there 'div()' in the standard lib

Discussion in 'C Programming' started by Joe Wright, Nov 28, 2007.

  1. Joe Wright

    Joe Wright Guest

    Does anybody know why div() was implemented?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Nov 28, 2007
    #1
    1. Advertising

  2. Joe Wright

    user923005 Guest

    On Nov 27, 6:03 pm, Joe Wright <> wrote:
    > Does anybody know why div() was implemented?


    It does seem a tempest in a teapot to get simultaneous div and mod
    results, but I guess they wanted a portable way to do that and figured
    that the library implementor would be able to do it faster than
    average.

    It turns out to be useful for things like time calculations (e.g.
    convert a bushel of seconds into year/month/day/hour/min/sec).
    user923005, Nov 28, 2007
    #2
    1. Advertising

  3. Joe Wright said:

    > Does anybody know why div() was implemented?


    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.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 28, 2007
    #3
  4. Joe Wright <> wrote:
    > Does anybody know why div() was implemented?


    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).

    --
    Peter
    Peter Nilsson, Nov 28, 2007
    #4
  5. Joe Wright

    Ian Collins Guest

    Peter Nilsson wrote:
    > Joe Wright <> wrote:
    >> Does anybody know why div() was implemented?

    >
    > 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).
    >

    The comment at

    http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/div.c#49

    is interesting.

    --
    Ian Collins.
    Ian Collins, Nov 28, 2007
    #5
  6. "Richard Heathfield" <> wrote in message
    news:...
    > Joe Wright said:
    >> Does anybody know why div() was implemented?

    >
    > 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...

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Nov 28, 2007
    #6
  7. Stephen Sprunk said:

    <snip>

    > 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...


    What constitutes a "decent compiler" has perhaps moved on somewhat from
    whenever it was that div() was introduced.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Nov 28, 2007
    #7
  8. Joe Wright

    CBFalconer Guest

    Joe Wright wrote:
    >
    > Does anybody know why div() was implemented?


    Because it returns both the quotient and the remainder with one
    relatively long division instruction.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 28, 2007
    #8
  9. Joe Wright

    CBFalconer Guest

    Stephen Sprunk wrote:
    > "Richard Heathfield" <> wrote:
    >> Joe Wright said:
    >>
    >>> Does anybody know why div() was implemented?

    >>
    >> 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...


    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.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 28, 2007
    #9
  10. In article <>,
    CBFalconer <> wrote:

    >> 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.


    If you have a compiler with no optimisation, ensuring that you don't
    do unnecessary divisions is likely to be the least of your problems.

    -- Richard

    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Nov 28, 2007
    #10
  11. Joe Wright

    Eric Sosman Guest

    Richard Heathfield wrote:
    > Joe Wright said:
    >
    >> Does anybody know why div() was implemented?

    >
    > 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.)


    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.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 28, 2007
    #11
  12. Joe Wright

    Stephen Howe Guest

    > Does anybody know why div() was implemented?

    I like it and wish there was a udiv(), uldiv() and ulldiv()

    Stephen Howe
    Stephen Howe, Nov 28, 2007
    #12
  13. Joe Wright

    Joe Wright Guest

    Eric Sosman wrote:
    > Richard Heathfield wrote:
    >> Joe Wright said:
    >>
    >>> Does anybody know why div() was implemented?

    >>
    >> 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.)

    >
    > 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.
    >

    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.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Nov 29, 2007
    #13
  14. Joe Wright

    Eric Sosman Guest

    Joe Wright wrote:
    >
    > 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.


    It looks like DJGPP's div() has (had?) a bug. The quotient
    is right (40/-3 = -13.333... -> -13), but the remainder is wrong.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 29, 2007
    #14
  15. Joe Wright

    Chris Torek Guest

    In article <>
    Ian Collins <> wrote:
    >The comment at
    >http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/div.c#49
    >is interesting.


    I recognize that comment.... :)

    (I wrote that comment, and in fact, that entire file, except for
    the Sun copyright at the top, which was not there in the original
    BSD version, nor the two unnecessary and nonportable "#include"s.
    Also, the "ident" was a static array initializer, not a "#pragma";
    was inside a #ifndef lint; had slightly different text; and, of
    course, the SCCSID key"words" were expanded when I checked it in
    at Berkeley.)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Dec 2, 2007
    #15
  16. > Ian Collins <> wrote:
    > > The comment at
    > > http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/lib...
    > > is interesting.


    Now that I actually look at it in detail, I agree!

    Chris Torek <> wrote:
    >
    > I recognize that comment.... :)


    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?

    I always took the C89 (draft) as saying that division
    involving one or more negative operands could result
    in rounding up or down. That leads to 8 different forms
    of division, if you exclude the possibility of mixed
    runtime behaviour, i.e. sometimes round up, sometimes
    round down.

    In mathematics, I've certainly seen what I call
    mathematical or modulo rounding wherein the remainder
    is always positive, i.e. 0 <= |r| < |d|. But apart from
    C89, I can't recall seeing an 'ordinary' computer
    language that offered any direct support for it.

    num den / zero -inf math
    7 5 1.4 +1 r +2 +1 r +2 +1 r +2
    7 -5 -1.4 -1 r +2 -2 r -3 -1 r +2
    -7 5 -1.4 -1 r -2 -2 r +3 -2 r +3
    -7 -5 1.4 1 r -2 +1 r -2 +2 r +3

    I've certainly used and written maths applications that
    offered the math form of rounding.

    --
    Peter
    Peter Nilsson, Dec 3, 2007
    #16
  17. Joe Wright

    Chris Torek Guest

    >> Ian Collins <> wrote:
    >>> The comment at
    >>> http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/lib...
    >>> is interesting.

    >

    In article <>,
    Peter Nilsson <> wrote:
    >Now that I actually look at it in detail, I agree!


    >Chris Torek <> wrote:
    >> I recognize that comment.... :)


    >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?]


    I recall Fortran allowing only two kinds of integer division:
    round towards 0 (a la C99) and round towards -Inf (C89 and C99
    both). As far as I know, this was because at least one machine
    actually did the latter, but I have no idea which machine.

    >Is your interpretation of C89 that this is the only
    >other kind of rounding supported?


    That was my impression, yes. Could be wrong. :) My C89 standard
    is not handy (I think it is in a box but I have not seen it in
    years now). C99 requires the truncation-towards-zero behavior,
    which would simplify the implementation of div() quite a bit.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Dec 29, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
  2. K Viltersten

    <div ... /> and <div ...></div>

    K Viltersten, Mar 30, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    728
  3. Thai Le
    Replies:
    3
    Views:
    92
    Paulo Köch
    Feb 8, 2007
  4. Dwayne Madsen

    Q: Div A inside Div B is larger than Div B

    Dwayne Madsen, Jun 1, 2005, in forum: Javascript
    Replies:
    1
    Views:
    179
    David Dorward
    Jun 1, 2005
  5. mscir
    Replies:
    3
    Views:
    299
    Martin Honnen
    Jun 26, 2005
Loading...

Share This Page