Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 365649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:33:26+00:00 2026-05-12T13:33:26+00:00

I am seeing something odd with storing doubles in a dictionary, and am confused

  • 0

I am seeing something odd with storing doubles in a dictionary, and am confused as to why.

Here’s the code:

            Dictionary<string, double> a = new Dictionary<string, double>();
            a.Add("a", 1e-3);

            if (1.0 < a["a"] * 1e3)
                Console.WriteLine("Wrong");

            if (1.0 < 1e-3 * 1e3)
                Console.WriteLine("Wrong");

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.

This is for C# 3.5 in Visual Studio 2008.

Is this a floating point accuracy problem? Then why does the second if statement work? I feel I am missing something very fundamental here.

Any insight is appreciated.

Edit2 (Re-purposing question a little bit):

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 immediate window. I paste the code from the first if statement into the immediate window and it evaluates false.

Update

First of all, thanks very much for all the great answers.

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:
screenshot of problem

The immediate window tells me the if is false, yet the code falls into the conditional.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:33:26+00:00Added an answer on May 12, 2026 at 1:33 pm

    It is floating precision problem.

    Second statement works because the compiler counts the expression 1e-3 * 1e3 before emitting the .exe.

    Look it up in ILDasm/Reflector, it will emit something like

     if (1.0 < 1.0)
                    Console.WriteLine("Wrong");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am probably not seeing something here, that is why I am asking for
I'm seeing something very very strange happening in a Flex app I'm maintaining. I've
I do remember seeing someone ask something along these lines a while ago but
I'm sure I am just doing something really dumb and not seeing it but
Seeing this: http://www.suckless.org/wiki.html . A wiki based on Mercurial. Are there any other non-code
After seeing the cool new reputation tab on the stackoverflow user page, I was
I'm seeing strange errors when my C++ code has min() or max() calls. I'm
I think I'm missing something simple, but I'm just not seeing it. I have
Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how
After seeing a previous post Making Applications programmed in .NET languages work on older

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.