class Test
{
static float member;
static void Main()
{
member = Calc();
float local = Calc();
Console.WriteLine(local==member);
}
static float Calc()
{
float d1 = 2.82323f;
float d2 = 2.3f;
return d1*d2;
}
}
That prints "False". If you change "member" to be a local variable, or
"local" to a member variable, or (say) print out the value of "local"
afterwards, it prints "True".
I tried your code and the result was "true"! I tried release and debug, I
tried different values,
I also tried passing one of the values as parameter I also tried it using
double. It also returned "true".
I don't believe that ever a compison of two floats with the same value will
yield to false.
But I tried the following and the results were exactly as expected:
Console.WriteLine(1.11f==1.11); -> false
Console.WriteLine((float)1.11==1.11); -> false
Console.WriteLine((double)1.11f==1.11); -> false
All three returned false because comparing floating point constant has a
different representation in float and double.
Console.WriteLine((float)1.11==1.11f); -> true
No cast occures here, it seems both are parsed as float literals so it
returns true.
Console.WriteLine((double)1.11f==1.11f); -> true
This one returns true which was not as I expected. shouldn't 1.11==1.11f
the same as ((double)1.11f==1.11f?
Shouldn't there be a loss in precision and result in a false?