Re: Funny Comparison with Float

Jon Skeet <[email protected]> Thu, 6 Mar 2008 14:29:50 +0000
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
Brady Kelly wrote:
> My colleague has just noticed a funny in comparing a float to a literal
> value.  Where 'version' is a 'float', the expression "version == 1.1"
> evaluates to false, where the comparison "version == 1.1F" does.  Last
> week's thread on floating point maths prompted me to raise the issue here to
> find out from the experts just what happens here.

One point that no-one else has picked up on yet (as far as I've seen) -
what is "version" doing as a binary floating point number anyway?
Assuming it's a version number in a normal kind of way, it should really
be a decimal (or a string).

Binary floating point (float/double) is best used for values which a
measurements of real-world values which have infinite variation -
length, mass etc.

Decimal floating point (the decimal type) is good for values which are
inherently decimal to start with, money being the obvious example.

You may or may not find my short articles about floating point useful:
http://pobox.com/~skeet/csharp/floatingpoint.html
http://pobox.com/~skeet/csharp/decimal.html


Jon

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com