Foreach Range Statement

Don Clugston dac at nospam.com.au
Mon Jul 23 12:02:16 PDT 2007


Sean Kelly wrote:
> Don Clugston wrote:
>> Bill Baxter wrote:
>>> Don Clugston wrote:
>>>
>>>> Consider
>>>> -real.infinity..real.infinity
>>>> Are the infinities part of the range? If not, how do you specify a 
>>>> range which includes infinity?
>>>
>>> Does it matter that much?  I suppose it would be cool if it did 
>>> something really consistent, but Numpy just craps out and gives you 
>>> an empty list, and Matlab raises an error "Maximum variable size 
>>> allowed by the program is exceeded".
>>
>> I think that if you can't specify a range including an infinity, then 
>> floating point ranges don't make sense. Especially, I really don't 
>> like the idea that -real.infinity..real.infinity would include 
>> -infinity, but not +infinity.
> 
> Hm... what if I wanted a range that included ulong.max?  Is there any 
> way to do that either?

Probably not. The [a..b) definition of a range is great as long as you only use 
ranges for array slicing, but it doesn't generalise well to other use cases.

To say x must be between -5.0 and +5.0, inclusive (mathematically [-5.0, 5.0]), 
using the existing semantics, you'd have to say:

if (x in -5.0 .. nextUp(5.0)) ...

and -5.0 to 5.0 exclusive (mathematically (-5.0, 5.0)) is:
if (x in nextUp(-5.0) .. 5.0) ...

Both of these cases are going to be far more common than [-5.0, 5.0) which 
should be as common as (-5.0, 5.0] which requires the monstrosity:
if (x in nextUp(-5.0)..nextDown(5.0)) ...

(and nextUp isn't even in Phobos - you have to use Tango <g>).

   I don't suppose ulong.max+1 works in that case?
No.
>   I know it would be a tad weird because infinity+1 == infinity, but 
> perhaps this is one case where the semantics should just be consistent 
> with everything else.

How can you store infinity + 1?

Also, it won't work for 0.0001 .. 0.0003. You don't actually want to add 1.



More information about the Digitalmars-d mailing list