🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2749

Re: div() & ldiv() behavior?

#2749
From: Stan Brown
Date: Mon, 15 Apr 1991 20:49
28 lines
1754 bytes
In article <10989@ncar.ucar.edu>, steve@unidata.ucar.edu (Steve Emmerson) writes:
> Would someone who knows please tell me the behavior of div() and ldiv()?
> 
> I know what this means when both arguments are positive, but I'm uncertain
> what this means for the other three cases.  I you would tell me, I would

Sec 4.10.6.2 of the standard says that div(int numer, int denom)
"computes the quotient and  remainder of the division of the numerator
numer by the denominator denom.  If the division is inexact, the
resulting quotient is the integer of lesser magnitude that is nearest to
the algebraic quotient.  If the result [can] be represented, ... 
quot * denom + rem shall equal numer."

This means, as I read it, that the quotient is always truncated toward
zero.  Thus,
	div(8,3)   gives quot=2,  rem=2
	div(8,-3)  gives quot=-2, rem=2
	div(-8,3)  gives quot=-2, rem=-2
	div(-8,-3) gives quot=2,  rem=-2
There's a similar table in sec 4.10.6.2 of the rationale, with 7
substituted for 8 and 1's or -1's for the remainders.

BTW, note that this is may be different from the result of 8/(-3) etc.
Sec 3.3.5 says the result can be -2 or -3 (implementation-defined).

Stan Brown, Oak Road Systems, Cleveland, Ohio, USA    +1 216 371 0043
                   email (until 91/4/30): browns@iccgcc.decnet.ab.com
My opinions are mine:  I don't speak for any other person or company.
Technical Details
Message-ID: <4273.2809cb7b@iccgcc.decnet.ab.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!abvax!iccgcc!browns
References: <10989@ncar.ucar.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!abvax!iccgcc!browns
From: browns@iccgcc.decnet.ab.com (Stan Brown)
Newsgroups: comp.std.c
Subject: Re: div() & ldiv() behavior?
Message-ID: <4273.2809cb7b@iccgcc.decnet.ab.com>
Date: 15 Apr 91 20:49:15 GMT
References: <10989@ncar.ucar.edu>
Distribution: na
Lines: 28