<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi all,<div><br></div><div>There was a recent discussion on the Lispworks mailing list that I thought may be of interest for Clozure CL users. Essentially, someone was complaining about a possible bug in Lispworks where (floor 0.59d0 0.01d0) returned 0.010000000000000009D0. The reasonable expectation (even given the vagaries of inexact floating point arithmetic) that the remainder is always less than the divisor is violated.</div><div><br></div><div>Joe Marshall pointed out that this is probably due to the way floor is being computed (see below) and CLISP seems to do it the "right" way. CCL, which defines floor in terms of truncate gives</div><div><div><br></div><div>? (truncate 0.59d0 0.01d0)</div><div>58</div><div>0.010000000000000009D0</div><div><br></div><div>which does seem strange ...</div><div><br></div><div>Cheers</div><div>Sudhir</div><div><br><div>Begin forwarded message:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>From: </b></span><span style="font-family:'Helvetica'; font-size:medium;">Joe Marshall <<a href="mailto:jmarshall@alum.mit.edu">jmarshall@alum.mit.edu</a>><br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>Subject: </b></span><span style="font-family:'Helvetica'; font-size:medium;"><b>Re: Double precision Floor bug?</b><br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>Date: </b></span><span style="font-family:'Helvetica'; font-size:medium;">June 4, 2012 4:27:15 AM GMT+09:00<br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>To: </b></span><span style="font-family:'Helvetica'; font-size:medium;">Guy Footring <<a href="mailto:Guy@footring.net">Guy@footring.net</a>><br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>Cc: </b></span><span style="font-family:'Helvetica'; font-size:medium;"><a href="mailto:lisp-hug@lispworks.com">lisp-hug@lispworks.com</a><br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>Reply-To: </b></span><span style="font-family:'Helvetica'; font-size:medium;">Joe Marshall <<a href="mailto:jmarshall@alum.mit.edu">jmarshall@alum.mit.edu</a>><br></span></div><br>This was bothering me this weekend so I did some more playing around.<div>Computing the quotient part of the answer (the first result) is straightforward:</div><div>you just divide and drop the fractional part. It is the remainder that is</div>
<div>the problem. There are several ways to compute the remainder, and</div><div>it seems that Lispworks does it this way:</div><div><br></div><div><div>(defun floor (n divisor)</div><div> (let* ((quotient (/ n divisor))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> (integer-part (truncate quotient))</div><div> (remainder (- n (* integer-part divisor))))</div><div> (values integer-part remainder)))</div>
</div><div><br></div><div>But you could use the fractional part of the quotient instead like this:</div><div><br></div><div><div>(defun floor (n divisor)</div><div> (let* ((quotient (/ n divisor))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (integer-part (truncate quotient))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> (remainder (* (- quotient integer-part) divisor)))</div><div> (values integer-part remainder)))</div></div><div><br></div><div>If the arithmetic were exact, these would produce the same answers,</div>
<div>but in floating point, the first produces</div><div><span style="">58 0.010000000000000009d0</span></div><div><span style="">but the second produces</span></div><div><span style="">58 </span><font color="#222222" face="arial, sans-serif">0.009999999999999929d0</font></div>
<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">This second way of computing the remainder satisfies your expectations</font></div><div><font color="#222222" face="arial, sans-serif">about the constraints.</font></div>
<div><font color="#222222" face="arial, sans-serif"> </font></div><div><font color="#222222" face="arial, sans-serif">Incidentally, clisp appears to do it the second way.</font></div><div><font color="#222222" face="arial, sans-serif"><div>
[1]> (floor .59d0 .01d0)</div><div>58 ;</div><div>0.009999999999999929d0</div><div><br></div><div><br></div></font></div><div>-- <br>~jrm<br>
</div>
</blockquote></div><br></div></body></html>