[racket] Inconsistency of `in-range` and numerical issues

From: Alexander D. Knauth (alexander at knauth.org)
Date: Wed Feb 25 17:14:42 EST 2015

On Feb 25, 2015, at 3:04 AM, Konrad Hinsen <konrad.hinsen at fastmail.net> wrote:

> Racket (and Scheme in general) is hard to beat when it comes to numbers because of the wide range of number types that are provided. One way to fix Laurent's example is to request an "exact" interpretation of the decimal point:
> 
>  (length (for/list ([i (in-range #e.1 #e.7 #e.1)]) i)) ; 6
>  (length (for/list ([i (in-range #e.1 #e.8 #e.1)]) i)) ; 7
> 
> This code uses rationals rather than floats. You can even tell Racket to consider the decimal point a notation for rations by default, without the #e prefix, using the parameter read-decimal-as-inexact. Unfortunately this is tricky because the parameter must be set at read time, so you can't just set it in a module where you use numbers.
> 
> If anything could be (or could have been) improved in Racket, it's two points:
> 
> 1) read-decimal-as-inexact could be #f by default, preferring exactness over efficiency by default.

I just made a meta-language that does this:
https://github.com/AlexKnauth/exact-decimal-lang
The teaching languages also do this, but this can be used with any language.




Posted on the users mailing list.