[Haskell-cafe] instance Enum Double considered not entirely great?

Richard O'Keefe ok at cs.otago.ac.nz
Tue Sep 27 02:14:28 CEST 2011


On 27/09/2011, at 6:50 AM, Chris Smith wrote:

> On Mon, 2011-09-26 at 18:53 +0200, Lennart Augustsson wrote:
>> If you do [0.1, 0.2 .. 0.3] it should leave out 0.3.  This is floating
>> point numbers and if you don't understand them, then don't use them.
>> The current behaviour of .. for floating point is totally broken, IMO.
> 
> I'm curious, do you have even a single example of when the current
> behavior doesn't do what you really wanted anyway?  Why would you write
> an upper bound of 0.3 on a list if you don't expect that to be included
> in the result?

Because upper bounds are *UPPER BOUNDS* and are NOT as a rule included
in the result.  If you write [0,2..9] you
 - DO expect 0 in the result
 - DON'T expect 9 in the result
 - would be outraged if 10 were in the result.
You would "write an upper bound of 0.3 on a list" if you wanted the
list to be bounded above by 0.3.

>  I understand that you can build surprising examples with
> stuff that no one would really write... but when would you really *want*
> the behavior that pretends floating point numbers are an exact type and
> splits hairs?

No, the *existing* behaviour is the behaviour that pretends floating
point numbers are exact and splits hairs and fudges results.

#include <stdio.h>

int main(void) {
    double x;
    int i;
    for (i = 0; (x = 0.0 + i*0.1) <= 0.3; i++)
        printf("%.20f\n", x);
    return 0;
}

Output:
0.00000000000000000000
0.10000000000000000555
0.20000000000000001110

Just as expected.

An x > 0.3 in the output would be wrong.




More information about the Haskell-Cafe mailing list