Andrei Alexandrescu wrote:
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"?Nick Sabalausky wrote:"Walter Bright" <newshou...@digitalmars.com> wrote in message news:h30845$2kl...@digitalmars.com...bearophile wrote:Nope. Wrap around is always a special case, not just for ranges. It's in the nature of 2's complement arithmetic.Walter Bright Wrote:Me too, but does it work when the upper interval is int.max, char.max, ulong.max, ucent.max, etc?I like: a .. b+1 to mean inclusive range.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 ita 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.
That's what I call consistent! Jerome -- mailto:jeber...@free.fr http://jeberger.free.fr Jabber: jeber...@jabber.fr
signature.asc
Description: OpenPGP digital signature