[plt-scheme] for in-range (was redefinition in module)

From: Jos Koot (jos.koot at telefonica.net)
Date: Sat Mar 1 07:32:25 EST 2008

Hi
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. When some decades ago do-loops with real variables were introduced in fortran, there was discussion about how to approach it:

1: accumulated addition of the step to the start.

2: compute the number of iterations in advance as
   n=(inexact->exact (ceiling (/ (- finish start) step))))
   and loop (for ((var (in-range 0 n 1))) (let ((var (+ start (* i step)))) etc))

The second method was choosen, because it was argued that accumulative addition also would accumulate error. The code below illustrates the difference:
Kind regards, Jos

#lang scheme

(define start 1.0e10)
(define finish 1.000001e10)
(define step 0.01)

(define-values (nr-of-iterations-accumulated sum-accumulated)
 (let ((nr-of-iterations 0) (sum 0))
  (for ((var (in-range start finish step)))
   (set! nr-of-iterations (add1 nr-of-iterations))
   (set! sum (+ sum var)))
  (values nr-of-iterations sum)))

(define-values (nr-of-iterations-precomputed sum-precomputed)
 (let ((sum 0) (nr-of-iterations 0))
  (for ((i (in-range 0 (inexact->exact (ceiling (/ (- finish start) step))) 1)))
   (let ((var (+ start (* i step))))
    (set! nr-of-iterations (add1 nr-of-iterations))
    (set! sum (+ var sum))))
  (values nr-of-iterations sum)))

(printf "nr-of-iterations precomputed: ~s~n" nr-of-iterations-precomputed)
(printf "nr-of-iterations accumulated: ~s~n" nr-of-iterations-accumulated)
(printf "difference: ~s~n~n"
 (- nr-of-iterations-precomputed nr-of-iterations-accumulated))

(printf "sum-precomputed: ~s~n" sum-precomputed)
(printf "sum-accumulated: ~s~n" sum-accumulated)
(printf "difference: ~s~n" (- sum-precomputed sum-accumulated))
(printf "relative error: ~s~n"
 (* 2
  (/
   (- sum-precomputed sum-accumulated)
   (+ sum-accumulated sum-precomputed))))

Welcome to DrScheme, version 3.99.0.13-svn27feb2008 [3m].
Language: Module; memory limit: 750 megabytes.

nr-of-iterations precomputed: 1000000
nr-of-iterations accumulated:  999978
difference:                        22

sum-precomputed: 10000004999991172.0
sum-accumulated:  9999784999889490.0
difference:           220000101682.0
relative error: 2.2000241170863566e-005
> 

----- Original Message ----- 
From: "Noel Welsh" <noelwelsh at gmail.com>
To: "Jos Koot" <jos.koot at telefonica.net>
Cc: <plt-scheme at list.cs.brown.edu>
Sent: Friday, February 29, 2008 8:36 PM
Subject: Re: [plt-scheme] redefinition in module


> On Fri, Feb 29, 2008 at 7:04 PM, Jos Koot <jos.koot at telefonica.net> wrote:
>> For example, I had my own nice and more general version of syntax 'for'. I
>> no longer can import my own 'for' without renaming.
> 
> If you have suggestions to improve the comprehension macros I, for
> one, would like to hear them.  Designing such a system is very tricky,
> and perhaps the existing system can be improved further.
> 
> N.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080301/ea579f2f/attachment.html>

Posted on the users mailing list.