code review - Comparing Doubles in Visual Studio - a standard way to catch this? -


People,

Even experienced programmers sometimes write C # code like this:

  Double X = 2.5; Double y = 3; In fact, it is common knowledge that two pairs (or floats) can never be equal to one another, if it is (x + 0.5 == 3) {// it will never be executed}  

The way the computer handles floating point arithmetic

The problem is, everyone knows this, but in this way the code is still everywhere, ignoring it so much

The question for you:

  • You have with you in your development organization Have treated?
  • Is it common that the compiler should know that we all should really shout for VS-2010 so that if comparing two doubles / floats, then compiled time Include warning?

Update : People, thanks for the comments. I want to make it clear that I think most of the code above is wrong. Yes, you do not want to compare == doubles and floats at any time. Instead, you should use Epsilon-based comparison. Something is obvious. The real question here is "how can you decide on this problem", "how do you solve the technical issue"

Definitely point values ​​can be equal to one another, and if you have given them they will always be You should never compare equality using about equals, but why you need to understand it - and The example you have shown is not appropriate.

I do not think this is something that the compiler should give a necessary warning, but you would like to see if there is something that can be picked up on FxCop. I can not see it in, but it can be anywhere ...

Personally, I have full faith that enabled developers will find it in the code review, but it trusts you To start with, review a code, it depends on your developers when using double and when decimals are used, what I have found Often it is not ...


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -