Re: 12.34f vs (float) 12.34

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 03/15/04


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


Relevant Pages

  • Re: Large two-column floats at the bottom of the page ?
    ... I NEED to put a large float at the bottom of the page, ... columns document, using either a \figure* or a \table*. ... The \bottomfraction command does not affect starred commands ...
    (comp.text.tex)
  • RE: Decimal Arith in DCL was(RE: Floating point arithmetic support in DCL)
    ... < support in DCL) ... interpreter that uses float, decimal yes, but float no. ... Or do you not consider this a command line ... < Alan E. Feldman ...
    (comp.os.vms)
  • pyparsing and svg
    ... ## couple:: number dot number comma number dot number ... from pyparsing import Word, Literal, alphas, nums, Optional, OneOrMore ... couple = float + comma + float ...
    (comp.lang.python)
  • Re: Figure inside itemize or enumerate environment
    ... I've added a command to easily put an image insie my code. ... every figure has at the bottom and top a line ... , then they should *not* float, and you would be perfectly correct in expecting the width to be reduced in a list. ...
    (comp.text.tex)
  • Re: problem with cast and unions
    ... my aim is to make a library compiling with suncc. ... The part where there is that problem is about conversion from a C float ... to a Small "cell" (actually an int). ... contain a valid representation of an 'int'. ...
    (comp.lang.c)