Floating Point Arithmetic in C#

Shervan360 1,661 Reputation points
2021-12-16T23:57:02.627+00:00

Hello,

I saw a video tutorial who made in 2014. I wrote the same code in Visual Studio 2022 with C# 10 and I have a true output. but in this movie output is false.
Does have any change occur in floating-point calculation between 2014 and 2022 in .NET?

using System;  
  
namespace ConsoleApp1  
{  
    public class Program  
    {  
        public static void Main(string[] args)  
        {  
            float x = 5.05f;  
            float y = 0.95f;  
  
            Console.WriteLine(x + y);  
            Console.WriteLine("x + y == 6?" + (x + y == 6.0f)); // in video (made in 2014) is false but in C# 10 is true.  
        }  
    }  
}  

158330-screenshot-2021-12-16-185151.jpg

158320-screenshot-2021-12-16-185121.jpg

Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. WayneAKing 4,931 Reputation points
    2021-12-18T10:47:12.563+00:00

    In addition to the x86 and x64, the .NET version
    seems to be important.

    Comparing floating point numbers for equality has always
    been discouraged, and there are many references which
    explain why and how to correctly handle such
    requirements.

    https://floating-point-gui.de/errors/comparison/

    https://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison

    Single.Equals Method
    https://learn.microsoft.com/en-us/dotnet/api/system.single.equals?view=net-6.0

    "The Equals method should be used with caution,
    because two apparently equivalent values can be
    unequal due to the differing precision of the
    two values. The following example reports that
    the Single value .3333 and the Single returned
    by dividing 1 by 3 are unequal."

    ...

    "Rather than comparing for equality, one recommended
    technique involves defining an acceptable margin of
    difference between two values (such as .01% of one
    of the values). If the absolute value of the
    difference between the two values is less than
    or equal to that margin, the difference is likely
    to be due to differences in precision and, therefore,
    the values are likely to be equal. The following
    example uses this technique to compare .33333 and 1/3,
    the two Single values that the previous code example
    found to be unequal."

    ...

    "The precision of floating-point numbers beyond the
    documented precision is specific to the implementation
    and version of the .NET Framework. Consequently, a
    comparison of two particular numbers might change between
    versions of the .NET Framework because the precision of
    the numbers' internal representation might change."

    • Wayne
    2 people found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-12-17T16:12:03.207+00:00

    The result may depend on the hardware the test is run on. For performance, c# decided that if the hardware used higher precision, then it would accept the results. Originally extra casting code was used, but optimization may remove this code. The jit compiler is more aggressive.

    This means depending on optimization you will get different for the same code in different parts of the program.

    This is all ok because the spec allows using higher precision then the data type specifies.

    0 comments No comments

  3. Shervan360 1,661 Reputation points
    2021-12-18T08:14:38.273+00:00

    In .NET 5, both x86 and x64 generate true.
    In .NET 4.7, x86 generate false and x64 generate true.

    In addition to the x86 and x64, the .NET version seems to be important.

    158580-47-x86.jpg

    158723-47-x64.jpg

    158724-5-x86.jpg
    158705-5-x64.jpg

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.