Can anyone show me an example of two C# variables containing float values that "seem" to be equal but in fact are not. When I say "seem equal", what I mean is that they intuitively seem to be equal.
The reason why I'm looking for such an example is because I have code that compares two float variables for equality and Visual Studio is warning me that Comparison of floating point numbers can be unequal due to the differing precision of the two values.
. I understand that float variables are not precise (here's a StackOverflow question where this is discussed and explained very clearly) but I am failing to find an actual example where two values that seem to be equal are actually considered different by C#.
For instance, the first answer to the SO question I referenced earlier mentions that 9.2
and 92/10
are internally represented differently so I wrote the following code to verify if C# would treat them as equal or not and the result is that they are considered equal.
var f1 = 92f / 10f;
var f2 = 9.2f;
if (f1 == f2)
{
Console.Write("Equal, as expected");
}
else
{
Console.Write("Surprisingly not equal");
}
So, I'm looking for an example of f1
and f2
that "seem" to be equal but would cause C# to treat them as different.