Foreach Range Statement
Bill Baxter
dnewsgroup at billbaxter.com
Tue Jul 24 04:21:04 PDT 2007
Don Clugston wrote:
> OK, that makes sense. Although, for the integer case it's clear how many
> elements are in a range; it's not at all obvious for floating point.
>
>> To have the beginning or end be infinite is asking for trouble. For
>> instance in Matlab that tries to allocate an infinite-sized array of
>> numbers.
>
> Agreed. Although 0..infinity is only one element bigger than 0..real.max.
Either way, it's still going to be a memory error in Matlab. The
real.max one would probably be a memory error in Numpy too.
>>
>> Right. Numpy had the same problem. Python itself uses the same
>> non-inclusive rule as D. But Python only handles integers in things
>> like the "range(start,end,step)" function. The Numpy folks wanted to
>> extend that to work for floating point types as well. But actually,
>> in both matlab and numpy, if you want an evenly spaced set of numbers,
>> you usually use the 'linspace' function, which has the signature
>> linspace(begin,end,numvals). This creates an inclusive array of numbers.
>
> That makes sense. You could also have a logarithmic range.
Yes, both have a "logspace" function as well.
> But what need is there for using ".." with floating point numbers?
> Surely we can already write
> foreach(float x, linspace(begin, end, numvals)){}
I can't really think of any super duper reason. It's just a shortcut for:
for(float x=0.0; x<end; x+=0.1) { }
I've used it in Python before. I think I used it more in Matlab,
though, where the ranges are inclusive.
>> I think one source of confusion is that ranges and slices are very
>> similar things, but not quite the same.
>>
>> * A range is just a sequence of numbers. It can exist and be
>> interpreted independently. Here allowing floating point numbers makes
>> sense. Allowing for infinity may make sense, but practically it's
>> very niche. Iterating over infinite things usually takes either too
>> much time or too much memory.
>
> How is that different to a set? I've always assumed a range (a,b)
> contained EVERYTHING between a and b. I'm not very familiar with either
> Python or Matlab.
Yes. It is just a set. An ordered set with fixed spacing between
elements, expressed using a compact notation.
>> * A slice needs an object to operate on for interpretation of
>> object-relative things like $. Generally speaking, only integers make
>> sense in a slice. Infinity doesn't really make sense because you
>> can't generally have things that are both slice-able and infinite on a
>> computer.
>>
>> (* An interval just represents two points on a numberline, plus maybe
>> an indication of the inclusivity of the endpoints. Infinity -- ok.
>> Floating point -- ok.)
>>
>> It may be possible to combine the concepts into one type, but they
>> *are* slightly different, and may benefit from being treated as so.
>
> That clarification is very helpful. Thanks.
--bb
More information about the Digitalmars-d
mailing list