<div dir="ltr"><div id=":f" class=""><div id=":g" style="overflow:hidden"><div dir="ltr"><div><div><div><div><div><div>Hi Brad,<br><br></div>First of all, I would advocate modulo, not quotient.<br>Then at least it also works correctly for negative integers.<br>
<br></div>Secondly, I see the code out of context, but variable names like "weight" and "volume"<br>
to me suggest that they may be arbitrary (non-negative) real numbers.<br></div>And then modulo doesn't work.<br><br></div>So for posterity, if you know a and b are integers:<br></div> replace (floor (/ a b)) with (modulo a b)<br>
</div> replace (truncate (/ a b)) with (quotient a b)<div class=""><div id=":101" class="" tabindex="0"><img class="" src="https://mail.google.com/mail/u/0/images/cleardot.gif"></div></div></div></div></div><table class="">
<tbody><tr><td class=""><img id=":0_71" name=":0" src="https://mail.google.com/mail/c/u/0/photos/public/AIbEiAIAAABECNDG2Yau97_gqwEiC3ZjYXJkX3Bob3RvKihlNDliZDQ4MTAyNDNlZjRhYzJiOTY4NmI0NzVmYjY2NDAwZjMyMTJlMAH38Kp41SaF2z6cYs7yNvg_0O1uVA?sz=32" class=""></td>
<td class=""><br></td></tr></tbody></table></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/6/10 Bradley Lucier <span dir="ltr"><<a href="mailto:lucier@math.purdue.edu" target="_blank">lucier@math.purdue.edu</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Re:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
FWIW, I would have at least written:<br>
<br>
((qty (in-range 0 (add1 (min (floor (/ weight-left weight))<br>
(floor (/ volume-left volume)))))))<br>
</blockquote>
<br>
I have now seen the<br>
<br>
(floor (/ a b))<br>
<br>
idiom a number of times, and wonder why people prefer it to<br>
<br>
(quotient a b)<br>
<br>
Normally, to calculate (/ a b) where a and b are exact integers requires one to calculate (gcd a b) to put the fraction into lowest terms p/q=(quotient a (gcd a b))/(quotient b (gcd a b)); then, to calculate (floor p/q), one must calculate (quotient p q).<br>
<br>
For large integers of size $N$ bits, (gcd a b) takes $O(N\log^2(N))$ fixnum operations, where quotient takes $O(N\log N)$ operations. This assumes Fourier-based methods for bignum multiplication; for more direct methods, the difference in operation count is larger.<br>
<br>
In any case, the (floor (/ ...)) idiom takes noticeably more time than (quotient ...). If one knows that a and b are positive integers, they give the same results.<br>
<br>
This is an argument not to use (floor (/ ...)). Are there arguments in favor of this idiom?<br>
<br>
Brad<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/<u></u>users</a><br>
</blockquote></div><br></div>