[OT] The Usual Arithmetic Confusions
bachmeier
no at spam.net
Thu Feb 17 17:53:58 UTC 2022
On Friday, 28 January 2022 at 02:15:51 UTC, Paul Backus wrote:
> Unfortunately, this is also one of the areas of D that comes
> directly from C, so D programmers have to watch out for these
> as well.
Here's a lovely one I wrote about yesterday in Learn:
```
import std.conv, std.range, std.stdio;
void main() {
writeln(iota(5, 0, -1));
writeln(iota(5, -1, -1));
writeln(iota(5.to!uint, -1, -1));
writeln(iota(5.to!uint, 0, -1));
writeln(-1.to!uint);
auto z = -1;
writeln(z.to!uint);
}
```
Which delivers the following output:
```
[5, 4, 3, 2, 1]
[5, 4, 3, 2, 1, 0]
[]
[5, 4, 3, 2, 1]
4294967295
std.conv.ConvOverflowException@/usr/include/dmd/phobos/std/conv.d(567): Conversion negative overflow
----------------
??:? pure @safe bool std.exception.enforce!(bool).enforce(bool,
lazy object.Throwable) [0x555fe1c5c946]
??:? pure @safe uint std.conv.toImpl!(uint, int).toImpl(int)
[0x555fe1c6f1ff]
??:? pure @safe uint std.conv.to!(uint).to!(int).to(int)
[0x555fe1c6f1d0]
??:? _Dmain [0x555fe1c5594c]
```
All I wanted was a function that iterates through the elements of
an array starting at the end. The only time you have a problem is
if you want to include the first element of the array.
A simple solution is to add a `-scottmeyers` switch that retains
full compatibility with C, but sets the default as a language
that is productive.
More information about the Digitalmars-d
mailing list