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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:33:24+00:00 2026-05-28T04:33:24+00:00

Description This is not a real world example! Please don’t suggest using decimal or

  • 0

Description

This is not a real world example! Please don’t suggest using decimal or something else.

I am only asking this because I really want to know why this happens.

I recently saw the awesome Tekpub Webcast Mastering C# 4.0 with Jon Skeet again.

On episode 7 – Decimals and Floating Points it is going really weird and even our
Chuck Norris of Programming (aka Jon Skeet) does not have a real answer to my question.
Only a might be.

Question: Why did MyTestMethod() fail and MyTestMethod2() pass?

Example 1

[Test]
public void MyTestMethod()
{
    double d = 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;

    Console.WriteLine("d = " + d);
    Assert.AreEqual(d, 1.0d);
}

This results in

d = 1

Expected: 0.99999999999999989d
But was: 1.0d

Example 2

[Test]
public void MyTestMethod2()
{
    double d = 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;
    d += 0.1d;

    Console.WriteLine("d = " + d);
    Assert.AreEqual(d, 0.5d);
}

This results in success

d = 0,5

But why ?

Update

Why doesn’t Assert.AreEqual() cover that?

  • 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-28T04:33:24+00:00Added an answer on May 28, 2026 at 4:33 am

    Okay, I haven’t checked what Assert.AreEqual does… but I suspect that by default it’s not applying any tolerance. I wouldn’t expect it to behind my back. So let’s look for another explanation…

    You’re basically seeing a coincidence – the answer after four additions happens to be the exact value, probably because the lowest bit gets lost somewhere when the magnitude changes – I haven’t looked at the bit patterns involved, but if you use DoubleConverter.ToExactString (my own code) you can see exactly what the value is at any point:

    using System;
    
    public class Test
    {    
        public static void Main()
        {
            double d = 0.1d;
            Console.WriteLine("d = " + DoubleConverter.ToExactString(d));
            d += 0.1d;
            Console.WriteLine("d = " + DoubleConverter.ToExactString(d));
            d += 0.1d;
            Console.WriteLine("d = " + DoubleConverter.ToExactString(d));
            d += 0.1d;
            Console.WriteLine("d = " + DoubleConverter.ToExactString(d));
            d += 0.1d;        
            Console.WriteLine("d = " + DoubleConverter.ToExactString(d));
        }
    }
    

    Results (on my box):

    d = 0.1000000000000000055511151231257827021181583404541015625
    d = 0.200000000000000011102230246251565404236316680908203125
    d = 0.3000000000000000444089209850062616169452667236328125
    d = 0.40000000000000002220446049250313080847263336181640625
    d = 0.5
    

    Now if you start with a different number, it doesn’t work itself out in the same way:

    (Starting with d=10.1)

    d = 10.0999999999999996447286321199499070644378662109375
    d = 10.199999999999999289457264239899814128875732421875
    d = 10.2999999999999989341858963598497211933135986328125
    d = 10.39999999999999857891452847979962825775146484375
    d = 10.4999999999999982236431605997495353221893310546875
    

    So basically you happened to get lucky or unlucky with your test – the errors cancelled themselves out.

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

Sidebar

Related Questions

Uhm I'm not sure if anyone has encountered this problem a brief description is
I have something like this: [Description(Sets the color.), Category(Values), DefaultValue(Color.White), Browsable(true)] public Color MyColor
Does anyone see a problem with this, its not working saying bad file descriptor
After reading this description of late static binding (LSB) I see pretty clearly what
FYI, There is some overlap in the initial description of this question with a
How to describe this in description logic? every human is either male or female
I have an enum with Description attributes like this: public enum MyEnum { Name1
keyword = house SELECT * FROM products WHERE description LIKE '%house%' This query also
I have a query which produces a list of orders like this: Invoice Description
Expanding this question on how I learnt to pass from problem description to code

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.