[OT] The Usual Arithmetic Confusions

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Mon Jan 31 17:52:17 UTC 2022


On Monday, 31 January 2022 at 17:22:42 UTC, Paulo Pinto wrote:
> Compilers can do whatever they feel like, except when one 
> doesn't follow "A compiler may not optimize code under the 
> assumption that overflow does not occur" is no longer compliant 
> with the language specification, no matter what.

If you get the same output/response from the same input then you 
haven't deviated from the specification.

Thus if you have overflow checks on integer artithmetics then 
this:

```
for(int i=1; i<99999; i++){
    int x = next_monotonically_increasing_int_with_no_sideffect();
    if (x < x+i){…}
}

```

has the same effect as this:

```
int x;
for(int i=1; i<99998; i++){
    x = next_monotonically_increasing_int_with_no_sideffect();
}
assert(x <= maximum_integer_value - 99998);

```

> Similarly a Scheme compiler that doesn't do tail call recursion 
> isn't a Scheme proper, as the standard has specific details how 
> tail recursion is required to exist.

A well written language specification should only specify the 
requirements for observable behaviour (including memory 
requirements and interfacing requirements). If it is is 
observable in Scheme, then it makes sense, otherwise it makes no 
sense.



More information about the Digitalmars-d mailing list