Foreach Range Statement

Sean Kelly sean at f4.ca
Mon Jul 23 12:14:42 PDT 2007


Don Clugston wrote:
> 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)) ...

Makes me feel like we were better off without foreachable ranges in the 
first place.  It's obviously possible to do:

foreach( f; inclusive( -float.infinity, float.infinity ) ) {}

And it is potentially more meaningful as well, given that we can't use 
the mathematical notation [] vs [), etc.  Also, just like the new 
foreachable ranges, the above syntax evaluates both the begin and end 
arguments only once and doesn't require the user to explicitly specify a 
type.

> (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?

You can't :-)  For some reason I thought it would overflow to infinity 
and "just work", but you're right.  The value has to be stored.

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

Yup.  Personally, I'd much rather have a separate, explicitly defined 
range syntax than this new foreach feature, or just leave things as-is. 
  But then I'm not terribly fond of basically any new features 
introduced in 2.0, so I suppose this is just par for the course.


Sean



More information about the Digitalmars-d mailing list