Case Range Statement ..

"Jérôme M. Berger" jeberger at free.fr
Wed Jul 8 14:41:17 PDT 2009


Andrei Alexandrescu wrote:
> Nick Sabalausky wrote:
>> "Walter Bright" <newshound1 at digitalmars.com> wrote in message 
>> news:h30845$2kla$2 at digitalmars.com...
>>> bearophile wrote:
>>>> Walter Bright Wrote:
>>>>> I like:
>>>>>     a .. b+1
>>>>> to mean inclusive range.
>>>> Me too, but does it work when the upper interval is int.max, 
>>>> char.max, ulong.max, ucent.max, etc?
>>> Nope. Wrap around is always a special case, not just for ranges. It's 
>>> in the nature of 2's complement arithmetic.
>>
>> Suppose, hypothetically, that we did have a "right-inclusive-range" 
>> token. At the moment I don't give a rat's ass what it actually is, 
>> '...', '..#', 'poopiesOnAFlyingStick', whatever. Now, as has been 
>> pointed out far to many times by now to still not get:
>>
>> a .. int.max+1   // wraparound breaks it
>> a right_inclusive_range_tok int.max    // works fine, wraparound isn't 
>> even relevant.
> 
> Writing an inclusive e.g. loop that goes all the way to the largest 
> integers is tricky no matter what. So this is not a problem with open 
> ranges. Open ranges are there for a good reason.
> 
>> And another problem people have already pointed out numerous times 
>> with making "a .. b+1" the inclusive range syntax:
>>
>> double a=1, b=5;
>> // Let's make an inclusive range from 1.0 to 5.0!
>> a .. b+1
>> // Oh look! 5.5f is in the range! It didn't f*^* work!
> 
> b+1 is correct if the range is iterated by 1, as in foreach or as an 
> array index. That's what Walter meant because that's what's in the 
> language. If you want to define a floating-point range that is closed to 
> the right, you may want to use an open range from a to nextUp(b). The 
> nextUp function (defined in std.math by Don I think) returns the very 
> next floating point value. That's exactly what I do in std.random.
> 
	So, the correct way to define an inclusive range is with "+1" 
except when it's with "nextUp", unless you're talking about the 
right end of the range where it is inclusive by default and the 
right way to make it exclusive is with "+1" except when it's with 
"nextUp"?

	That's what I call consistent!

		Jerome
-- 
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20090708/02fd7a91/attachment.pgp>


More information about the Digitalmars-d mailing list