float init 0 request

pete email at email.com
Sat Aug 15 10:13:00 UTC 2026


I guess it depends upon the use case. In my experience with 
mostly developing 3D apps and game code etc I have only ever 
wanted the default to be 0. I have had bad experiences with the 
nan default. One time it caused a massive slow down which delayed 
me for ages trying to understand what was causing the problem. I 
just assumed for a while that D was just slow. Other times it 
caused weird visual artifacts or incorrect rendering for no 
obvious reason.

> When reviewing code, and you see:
> ```d
> float f;
> ```

For what I do the above example is not the common problem. This 
is usually pretty easy to see either as a local or a global. My 
problem is when using a struct with a float I would generally 
assume that the float would be initialised to 0 (this is what 
C/C++ does for example and copying code examples from the web 
will have some C code such as:

```
typedef struct VkClearDepthStencilValue {
     float       depth;
     uint32_t    stencil;
} VkClearDepthStencilValue;

VkClearDepthStencilValue depthStencilClear = {
     .stencil = 0
};

or

VkClearDepthStencilValue depthStencilClear = {0};
```

I would assume the D equivalent would produce the same result 
because of course everything would just be zeroed by default :)

```
VkClearDepthStencilValue depthStencilClear;
```
No this produces a bug that is unexpected and may not be trivial 
to track down.

I do understand the reasoning for defaulting to nan though. It is 
just that I have always wanted and expected it to be 0.

I do love using D btw but this is one of the small quirks that 
usually I remember to work around :)


More information about the Digitalmars-d mailing list