13

It is ok to do this?

double doubleVariable=0.0;
if (doubleVariable==0) {
   ...
}

Or this code would suffer from potential rounding problems?

Nestor
  • 13,706
  • 11
  • 78
  • 119
  • the rounding problem would only happen if you were trying to get a double result from dividing two integers – The Lazy Coder Apr 14 '11 at 05:13
  • 1
    I don't agree with that. if (x=0.1) ... has rounding problems. – Nestor Apr 14 '11 at 05:18
  • 1
    That's because 0.1 does not have an exact representation as a binary floating-point number. A decimal type may better suit your needs (x=0.1M) – Eric Mickelsen Apr 14 '11 at 05:24
  • @Eric: I understand the issue. But I've seen code doing x==0 or x==1 and I wanted to make sure that's fine (for x being a double). – Nestor Apr 14 '11 at 05:32
  • possible duplicate of [Comparing double values in C#](http://stackoverflow.com/questions/1398753/comparing-double-values-in-c-sharp) – Dzyann Jul 27 '15 at 15:36

6 Answers6

8

Nope it's perfectly legal if you are only going to compare against 0 as the right side of comparison will automatically casted to double. On the other hand, it would yield all the round-off errors if you where to compare against == 0.10000001

You are better or reading the discussion about float to 0 comparison here: Is it safe to check floating point values for equality to 0?

Also this discussion is very informative about weird precision problems on floats: Why the result is different for this problem?

i.e. below will yield false:

double d1 = 1.000001; double d2 =0.000001;
Console.WriteLine((d1-d2)==1.0);
Community
  • 1
  • 1
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
3

What you have there is 0, which is an integer literal. It is implicitly converted to a double which you could represent with the double literal 0.0 (implicit conversion). Then there is a comparison between the two doubles. A rounding error could cause doubleVariable to not be equal to 0.0 (by some other math you might do, not just setting it), but there could never be a rounding error when converting the integer 0 to double. The code you have there is totally safe, but I would favor == 0.0 instead.

Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41
2

Try:

if (double.Equals(doubleValue, 0.0)){}
vydriani
  • 29
  • 1
  • 6
    This question has been asked three years ago, and your post isn't very detailed. Maybe you can improve your answer by adding details and outlining the solution. Please also take a __[tour]__ and see [how to answer](http://stackoverflow.com/help/how-to-answer). – Unihedron Sep 05 '14 at 14:58
1

If you're just comparing a double variable against 0.0 (or 0), I believe it's safe to do it that way because I think 0 can be represented exactly in floating point, but I'm not 100% sure.

In general, the suggested approach for comparing floating point numbers is to choose a "delta" value at which you'll consider two doubles to be equal if their difference is less than the delta. This handles exact representation limitations with floating point numbers.

double first = 1.234;
double second = 1.2345;
double difference = Math.Abs(first - second);

double threshold = 0.000001; // doubles are equal if their difference is less than this value - you choose this value based on your needs
bool areEqual = difference < threshold;
Andy White
  • 86,444
  • 48
  • 176
  • 211
1

hmm... I think as long as the number has an exact binary fraction representation (like 0) the comparison is perfectly valid.

Nestor
  • 13,706
  • 11
  • 78
  • 119
0

You should not use double for such comparision. double creates problem.
e.g double n1=0.55 double n2=100 then double ans=n1*n2 should be 55.0
but when you debug ans is 55.000000000000007. if(ans==55.0)
will fail. in such case you can face a problem.

EmersioN
  • 91
  • 4