Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why is there 'div()' in the standard lib

Reply
Thread Tools

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

 
 
Joe Wright
Guest
Posts: n/a
 
      11-28-2007
Does anybody know why div() was implemented?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      11-28-2007
On Nov 27, 6:03 pm, Joe Wright <(E-Mail Removed)> 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).
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-28-2007
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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      11-28-2007
Joe Wright <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-28-2007
Peter Nilsson wrote:
> Joe Wright <(E-Mail Removed)> 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/xr...t/gen/div.c#49

is interesting.

--
Ian Collins.
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      11-28-2007
"Richard Heathfield" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-28-2007
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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-28-2007
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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      11-28-2007
Stephen Sprunk wrote:
> "Richard Heathfield" <(E-Mail Removed)> 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

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      11-28-2007
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there any timing class in the standard lib? Thai Le Ruby 3 02-08-2007 02:18 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Need odbc32.lib odbccp32.lib Praetorian C++ 1 04-20-2006 07:14 PM
Diff betw common/lib and shared/lib in Tomcat James Yong Java 0 09-12-2005 02:36 AM



Advertisments