2 comments

  • WalterBright1 hour ago
    NaNs are a very underappreciated feature of IEEE-754 floating point. In the D programming language, floats get default initialized to NaN, not to 0.0.<p><pre><code> double y = 0.0; &#x2F;&#x2F; initialized to 0.0 double x; &#x2F;&#x2F; initialized to NaN </code></pre> The discussion routinely comes up as &quot;why not default initialize to 0.0?&quot; The reason is a routine mistake in programming is forgetting to initialize a variable. With a floating point 0.0, one may never realize that the floating point calculation results are wrong. But with NaN, the result of a floating point computation will be NaN, which is unlikely to go unnoticed.<p>I don&#x27;t know of any other programming language with this safety feature.<p>Also, the D `char` type is initialized to 0xFF, not 0, because Unicode says that 0xFF is an invalid character.
    • p1necone50 minutes ago
      Just requiring explicit assignment before first use feels like the superior approach to automatic initialization, regardless of whether the automatic initialization is with 0 or with NaN.
      • WalterBright36 minutes ago
        That suggestion is often made.<p>The trouble with it is a bug I&#x27;ve seen often. People will get an error message about an &quot;uninitialized variable&quot;. Then they go into &quot;just get the compiler to shut up&quot; mode, amd pick &quot;0&quot; as the initializer. Then, the program compiles and runs, and silently produces the wrong answer. Code reviews will simply pass over the &quot;0&quot; initializer, as it looks right.<p>With default NaN initialization, the programmer is more likely to stop and think about it, not just insert 0.<p>Another issue with it is:<p><pre><code> float x = 0.0; setFloat(&amp;x); void setFloat(float* px) { *px = 3.0; } </code></pre> For the purposes of code clarity I don&#x27;t want to see a variable initialized to a value that is never used, just to shut the compiler up.
    • anitil41 minutes ago
      That&#x27;s a very thoughtful decision, I always enjoy your updates on D
    • WalterBright1 hour ago
      Another crucial use of NaNs is if you have a sensor. If the sensor has failed, the sensed value should be transmitted as NaN, not 0, so the receiver knows the data is bad.
      • AlotOfReading31 minutes ago
        My experience is that if you write an interface that (rarely) returns NaNs, someone will use it assuming it&#x27;s never NaN no matter how good the docs are. Then their code does bad things and you have to patiently explain why they&#x27;re wrong and yes, they <i>are</i> holding isnan() wrong (in C&#x2F;C++).
    • wpollock45 minutes ago
      &gt; ... Unicode says that 0xFF is an invalid character.<p>Not so. You may be thinking of UTF-8 encoding. 0xff is DEL in Unicode.
      • WalterBright32 minutes ago
        The &quot;char&quot; type in D represents a UTF-8 code unit, the byte 0xFF is not a valid character code and is strictly forbidden.
  • GMoromisato1 hour ago
    I use nan boxing in GridWhale. It feels like the Infinite Hotel[1]: you can always add another type. Note that these techniques also rely on the fact that we don&#x27;t use all 64-bits for memory addressing. If we ever do, lots of VMs will break.<p>For me, the major advantage of nan boxing is that you don&#x27;t have to allocate a whole class of types (like floats). That saves so much at garbage collection time.<p>------------<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hilbert%27s_paradox_of_the_Grand_Hotel" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hilbert%27s_paradox_of_the_Gra...</a>