A Philosophy of Software Design
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Tue Jun 30 04:00:12 UTC 2026
On 30/06/2026 7:49 AM, Walter Bright wrote:
> On 6/29/2026 1:05 AM, Richard (Rikki) Andrew Cattermole wrote:
>> But you can also throw static analysis at it to catch royally stupid
>> cases like so:
>>
>> ```d
>> void thing(float arg) {
>> float f;
>>
>> return f + arg * 2;
>> }
>> ```
>
I just noticed that is void and should be float oops.
> Default initializing f to nan catches it and propagates the error.
> Default initialization to 0 does not.
At _runtime_ yes.
But it shouldn't get that far for royally stupid code like this.
Due to:
1. NaN like this should be considered to be a poison value, that will
optimize _any_ expression that touches it by a modern backend.
2. It will NEVER do something useful at runtime. It will always a be a
bug in the code, due to not initializing or not knowing that it is NaN
not 0.
3. Failing to compile for 100% bad code is always better than runtime
bad data. Even if it is a sentinel like NaN.
ldc -O:
```d
float thing(float arg) {
float f;
return f + arg * 2;
}
float another(float arg) {
return thing(arg) + 2;
}
```
```
.LCPI0_0:
.long 0x7fc00000
float example.thing(float):
movss xmm0, dword ptr [rip + .LCPI0_0]
ret
.LCPI1_0:
.long 0x7fc00000
float example.another(float):
movss xmm0, dword ptr [rip + .LCPI1_0]
ret
```
More information about the Digitalmars-d
mailing list