3

Possible Duplicate:
What is the difference between Decimal, Float and Double in C#?

Help me.

I am developing a application in C# . I am trying:

DateTime dtm1 = new DateTime(2012, 11, 15, 11, 3, 0);
DateTime dtm2 = new DateTime(2012, 11, 15, 11, 3, 20);
TimeSpan timespan3 = dtm2 - dtm1;
decimal _Hour = Convert.ToDecimal(timespan3.TotalHours);

When do such me with output as follows:

 _Hour = 0.00555555555555556M

and the which is not exactly, when using is a type of double it for output:

 double _Hour = timespan3.TotalHours;

 output: 0.0055555555555555549

One for example:

public decimal tinhDienTichHinhThang(decimal D1, decimal D2, decimal H)
        {
            //tính tổng 2 đáy
            decimal tong2Day = D1 + D2;
            //cộng vào nhân chiều cao :))
            tong2Day = tong2Day * H;

            //return diện tích
            return tong2Day / 2;
        }


DateTime dtm1 = new DateTime(2012, 11, 15, 11, 3, 0);
DateTime dtm2 = new DateTime(2012, 11, 15, 11, 3, 20);
TimeSpan timespan3 = dtm2 - dtm1;

///progress
///cal1: _Hour
///cal2: decimal D1 = 0.25
///cal3: decimal D2 = 5
///cal4: decimal D3 = 0.9

decimal test1 = (decimal test1 = Math.Round((D1 + tinhDienTichHinhThang(D2, 0, Convert.ToDecimal(timespan3.TotalHours))) * D3, 3, MidpointRounding.AwayFromZero);

output: test1 = 0.237

if calculate manual:

test1 =  Math.Round((0.25 + ((5+0)*timespan3.TotalHours/2))*0.9, 3, MidpointRounding.AwayFromZero);

output:test1 = 0.238 (exactly: 0.2375)

Note: Calculate win XP then exactly: 0.2375 But calculate win 7 then not exactly.

Please interpret cho me the problem that why and resolve this problem with the way?

Community
  • 1
  • 1
  • 4
    What exactly are you trying to achieve to start with? In my experience with date and time, if you're using `double` **or** `decimal` you're probably going about things the wrong way. – Jon Skeet Dec 14 '12 at 07:40

3 Answers3

1

Doubles are floating point values. They are still limited to 64 bits of precision, but they have a value and an exponent that value is raised to (essentially scientific notation.) That's why they have more range than ints or longs, but because of this extended range they are unable to represent every possible value in that range.

Decimals have more bits than a double, so you're just seeing side effects of double's less precision compared to decimal when you are converting the value.

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
  • Thanks Zach Johnson. But window XP exactly, win 7 not excactly. In my example it gives 2 different results when using Round respectively 0.238 and 0.237 – user1903252 Dec 14 '12 at 07:41
  • @user1903252 just taking a stab at this in the dark, but it's possible that you're seeing the difference between floating point arithmetic implemented in hardware vs software. – Zach Johnson Dec 14 '12 at 07:52
  • Controlling you to say that is sao? You way that resolve are the problem like me say are not? – user1903252 Dec 14 '12 at 08:18
  • @user1903252 sorry, I don't really understand what you mean? A little investigation tells me you might be translating from Vietnamese? (the non-English word "sao" in here and "cho" in the question.) Unfortunately something is getting lost in translation. – Zach Johnson Dec 14 '12 at 08:27
  • 1
    Doubles have 53 bits of precision, not 64. Decimals are held in decimal radix, not binary, and they are 'more accurate' in decimal places for that reason, not just because of having extra bits. Your statement about hardware vs software is mere guesswork. -1 – user207421 Dec 14 '12 at 20:15
0

I'm not sure what is your problem. But if you have a problem with different values in decimal and double, it is by design. Numbers with floating point by default is not precize.

Update: (answer to your question in answers)

Due to this your question in answers, first and second statements different:

cause

5M * Convert.ToDecimal(timespan3.TotalHours)/ 2M

calculations is in decimal, but in second:

5 * timespan3.TotalHours / 2

calculations are in double (cause timespan3.TotalHours is double). It means that the result can be different from first statement. But at the and of calculation, you cast it to decimal and it implicitly convert value to decimal, that can change value.

In third statement, you have integer-valued calculations too, but double is floating point type, so it's value can be different from second statement, where fixed point type decimal.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
  • But window XP exactly, win 7 not excactly. In my example it gives 2 different results when using Round respectively 0.238 and 0.237 – user1903252 Dec 14 '12 at 07:47
0
decimal test1 = (0.25M + 5M * Convert.ToDecimal(timespan3.TotalHours) / 2M) * 0.9M;

decimal test2 = (decimal)((0.25 + 5 * timespan3.TotalHours / 2) * 0.9);

double test3 = (0.25 + 5 * timespan3.TotalHours / 2) * 0.9;

When to perform do such they ra the following output not the same. You can guide to resolve this problem are no?