[OT] The Usual Arithmetic Confusions

Dukc ajieskola at gmail.com
Thu Feb 3 17:33:35 UTC 2022


On Thursday, 3 February 2022 at 17:05:01 UTC, Ola Fosheim Grøstad 
wrote:
> Modular arithmetics doesn't help at all, it makes it worse. It 
> is better to have a conditional correctly removed than wrongly 
> get it inverted, the latter is disastrous for correctness.
>
> So no, undefined behaviour is not worse than defined behaviour 
> when the defined behaviour is the kind of behaviour nobody 
> wants!

Oh now I understand what you're saying. I don't agree though. 
With overflow at least you can clearly reason what's happening. 
If compiled code starts to mysteriously disappear when you have 
overflows there is potential for some very incomprehensible bugs.

It probably would not be that bad in the `x < x + 1` example but 
in the real world you might have careless multiplying of 
integers, for instance. Lets say I do this:

```d
fun(aLongArray[x]);
x *= 0x10000;
```

If the array is long enough, with semantics you're advocating the 
compiler might reason:

1. `x` can't overflow, so it must be 0x7FFF at most before the 
multicipation.
2. I know `aLongArr` is longer than that, so I can elide the 
bounds check.

Overflows are much less an issue than stuff like that.


More information about the Digitalmars-d mailing list