comparing doubles can lead to hard to find bugs due to problems with precision, so I would like to find all the lines in my code where I use a line like
doubleVariable = SomeDoubleExpression
doubleVariable = doubleVariable
and replace them with
doubleVariable.Equals( SomeDoubleExpression )
Is there a way to do this?
Without writing an entire parsing app that gathers all the variables and properties to determine their types and looks for them within code, you can certainly find the assignment statements with a regular expression. Something like this:
That will actually find more than you need since it will also flag things like
if a =
If you already know which doubles you are looking for, then:
The same disclaimer about If statements applies.
In the menu go to Project > Analysis Warnings…
make sure “Performing a Item1 comparison on floating-point values can yield unexpected results due to their inexact binary representation.” is checked and they’ll appear in Analyze Project results.
Great reminder of an often overlooked (at least by me) feature, Will!