<div dir="ltr">I feel like swapping out flonums by default to rationals by default would cause unexpected slowness in a large number of programs though. Would it be possible to make it a reader extension like at-exp is currently? So I can say "#lang exact-decimals racket", and the reader would read the decimals as rationals?</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 26, 2015 at 9:13 AM, Neil Toronto <span dir="ltr"><<a href="mailto:neil.toronto@gmail.com" target="_blank">neil.toronto@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 02/24/2015 01:11 PM, Konrad Hinsen wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 24/02/2015 16:41, Laurent wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've discovered a rather troubling behaviour when using `in-range` with<br>
floating point numbers, which I think is worth knowing in case you<br>
hadn't consider the issue before:<br>
<br>
On my machine, I get the following:<br>
<br>
(length (for/list ([i (in-range .1 .7 .1)]) i)) ; 6<br>
(length (for/list ([i (in-range .1 .8 .1)]) i)) ; 8 (!)<br>
<br>
But:<br>
(length (for/list ([i (in-range 1/10 7/10 1/10)]) i)) ; 6<br>
(length (for/list ([i (in-range 1/10 8/10 1/10)]) i)) ; 7<br>
<br>
<br>
Would it be a good idea to safe-guard these kinds of cases directly in<br>
`in-range`?<br>
</blockquote>
<br>
The problem is an old one that already troubled programmers in the age<br>
of Fortran. I suspect there is no reasonable safe-guard, with<br>
"reasonable" meaning that it does what people expect in all situations.<br>
<br>
The only way to stay out of trouble is to avoid loops defined by an<br>
accumulating floating-point value. This means either don't use floats<br>
(write the loop over integers or rationals and convert to floats when<br>
using the loop index), or don't use accumulation (define your range by<br>
two points and the number of subdivisions, rather than the width of<br>
subintervals).<br>
</blockquote>
<br></div></div>
I should have chimed in to support this two days ago, but this is exactly the right answer. Here's what Konrad means by his first alternative (write the loop over integers or rationals):<br>
<br>
(length<br>
(for*/list ([i (in-range 1 8 1)]<br>
[i (in-value (* i 0.1))])<br>
i))<br>
<br>
The second alternative is a little harder to get right because of fencepost errors [1]. Fortunately, Racket has a library function for it. Unfortunately, it's buried in `plot/utils`. Here it is in action:<br>
<br>
> (require (only-in plot/utils linear-seq))<br>
<br>
> (linear-seq 0.0 1.0 4)<br>
'(0.0 0.3333333333333333 0.6666666666666666 1.0)<br>
<br>
> (linear-seq 0.0 1.0 4 #:start? #f)<br>
'(0.14285714285714285 0.42857142857142855 0.7142857142857142 1.0)<br>
<br>
> (linear-seq 0.0 1.0 4 #:start? #f #:end? #f)<br>
'(0.125 0.375 0.625 0.875)<br>
<br>
I should really move this function into `math/base`.<br>
<br>
If you must use a flonum step size, do something like this:<br>
<br>
(define (flonum-range start end step)<br>
(define n (exact-ceiling (/ (- end start) step)))<br>
(for/list ([i (in-range 0 n)])<br>
(+ start (* i step))))<br>
<br>
To get points with 0.5 ulp error each, which is the best you can do, add (require math/flonum) to your program and change the loop body to (flfma step (fl i) start).<br>
<br>
Arguably, `in-range` should do something like the above when given floating-point step lengths. I don't know how feasible that is, though.<br>
<br>
Neil ⊥<br>
<br>
[1] <a href="http://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error" target="_blank">http://en.wikipedia.org/wiki/<u></u>Off-by-one_error#Fencepost_<u></u>error</a><div class="HOEnZb"><div class="h5"><br>
<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>
</div></div></blockquote></div><br></div>