C#


CSHARP.ARITH.FPEQUAL : Floating Point Equality (C#)

Summary

A comparison between non-integral values might be unreliable.

Floating point operations are by nature approximated. This can introduce bugs when precise, mathematical properties are expected from inherently imprecise floating point computations. Moreover, computations that might lose precision or overflow, whose result is stored in a larger type, are suspicious since, by computing on the larger type from the beginning, one could avoid approximations and overflows.

Properties

Class Name Floating Point Equality (C#)
Significance reliability
Mnemonic CSHARP.ARITH.FPEQUAL
Categories
CWE CWE:1077 Floating Point Comparison with Incorrect Operator
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Floating Point Equality (C#)"

Example

using System;

namespace DocumentationExamples
{
    public class Approximation
    {

        public static void Main(string[] args)
        {
            for (double d = 0.0; d != 5.0; d += 0.001) // Floating Point Equality (C#) warning issued here 
                                                       // due to floating point approximations, d != 5.0 will always be true and the loop will never terminate 
               Console.WriteLine(d);

            int days = int.Parse(args[0]);
            long milliseconds = days * 24 * 60 * 60 * 1000; // Cast: int Computation to long (C#) warning issued here 
                                                            // - multiplication is performed on integers; only the result is cast to long 
            Console.WriteLine("Inside %d days there are %d milliseconds", days, milliseconds);

        }

    }

}

To resolve the Floating Point Equality (C#) warning, replace d != 5.0 with d < 5.0 or replace floating point computations with (stable and more efficient) integral computations, as follows:

for (int d = 0; d != 5000; d++)
  Console.WriteLine(d / 1000.0);

To resolve the Cast: int Computation to long (C#) warning, perform the multiplication as an operation on long. Note that overflow can still occur if days is sufficiently large.

   int days = Convert.ToInt32(args[0]);
   long milliseconds = days * 24L * 60 * 60 * 1000;
   Console.WriteLine("Inside %d days there are %d milliseconds", days, milliseconds);

Resolution

Use approximated comparisons (up to some epsilon) rather than exact comparisons. Use integral numbers instead of floating point values, whenever possible. Compute over integral numbers and translate into floating points only at the end, whenever possible. If a large type for the result of a computation is desired, compute from the beginning on that type instead of applying a type conversion at the end of the computation.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.