<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16608" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face="Courier New" size=2>Hi</FONT></DIV>
<DIV><FONT face="Courier New" size=2>I had a look in scheme/private/for.ss. If I
understand the code correctly in-range computes subsequent values of the
variable by accumulated addition of the step to the start. </FONT><FONT
face="Courier New" size=2>When some decades ago do-loops with real variables
were introduced in fortran, there was discussion about how to approach
it:</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>1: accumulated addition of the step to the
start.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>2: compute the number of iterations in
advance as</FONT></DIV>
<DIV><FONT face="Courier New" size=2> n=(inexact->exact (ceiling
(/ (- finish start) step))))</FONT></DIV>
<DIV><FONT face="Courier New" size=2> and loop (for ((var
(in-range 0 n 1))) (let ((var (+ start (* i step)))) etc))</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>The second method was choosen, because it
was argued that accumulative addition also would accumulate error. The code
below illustrates the difference:</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Kind regards, Jos</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>#lang scheme</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>(define start 1.0e10)<BR>(define finish
1.000001e10)<BR>(define step 0.01)</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>(define-values
(nr-of-iterations-accumulated sum-accumulated)<BR> (let ((nr-of-iterations
0) (sum 0))<BR> (for ((var (in-range start finish step)))<BR>
(set! nr-of-iterations (add1 nr-of-iterations))<BR> (set! sum (+ sum
var)))<BR> (values nr-of-iterations sum)))</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>(define-values
(nr-of-iterations-precomputed sum-precomputed)<BR> (let ((sum 0)
(nr-of-iterations 0))<BR> (for ((i (in-range 0 (inexact->exact (ceiling
(/ (- finish start) step))) 1)))<BR> (let ((var (+ start (* i
step))))<BR> (set! nr-of-iterations (add1
nr-of-iterations))<BR> (set! sum (+ var sum))))<BR>
(values nr-of-iterations sum)))</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>(printf "nr-of-iterations precomputed:
~s~n" nr-of-iterations-precomputed)<BR>(printf "nr-of-iterations accumulated:
~s~n" nr-of-iterations-accumulated)<BR>(printf "difference: ~s~n~n"<BR> (-
nr-of-iterations-precomputed nr-of-iterations-accumulated))</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>(printf "sum-precomputed: ~s~n"
sum-precomputed)<BR>(printf "sum-accumulated: ~s~n" sum-accumulated)<BR>(printf
"difference: ~s~n" (- sum-precomputed sum-accumulated))<BR>(printf "relative
error: ~s~n"<BR> (* 2<BR> (/<BR> (- sum-precomputed
sum-accumulated)<BR> (+ sum-accumulated
sum-precomputed))))</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>Welcome to DrScheme, version
3.99.0.13-svn27feb2008 [3m].<BR>Language: Module; memory limit: 750
megabytes.</FONT></DIV>
<DIV><BR><FONT face="Courier New" size=2>nr-of-iterations precomputed:
1000000<BR>nr-of-iterations accumulated:
999978<BR>difference:
22</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><FONT face="Courier New">sum-precomputed:
10000004999991172.0<BR>sum-accumulated:
9999784999889490.0</FONT></FONT></DIV>
<DIV><FONT face=Arial size=2><FONT
face="Courier New">difference: 220000101682.0<BR>relative
error: 2.2000241170863566e-005</FONT><BR>> </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>----- Original Message ----- </FONT>
<DIV><FONT face=Arial size=2>From: "Noel Welsh" <</FONT><A
href="mailto:noelwelsh@gmail.com"><FONT face=Arial
size=2>noelwelsh@gmail.com</FONT></A><FONT face=Arial size=2>></FONT></DIV>
<DIV><FONT face=Arial size=2>To: "Jos Koot" <</FONT><A
href="mailto:jos.koot@telefonica.net"><FONT face=Arial
size=2>jos.koot@telefonica.net</FONT></A><FONT face=Arial
size=2>></FONT></DIV>
<DIV><FONT face=Arial size=2>Cc: <</FONT><A
href="mailto:plt-scheme@list.cs.brown.edu"><FONT face=Arial
size=2>plt-scheme@list.cs.brown.edu</FONT></A><FONT face=Arial
size=2>></FONT></DIV>
<DIV><FONT face=Arial size=2>Sent: Friday, February 29, 2008 8:36
PM</FONT></DIV>
<DIV><FONT face=Arial size=2>Subject: Re: [plt-scheme] redefinition in
module</FONT></DIV></DIV>
<DIV><FONT face=Arial><BR><FONT size=2></FONT></FONT></DIV><FONT face=Arial
size=2>> On Fri, Feb 29, 2008 at 7:04 PM, Jos Koot <</FONT><A
href="mailto:jos.koot@telefonica.net"><FONT face=Arial
size=2>jos.koot@telefonica.net</FONT></A><FONT face=Arial size=2>>
wrote:<BR>>> For example, I had my own nice and more general version of
syntax 'for'. I<BR>>> no longer can import my own 'for' without
renaming.<BR>> <BR>> If you have suggestions to improve the comprehension
macros I, for<BR>> one, would like to hear them. Designing such a
system is very tricky,<BR>> and perhaps the existing system can be improved
further.<BR>> <BR>> N.</FONT></BODY></HTML>