Re: 12.34f vs (float) 12.34
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 03/15/04
- Next message: JP: "Impersonation and LinkLabels result in SecurityException"
- Previous message: Ignacio Machin \( .NET/ C# MVP \): "Re: Label's without background?"
- In reply to: cody: "Re: 12.34f vs (float) 12.34"
- Next in thread: cody: "Re: 12.34f vs (float) 12.34"
- Reply: cody: "Re: 12.34f vs (float) 12.34"
- Reply: cody: "Re: 12.34f vs (float) 12.34"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 15 Mar 2004 13:56:01 -0000
cody <please_dont.spam.deutronium@gmx.de> wrote:
> > 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"!
Try compiling and running from the command line - and from various
different processor architectures. (Mine is a P4.)
> 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.
So how do you explain the results I'm getting? I absolutely *swear*
that on my box, compiling and running the program I gave from the
command line prints False, and I believe it's allowed to.
> 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.
Sure.
> Console.WriteLine((float)1.11==1.11f); -> true
>
> No cast occures here, it seems both are parsed as float literals so it
> returns true.
Yes.
> 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?
No.
> Shouldn't there be a loss in precision and result in a false?
No. Every float is exactly representable as a double, but not every
double is exactly representable as a float. 1.11f is the float closest
to the exact value 1.11. That close value is exactly representable as a
double - but it isn't the same value as 1.11d, which is the double
closest to the exact value 1.11.
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: JP: "Impersonation and LinkLabels result in SecurityException"
- Previous message: Ignacio Machin \( .NET/ C# MVP \): "Re: Label's without background?"
- In reply to: cody: "Re: 12.34f vs (float) 12.34"
- Next in thread: cody: "Re: 12.34f vs (float) 12.34"
- Reply: cody: "Re: 12.34f vs (float) 12.34"
- Reply: cody: "Re: 12.34f vs (float) 12.34"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|