Always false float comparisons

ag0aep6g via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 30 04:35:26 PDT 2017


On 06/30/2017 07:38 AM, Ecstatic Coder wrote:
> I'm just against putting it on by default, so that the current behavior 
> is kept, because I don't see where is the language improvement in having 
> to put these ugly manual conversion everywhere just because the 
> string/array length was made unsigned.

So what you really want is: signed array lengths. You only have a use 
for the sloppy conversion, because D doesn't have signed array lengths.

> I always use signed integers for string/array indices because unsigned 
> indices become dangerous as soon as your algorithm starts to decrement 
> it...

I guess they're "dangerous" because you get ulong.max instead of (a 
signed) -1? But that's exactly what happens with the implicit 
conversion, too.

At some point you have to think of that. Either when adding/subtracting 
or when comparing signed with unsigned. With a warning/error on the 
implicit conversion, you'd get a nice reminder.

By the way, std.experimental.checkedint may interesting:

----
import std.experimental.checkedint: checked, ProperCompare,
     Saturate;

/* If negative values should not occur, throw an error on wrap
around: */
auto x = checked(size_t(0));
--x; /* error */

/* Or stop at 0: */
auto y = checked!Saturate(size_t(0));
--y; /* does nothing */
assert(y == 0); /* passes */

/* If negative values are valid, you can use proper comparisons
without sloppy implicit conversions or verbose manual ones: */
auto z = checked!ProperCompare(int(-1));
auto array = [1, 2, 3];
assert(z < array.length); /* passes */
----


More information about the Digitalmars-d mailing list