<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
Only lately with C99.<BR>
Prior to that it was unspecified.<BR>
 <BR>
 - Jay<BR><BR> <BR>
<HR id=stopSpelling>
From: hosking@cs.purdue.edu<BR>Date: Wed, 20 Jan 2010 05:09:33 -0500<BR>To: jay.krell@cornell.edu<BR>CC: m3devel@elegosoft.com<BR>Subject: Re: [M3devel] integer division/mod questions?<BR><BR>C specifies truncated division.
<DIV><BR>
<DIV><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span>
<DIV style="WORD-WRAP: break-word"><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span>
<DIV><FONT class=ecxApple-style-span color=#0000ff><FONT class=ecxApple-style-span face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span>Antony Hosking</SPAN></SPAN></FONT></FONT><FONT class=ecxApple-style-span face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN class=ecxApple-converted-space> </SPAN>|<SPAN class=ecxApple-converted-space> </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span>Associate Professor</SPAN></SPAN><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span> | Computer Science | Purdue University</SPAN></SPAN></FONT></DIV>
<DIV><FONT class=ecxApple-style-span face=GillSans-Light><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span>305 N. University Street | West Lafayette | IN 47907 | USA</SPAN></FONT></DIV>
<DIV><FONT class=ecxApple-style-span color=#0000ff face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span>Office</SPAN></SPAN></FONT><FONT class=ecxApple-style-span face=GillSans-Light><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span> <?XML:NAMESPACE PREFIX = SKYPE /><SKYPE:SPAN id=softomate_v3_highlight_0 class=skype_v3_tb_injection title=Call this phone number in United States of America with Skype: +17654946001 skypeid="0" skypeaction="call" nof="" isfax="" freecall="" fwidth=".w16" type=".flex" mode=".compat" path="file://C:/Users/jay/Temp/__SkypeIEToolbar_Cache/e70d95847a8f5723cfca6b3fd9946506/static/" durex2="%DADDYHEIGHT%" durex="%DADDYWIDTH%" context="+1 765 494 6001"><SKYPE:SPAN style="BACKGROUND-IMAGE: url(file://C:/Users/jay/Temp/__SkypeIEToolbar_Cache/e70d95847a8f5723cfca6b3fd9946506/static/inactive_a.compat.flex.w16.gif)" id=skype_v3_tb_droppart_0 class=skype_v3_tb_imgA title="Skype actions" skypeid="0" skypeaction="drop" skypesms="0"><SKYPE:SPAN style="BACKGROUND-IMAGE: url(file://C:/Users/jay/Temp/__SkypeIEToolbar_Cache/e70d95847a8f5723cfca6b3fd9946506/static/famfamfam/US.gif)" id=skype_v3_tb_img_f0 class=skype_v3_tb_imgFlag></SKYPE:SPAN></SKYPE:SPAN><SKYPE:SPAN id=skype_v3_tb_img_s0 class=skype_v3_tb_imgS></SKYPE:SPAN><SKYPE:SPAN id=skype_v3_tb_text0 class=skype_v3_tb_injectionIn><SKYPE:SPAN id=skype_v3_tb_innerText0 class=skype_v3_tb_innerText> +1 765 494 6001 </SKYPE:SPAN></SKYPE:SPAN><SKYPE:SPAN id=skype_v3_tb_img_r0 class=skype_v3_tb_imgR></SKYPE:SPAN></SKYPE:SPAN> <SKYPE:SPAN id=softomate_v3_print_0 class=skype_v3_tb_injection_print context="">+1 765 494 6001</SKYPE:SPAN>  |<SPAN class=ecxApple-converted-space> </SPAN></SPAN></SPAN></FONT><FONT class=ecxApple-style-span color=#0000ff face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span>Mobile</SPAN></SPAN></FONT><FONT class=ecxApple-style-span face=GillSans-Light><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span><SPAN class=ecxApple-converted-space> </SPAN><SKYPE:SPAN id=softomate_v3_highlight_1 class=skype_v3_tb_injection title=Call this phone number in United States of America with Skype: +17654275484 skypeid="1" skypeaction="call" nof="" isfax="" freecall="" fwidth=".w16" type=".flex" mode=".compat" path="file://C:/Users/jay/Temp/__SkypeIEToolbar_Cache/e70d95847a8f5723cfca6b3fd9946506/static/" durex2="%DADDYHEIGHT%" durex="%DADDYWIDTH%" context="+1 765 427 5484"><SKYPE:SPAN style="BACKGROUND-IMAGE: url(file://C:/Users/jay/Temp/__SkypeIEToolbar_Cache/e70d95847a8f5723cfca6b3fd9946506/static/inactive_a.compat.flex.w16.gif)" id=skype_v3_tb_droppart_1 class=skype_v3_tb_imgA title="Skype actions" skypeid="1" skypeaction="drop" skypesms="1"><SKYPE:SPAN style="BACKGROUND-IMAGE: url(file://C:/Users/jay/Temp/__SkypeIEToolbar_Cache/e70d95847a8f5723cfca6b3fd9946506/static/famfamfam/US.gif)" id=skype_v3_tb_img_f1 class=skype_v3_tb_imgFlag></SKYPE:SPAN></SKYPE:SPAN><SKYPE:SPAN id=skype_v3_tb_img_s1 class=skype_v3_tb_imgS></SKYPE:SPAN><SKYPE:SPAN id=skype_v3_tb_text1 class=skype_v3_tb_injectionIn><SKYPE:SPAN id=skype_v3_tb_innerText1 class=skype_v3_tb_innerText> +1 765 427 5484 </SKYPE:SPAN></SKYPE:SPAN><SKYPE:SPAN id=skype_v3_tb_img_r1 class=skype_v3_tb_imgR></SKYPE:SPAN></SKYPE:SPAN> <SKYPE:SPAN id=softomate_v3_print_1 class=skype_v3_tb_injection_print context="">+1 765 427 5484</SKYPE:SPAN> </SPAN></SPAN></FONT></DIV>
<DIV><FONT class=ecxApple-style-span face=GillSans-Light><BR class=ecxkhtml-block-placeholder></FONT></DIV></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN><BR class=ecxApple-interchange-newline></SPAN></DIV></SPAN></SPAN><BR class=ecxApple-interchange-newline></DIV><BR>
<DIV>
<DIV>On 19 Jan 2010, at 21:59, Jay K wrote:</DIV><BR class=ecxApple-interchange-newline>
<BLOCKQUOTE>
<DIV><BR>That paper also suggests a more efficient algorithm we should probably adopt:<BR><BR><BR>/* Floored division */<BR>long divF( long D, long d )<BR>{<BR>long q = D/d;<BR>long r = D%d;<BR>if ((r> 0 && d < 0) || (r < 0 && d> 0)) q = q-1;<BR>return q;<BR>}<BR><BR><BR>long modF( long D, long d )<BR>{<BR>long r = D%d;<BR>if ((r> 0 && d < 0) || (r < 0 && d> 0)) r = r+d;<BR>return r;<BR>}<BR><BR><BR>Though the condition should be perhaps the equivalent (r < 0) != (d < 0)<BR>or (r < 0) ^ (d < 0)<BR><BR><BR>We'd probably want to be sure the / followed by % got optimized into just one divide though.<BR><BR><BR>or maybe what I have is fine.<BR>As long as the inputs are the same sign, the current code does just one / or one %. It is only for mixed sign inputs that you need more care.<BR>I am assuming C never "rounds", but either truncates or "floors".<BR>e.g. 1/2 in the face of rounding is 1.<BR><BR><BR>Integer division probably not a big concern anyway, esp. with any negative numbers. The main place I ever see division/mod used is hash tables, and the numbers are always positive (I rarely see negative numbers used anywhere in "systems" programming actually -- file sizes, array indices, array sizes..all positive; "math" needs floating point often...)<BR><BR><BR>- Jay<BR><BR><BR>----------------------------------------<BR>
<BLOCKQUOTE>From: <A href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</A><BR></BLOCKQUOTE>
<BLOCKQUOTE>To: <A href="mailto:rodney_bates@lcwb.coop">rodney_bates@lcwb.coop</A>; <A href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</A><BR></BLOCKQUOTE>
<BLOCKQUOTE>Date: Wed, 20 Jan 2010 02:45:43 +0000<BR></BLOCKQUOTE>
<BLOCKQUOTE>Subject: Re: [M3devel] integer division/mod questions?<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>There is a positive zero and a negative zero, and which one you get<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>Rodney you reminded me of something I forgot to ask:<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>Is 0 div -1 = 0 or -1?<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>In particular, in floating point, 0 / -1 is presumably -0 and 0 / 1 is presumably +0. Modula-3 is specified as returning the largest integer not greater than the floating point result. So the question then is, is 0> -0? Or are they equal? Integer-wise, there is no -0.<BR></BLOCKQUOTE>
<BLOCKQUOTE>If 0> -0, then 0 div -1 should equal -1 instead of 0.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>My current code I believe returns 0 for 0 div anything.<BR></BLOCKQUOTE>
<BLOCKQUOTE>And I believe "the mod formula" holds as correct, since my testing checks that. I'll have to double double check.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>The previous code probably also but I'll have to double check.<BR></BLOCKQUOTE>
<BLOCKQUOTE>The previous code isn't necessarily the definition of correct though, since it was pretty clearly wrong for some cases.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>Which is imply the question: Everyone agrees with me that the previous code was wrong? In particular, for LONG_MIN div negative, it was giving negative results, but I believe the result should be positive. Mod had a similar problem.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>Nice if anyone can reproduce the problem with K&R + long long as well.<BR></BLOCKQUOTE>
<BLOCKQUOTE>I don't like making changes here without some kind of independent confirmation, though I'm also pretty sure of myself..a contradiction I realize.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>The previous code and I believe the current code are inconsistent as to when it would "trap", depending on compiler and optimization level. I assume that is ok for now, though in future we might want to make it predictable and honor the FloatMode stuff, but until that time, "unpredictable" is ok.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>In particular, LONG_MIN div or mod -1 can trap.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>div clearly has little choice. Mod I have to think about -- mod should be zero, right? LONG_MIN "evenly" divides by -1, the answer can't be represented, but the remainder can (it is zero). So maybe we should special case that to ensure it doesn't trap.<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>(anywhere I say "LONG_MIN", INT64_MIN has similar problem)<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>- Jay<BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE>
<BLOCKQUOTE>----------------------------------------<BR></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>Date: Tue, 19 Jan 2010 18:39:22 -0600<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>From: <A href="mailto:rodney_bates@lcwb.coop">rodney_bates@lcwb.coop</A><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>To: <A href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</A><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>Subject: Re: [M3devel] integer division/mod questions?<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><A href="mailto:hendrik@topoi.pooq.com">hendrik@topoi.pooq.com</A> wrote:<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>On Sun, Jan 17, 2010 at 10:44:53AM +0000, Jay K wrote:<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>-2147483648 div 2147483647 ?<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>-2147483648 mod 2147483647 ?<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>quotient = -1, remainer = -1 seems reasonable.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>2147483647 * -1 + -1 == -2147483648<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>There are two kinds of binary integer arithmetic -- two's complement<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>and one's complement. In my experience, two's complement machines tend<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>to have the remainder being the same sign as the dividend; one's<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>complement machines semm to have a remainder the same sign as the<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>divisor.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>One's complement machines are all but extinct these days.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>Tony, you were concerned about showing your age, but 20 years is but an<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>evening past. I remember programming ones-complement arithmetic<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>in assembly language, definitely more decades ago than two.<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>And that was not my first job.<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>There is a positive zero and a negative zero, and which one you get<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>can depend on the operation and operand values that produced the<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>result, so you have to check for both of them, often with two<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>separate conditional branches.<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>Anyone remember nines- or tens-complement arithmetic on decimal<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>machines? Electromechanical accounting machines?<BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>However, Modula-3 specifies div as rounding down, so<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>-2147483648 div 2147483647 == -2<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>and then<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><A href="http://www.modula3.com/cm3/doc/reference/arithmetic.html">http://www.modula3.com/cm3/doc/reference/arithmetic.html</A><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>The value x DIV y is the floor of<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>the quotient of x and y; that is, the maximum integer<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>not exceeding the real number z such that z * y = x.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>For integers x and y, the value of x MOD y is<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>defined to be x - y * (x DIV y).<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>This means that for positive y, the value of x MOD y<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>lies in the interval [0 .. y-1], regardless of<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>the sign of x. For negative y, the value of<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>x MOD y lies in the interval [y+1 .. 0], regardless<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>of the sign of x.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>And this is consistent with MOD producing a canonical representative of<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>an equivalence class modulo y. It's what's wanted for a lot of<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>algorithms. What it returns for negative y isn't as important, but<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>it is important to have positive MODuli for positive y no matter what<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>the sign of x is.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>But it's not what most two's-complement processors produce naturally.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>Having a MODulo operation that is mathematically well-behaved takes<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>extra effort on these machines.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>And it's also important to have a remainder that corresponds to the<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>division operator. On two's complement machines you have to either<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>* use extra instructions to correct the result of the divide<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>instruction to correspond to the mathematically desirable MOD<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>operator, or<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>* Have a separate remainder operation that does correspond to<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>division.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>On one's complement machines MOD will have the same effect as remainder.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>On two's complement, different.<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>-- hendrik<BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE><SPAN style="WHITE-SPACE: pre" class=ecxApple-tab-span></SPAN><SPAN style="WHITE-SPACE: pre" class=ecxApple-tab-span></SPAN><SPAN style="WHITE-SPACE: pre" class=ecxApple-tab-span></SPAN>  <SPAN style="WHITE-SPACE: pre" class=ecxApple-tab-span> </SPAN><SPAN style="WHITE-SPACE: pre" class=ecxApple-tab-span></SPAN> <BR></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></DIV></BLOCKQUOTE></DIV><BR></DIV>                                     </body>
</html>