Warn for simple equality tests on floats?

4 posts, 2 answers
  1. Clark
    Clark avatar
    2 posts
    Member since:
    May 2014

    Posted 06 Feb 2015 Link to this post

    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
  2. Answer
    Svetlozar
    Admin
    Svetlozar avatar
    269 posts

    Posted 06 Feb 2015 Link to this post

    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.

     
  3. DevCraft banner
  4. Clark
    Clark avatar
    2 posts
    Member since:
    May 2014

    Posted 06 Feb 2015 in reply to Svetlozar Link to this post

    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.
  5. Answer
    Ivan
    Admin
    Ivan avatar
    106 posts

    Posted 11 Feb 2015 Link to this post

    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.

     
Back to Top