Case Range Statement ..

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Mon Jul 13 03:11:47 PDT 2009


Don wrote:
> Michiel Helvensteijn wrote:
>> Andrei Alexandrescu wrote:
>>
>>> void main()
>>> {
>>>      foreach_reverse (i; 0.7 .. 100.7)
>>>      {
>>>          write(i, " ");
>>>      }
>>> }
>>>
>>> The last number printed is -0.3.
>>
>> A question if I may.
>>
>> Why does D allow iteration over an interval of floats? A floating point
>> number has no direct successor or predecessor. Any such interval would
>> contain zero, one or infinite elements.
>>
>> Given the -0.3, I'm assuming you increase/decrease by 1.0 each 
>> iteration. Is
>> this useful enough to be the standard behavior?
>>
> 
> It's a petri dish. Perfect breeding ground for bugs.
> 
> real BIG = 2.0 /(real.epsilon);
> 
> foreach(i; BIG .. BIG + 2) {
>   ...
> }
> 
> Infinite loop, since BIG + 1 == BIG.


It's also inconsistent:

   foreach (e; range)      // Iterate over *all* elements in range
   foreach (i; 0..10)      // Iterate over *all* integers from 0 to 9
   foreach (f; 0.0..10.0)  // Iterate over a small subset of
                              floating-point numbers between 0 and 10

-Lars



More information about the Digitalmars-d mailing list