Examples of floating point problems
&gt; NaN&#x2F;infinity values can propagate and cause chaos<p>NaN is the most misunderstood feature of IEEE floating point. Most people react to a NaN like they&#x27;d react to the dentist telling them they need a root canal. But NaN is actually a very valuable and useful tool!<p>NaN is just a value that represents an invalid floating point value. The result of any operation on a NaN is a NaN. This means that NaNs propagate from the source of the original NaN to the final printed result.<p>&quot;This sounds terrible&quot; you might think.<p>But let&#x27;s study it a bit. Suppose you are searching an array for a value, and the value is not in the array. What do you return for an index into the array? People often use -1 as the &quot;not found&quot; value. But then what happens when the -1 value is not noticed? It winds up corrupting further attempts to use it. The problem is that integers do not have a NaN value to use for this.<p>What&#x27;s the result of sqrt(-1.0)? It&#x27;s not a number, so it&#x27;s a NaN. If a NaN appears in your results, you know you&#x27;ve got mistake in your algorithm or initial values. Yes, I know, it can be clumsy to trace it back to its source, but I submit it is <i>better</i> than having a bad result go unrecognized.<p>NaN has value beyond that. Suppose you have an array of sensors. One of those sensors goes bad (like they always do). What value to you use for the bad sensor? NaN. Then, when the data is crunched, if the result is NaN, you know that your result comes from bad data. Compare with setting the bad input to 0.0. You never know how that affects your results.<p>This is why D (in one of its more controversial choices) sets uninitialized floating point values to NaN rather than the more conventional choice of 0.0.<p>NaN is your friend!