between and among: worth Phobosization?

Simen Kjærås simen.kjaras at gmail.com
Tue Dec 17 07:58:10 PST 2013


On 17.12.2013 16:22, Francesco Cattoglio wrote:
> On Monday, 16 December 2013 at 22:53:24 UTC, H. S. Teoh wrote:
>>> What's wrong with having it implemented analogous to
>>> std.random.uniform -- taking a bounds parameter which allows for
>>> open and/or closed at either end, with the default being "[)" ... ?
>>>
>>> By the way, I'd also like to see that open/closed-at-either-end
>>> specialization extended to std.range.iota().
>>
>> Oooh, do I hear a volunteer for cleaning up iota? ;-)
>>
>> While you're at it, you might as well implement:
>>
>>     https://d.puremagic.com/issues/show_bug.cgi?id=10762
>>
>> :-P
>>
>>
>> T
>
> Well, I might actually volunteer :)
> Any pointer about implementing that enhancement? Can one just forgot about the
> different versions (one for integrals, one for floating points) and just
> implement the One True Iota?

One problem of OTI (One True Iota) is floating-point inaccuracies. What does 
this function return? (hint: it's not 16777217)

float foo() {
     return 16777216f + 1.0f;
}

So if you want iota(16777217f, 16777245f, 1.0f) to not go into an infinite loop, 
you will probably need to special-case floating-point. If you assume that you 
can duplicate whatever is passed to iota, you could do something like this:


T _front;
size_t _index;
U _step;

void popFront() {
    ++_index;
    if (_front + _index * _step != _front) {
        _front += _index * _step;
        _index = 0;
    }
}

I don't think even this covers all bases (consider the case where the inaccuracy 
of real is > size_t.max).

I'm playing with writing a new iota now too. Could be fun to see what different 
solutions people come up with.

-- 
   Simen


More information about the Digitalmars-d mailing list