How to compare floating-point values safely in C#

2 Answers

0 votes
/*
====================================================================
 Title: Safe Floating-Point Comparison in C#

 Purpose:
     Demonstrates how to compare floating-point values safely
     using a tolerance (epsilon) instead of the == operator.

 Why not use == ?
     Floating-point values (float and double) are stored as
     binary approximations. As a result, tiny rounding errors
     can occur during calculations.

 Example:
     0.1 + 0.2 != 0.3

     Although mathematically equal, the actual stored values
     may differ by a very small amount.
====================================================================
*/

using System;

class Program
{
    /*
     * Function: ApproximatelyEqual
     *
     * Compares two double values using a tolerance (epsilon).
     *
     * Parameters:
     *     a        - First value
     *     b        - Second value
     *     epsilon  - Maximum acceptable difference
     *
     * Returns:
     *     true  -> Values are considered equal
     *     false -> Values are different
     *
     * This is a simple and commonly used approach for
     * floating-point comparisons.
     */
    static bool ApproximatelyEqual(
        double a,
        double b,
        double epsilon)
    {
        return Math.Abs(a - b) <= epsilon;
    }

    static void Main()
    {
        /*
         * Classic floating-point precision example.
         */
        double x = 0.1 + 0.2;
        double y = 0.3;

        /*
         * Define the tolerance.
         * Choose a value appropriate for your application.
         */
        double epsilon = 1e-9;

        Console.WriteLine("================================================");
        Console.WriteLine(" Safe Floating-Point Comparison in C#");
        Console.WriteLine("================================================");
        Console.WriteLine();

        /*
         * Display many decimal places so that precision
         * differences become visible.
         */
        Console.WriteLine($"x = {x:F17}");
        Console.WriteLine($"y = {y:F17}");
        Console.WriteLine();

        /*
         * BAD PRACTICE:
         * Direct comparison using ==.
         *
         * This may fail even when values should be
         * considered equal.
         */
        if (x == y) {
            Console.WriteLine("Direct comparison (==): Equal");
        }
        else {
            Console.WriteLine("Direct comparison (==): Not Equal");
        }

        Console.WriteLine();

        /*
         * GOOD PRACTICE:
         * Compare using a tolerance.
         */
        if (ApproximatelyEqual(x, y, epsilon)) {
            Console.WriteLine("ApproximatelyEqual(): Equal");
        }
        else {
            Console.WriteLine("ApproximatelyEqual(): Not Equal");
        }
    }
}



/*
run:

================================================
 Safe Floating-Point Comparison in C#
================================================

x = 0.30000000000000000
y = 0.30000000000000000

Direct comparison (==): Not Equal

ApproximatelyEqual(): Equal

*/

 



answered Oct 8 by avibootz
0 votes
using System;

// For financial calculations, prefer decimal instead of double

class Program
{
    static void Main()
    {
        decimal a = 0.1m;
        decimal b = 0.2m;
        decimal result = a + b;
        
        if (result == 0.3m) {
            Console.WriteLine("Exactly equal");
        }
    }
}



/*
run:

Exactly equal

*/

 



answered Oct 8 by avibootz
...