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 3998930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:35:26+00:00 2026-05-20T07:35:26+00:00

My sanity check fails because a double variable does not contain the expected result,

  • 0

My sanity check fails because a double variable does not contain the expected result, it’s really bizarre.

double a = 1117.54 + 8561.64 + 13197.37;
double b = 22876.55;
Console.WriteLine("{0} == {1}: {2}", a, b, a == b);

Gives us this output:

22876.55 == 22876.55: False

Further inspection shows us that variable a, in fact, contains the value 22876.550000000003.

This is reproducible in vb.net as well. Am I sane? What is going on?

  • 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-20T07:35:26+00:00Added an answer on May 20, 2026 at 7:35 am

    Floating point types are not always capable of accurately representing their exact decimal values. It’s either a “known bug” or “by design”, depending on your perspective. Either way, it’s a consequence of the internal representation of floating point types, and a common source of bugs.

    The problem is nearly unavoidable, too, short of writing a complex computer algebra system that represents values symbolically, rather than as numeric types. Open up Windows Calculator, determine the square root of 4, and then subtract 2 from that value. You’ll get some nonsensical floating point number that is incredibly close to 0, but not exactly 0. The result of your square root computation wasn’t stored as exactly 2, so when you subtract exactly 2 from it, you get an “unexpected” result. Unexpected, that is, unless you know the dirty little secret about base 2 arithmetic.

    If you’re curious, there are several places you might go to find out more information about why this is the case. Jon Skeet wrote an article explaining binary floating point operations in the context of the .NET Framework. If you have the time, you should also peruse the aptly-named publication, What Every Computer Scientist Should Know About Floating-Point Arithmetic.

    But the bottom line is that you shouldn’t expect to be able to compare the result of a floating point operation to a floating point literal. In this specific case, you might try using the decimal type, instead. It’s not really a “solution” (see the other answers for those, scary mathematical concepts like epsilons), but the results are often more predictable, as the decimal type is better at accurately representing base 10 numbers (such as those used in currency and financial calculations).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Other being able to sanity check values in a setter is there a more
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag
This is just a sanity check, but I've done several test cases before you
I just need a sanity check here... DEMO Basically, I can't figure out how
I need a sanity check as I've spent about an hour trying to figure
Currently i'm using the iMask script to help perform masking and sanity check on
I was just testing a small script in Prolog to sanity check the MySQL
Quick sanity check. Is Array.Copy for one dimensional arrays only, or can it handle
Sanity check, please. The solution I'm hearing sounds ill-conceived, but may be the only
I came across a Sanity Check sql task in an old DTS Package, but

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.