<table width="500"><tr><td><font size="" style="" color="">>It still seems wrong to me.  Which is right? -1 or -2?<br>>I guess it boils down to whether or not you believe<br>>that the modulus can be negative.<br>><br>>" -10 / 9<br>>" <br>>" For ruby, python and tcl you get: -2!<br>>" For java, c and gforth you get:   -1<br><br>This is pretty much off topic but the question seems to be if Ruby "does the<br>right thing or not" so I'll let it live a little longer.<br><br>Consider binary arithmetic and it makes sense.  When you have reached the<br>answer with as many whole bits as possible, remaining "non-whole bits" are<br>rounded off downwards.  The negative value is in two's complement format <br>so rounding the last bit downwards makes the result more negative than <br>the "real answer" would be.  <br><br>Contrast that to a positive number, where rounding a bit downward makes <br>the number smaller.<br><br>Coincidentally, it is also true for the relationship between<br>divisio!
n and "arithmetic shift right" which is usually considered to <br>be:  Shifting right n bits = dividing by 2^n.<br>If the rightmost bit is 1, i.e. if the number is odd, that bit disappears<br>off the edge and becomes a rounding error.  The result for a negative <br>number in 2's complement is then one less than you might expect.<br><br>For example (decimal) -3/2 is clearly -2, if implemented as -3 shifted <br>right one bit.  For a positive number: 3 shifted right one bit<br>becomes 1.  The extreme case ( (-1 / 2) / 2 ) / 2 is then still <br>-1 and so on.<br><br>Maybe it simply makes sense to define it that way because it fits so well<br>with the 2's complement representation.  Another idea - the<br>rule of always rounding down, also for two's complement numbers likely<br>produced the simplest logic for arithmetic circuits and therefore this has<br>lived on as the correct implementation for computer integer arithmetic<br><br>It makes sense for binary arithmetic, so the question!
 is perhaps if<br>"integer arithmetic", whatever that is,  should be defined this way simply<br>because of the choice of binary representation of numbers, or in some other,<br>arbitrarily chosen and later proven (read "mathematical" ;-) way.<br><br>Cheers<br>/Gunnar<br><br>---<br>Gunnar Andersson<br><br><br><br>_____________________________________________________________<br>Get Your Own .NU Web Address Now! ---> http://WWW.NUNAMES.NU<br></font></td></tr></table>