Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does a C# evaluate floating point in hover over and intermediate window versus compiled?
    text
    copied!<p>I am seeing something odd with storing doubles in a dictionary, and am confused as to why.</p> <p>Here's the code:</p> <pre><code> Dictionary&lt;string, double&gt; a = new Dictionary&lt;string, double&gt;(); a.Add("a", 1e-3); if (1.0 &lt; a["a"] * 1e3) Console.WriteLine("Wrong"); if (1.0 &lt; 1e-3 * 1e3) Console.WriteLine("Wrong"); </code></pre> <p>The second if statement works as expected; 1.0 is not less than 1.0. Now, the first if statement evaluates as true. The very odd thing is that when I hover over the if, the intellisense tells me false, yet the code happily moves to the Console.WriteLine.</p> <p>This is for C# 3.5 in Visual Studio 2008.</p> <p>Is this a floating point accuracy problem? Then why does the second if statement work? I feel I am missing something very fundamental here.</p> <p>Any insight is appreciated.</p> <p><strong>Edit2</strong> (Re-purposing question a little bit):</p> <p>I can accept the math precision problem, but my question now is: why does the hover over evaluate properly? This is also true of the intermediate window. I paste the code from the first if statement into the intermediate window and it evaluates false.</p> <p><strong>Update</strong></p> <p>First of all, thanks very much for all the great answers.</p> <p>I am also having problems recreating this in another project on the same machine. Looking at the project settings, I see no differences. Looking at the IL between the projects, I see no differences. Looking at the disassembly, I see no apparent differences (besides memory addresses). Yet when I debug the original project, I see: <a href="http://i30.tinypic.com/ega874.png" rel="nofollow noreferrer">screenshot of problem http://i30.tinypic.com/ega874.png</a></p> <p>The immediate window tells me the if is false, yet the code falls into the conditional.</p> <p>At any rate, the best answer, I think, is to prepare for floating point arithmetic in these situations. The reason I couldn't let this go has more to do with the debugger's calculations differing from the runtime. So thanks very much to Brian Gideon and stephentyrone for some very insightful comments.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload