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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:40:28+00:00 2026-05-12T00:40:28+00:00

One error I stumble upon every few month is this one: double x =

  • 0

One error I stumble upon every few month is this one:

        double x = 19.08;
        double y = 2.01;
        double result = 21.09;

        if (x + y == result)
        {
            MessageBox.Show("x equals y");
        }
        else
        {
            MessageBox.Show("that shouldn't happen!");  // <-- this code fires
        }

You would suppose the code to display “x equals y” but that’s not the case.
The short explanation is that the decimal places are, represented as a binary digit, do not fit into double.

Example:
2.625 would look like:

10.101

because

1-------0-------1---------0----------1  
1 * 2 + 0 * 1 + 1 * 0.5 + 0 * 0.25 + 1 * 0,125 = 2.65

And some values (like the result of 19.08 plus 2.01) cannot be be represented with the bits of a double.

One solution is to use a constant:

        double x = 19.08;
        double y = 2.01;
        double result = 21.09;
        double EPSILON = 10E-10;

        if ( x + y - result < EPSILON )
        {
            MessageBox.Show("x equals y"); // <-- this code fires
        }
        else
        {
            MessageBox.Show("that shouldn't happen!");
        }

If I use decimal instead of double in the first example, the result is “x equals y”.
But I’m asking myself If this is because of “decimal” type is not vulnerable of this behaviour or it just works in this case because the values “fit” into 128 bit.

Maybe someone has a better solution than using a constant?

Btw. this is not a dotNet/C# problem, it happens in most programming languages I think.

  • 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-12T00:40:28+00:00Added an answer on May 12, 2026 at 12:40 am

    Decimal will be accurate so long as you stay within values which are naturally decimals in an appropriate range. So if you just add and subtract, for example, without doing anything which would skew the range of digits required too much (adding a very very big number to a very very small number) you will end up with easily comparable results. Multiplication is likely to be okay too, but I suspect it’s easier to get inaccuracies with it.

    As soon as you start dividing, that’s where the problems can come – particularly if you start dividing by numbers which include prime factors other than 2 or 5.

    Bottom line: it’s safe in certain situations, but you really need to have a good handle on exactly what operations you’ll be performing.

    Note that it’s not the 128-bitness of decimal which is helping you here – it’s the representation of numbers as floating decimal point values rather than floating binary point values. See my articles on .NET binary floating point and decimal floating point for more information.

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

Sidebar

Related Questions

I read somewhere that one should never use error conditions as normal program flow.
Despite this being one of the best error messages I've ever seen (second only
One of our programs is sometimes getting an OutOfMemory error on one user's machine,
I have one user who gets an error message when he closes his browser.
In what situations should one catch java.lang.Error on an application?
I got a very similar error to the one below: How can I fix
I'm writing a global error handling module for one of my applications. One of
Due to repetitive errors with one of our Java applications: Engine engine_0: Error in
I just spent half an one our to find out what caused the Error-Message
Which one should I use? catch (_com_error e) or catch (_com_error& e)

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.