This is a migrated thread and some comments may be shown as answers.

Warn for simple equality tests on floats?

3 Answers 36 Views
Code Analysis
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Clark
Top achievements
Rank 1
Clark asked on 06 Feb 2015, 04:45 PM
Is there a way, using JustCode or VS, to identify and flag instances in which there is a direct, simple equality test between two floating point values?

Such comparisons are problematic. See, for instance, http://stackoverflow.com/questions/14844659/comparing-float-and-double-values-with-delta

As that article points out, >= and <= include within themselves an equality test.

My specific environment is VB.NET, .NET Framework 4.5, VS2013.

chw

3 Answers, 1 is accepted

Sort by
0
Accepted
Svetlozar
Telerik team
answered on 06 Feb 2015, 05:23 PM
Hi,

Thank you for your feedback!

JustCode doesn't have that warning built-in, but it is easy to add that functionality using a JustCode extension.

Here is a sample project where we have demonstrated how to build a JustCode extension - justcode-extensions

I also prepared a sample for you - gist

If you have other questions, please don't hesitate to write back!

Regards,
Svetlozar
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Clark
Top achievements
Rank 1
answered on 06 Feb 2015, 09:22 PM
First, I was able to get the sample extension to function and it successfully identified cases of comparisons of floating-point values. This, of course, made my day.

Second, the extension didn't recognize the original case that set me off on my crusade because that case was written not as "A [operator] B" but as "A.Equals(B)". If you have a way to check that variant, it would be a great help.

If there is an object model I should go look at, point me at it.
0
Accepted
Ivan
Telerik team
answered on 11 Feb 2015, 05:34 PM
Hi,

I am glad that the extension is helping you identify possible issues with your code.
In order to modify it so that it can detect "Equals" method as well you need to add the following code to the "AddCodeMarkers" method:
foreach (var methodInvocation in fileModel.All<IMethodInvocation>().Where(x => x.Language.AreNamesEqual(x.UsedMember.Identifier.Name, "Equals") && x.Arguments.Count() == 1))
{
    var leftExpressionIsFloatingPoint = methodInvocation.Expression.Type.Is("System.Single") || methodInvocation.Expression.Type.Is("System.Double");
    var argumentType = methodInvocation.Arguments.Arguments.First().Expression.Type;
    var argumentIsFloatingPoint = argumentType.Is("System.Single") || argumentType.Is("System.Double");
    if (leftExpressionIsFloatingPoint && argumentIsFloatingPoint)
    {
        methodInvocation.AddCodeMarker(WarningExampleID, this);
    }
}

This will essentially check all "Equals" methods that accept a single parameter and will check whether you are comparing floating-point values.
If you have any more questions or remarks, please feel free to contact us again! 

Regards,
Ivan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Code Analysis
Asked by
Clark
Top achievements
Rank 1
Answers by
Svetlozar
Telerik team
Clark
Top achievements
Rank 1
Ivan
Telerik team
Share this question
or