Re: Bug in Framework on Double arithmetic...?
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 16 May 2006 11:10:04 -0700
This is not a .NET thing. This is universal across all languages (that
I've ever worked in) and all chipsets (that I've ever worked on).
Floating point numbers cannot be represented exactly. They are all very
accurate approximations. So, as you do arithmetic with them... even
apparently simple arithmetic, you introduce small amounts of error.
There is an entire area of computing, called Numerical Analysis, that
studies how various algorithms affect floating point error (either
accentuating it or damping it). However, at the very least, you can
never count on two floating point quantities that "should be" equal
being in fact equal. As Larry pointed out, if you want to compare
floating point quantities for "equality" you must choose a minimum
tolerance for error (aka an "epsilon") and compare using that.
This is, incidentally, why you should never use floating point (ie
float or double) to represent monetary values. You should always use
the Decimal type. I believe that Decimal is considerably less efficient
than float or double (I could be wrong), but it does make stronger
guarantees about accuracy.
double and float are generally used for mathematical calculations such
as work in engineering or the sciences, statistical analysis, etc.
decimal is more generally used in business, where you can't just "lose
a penny" here and there and get away with it.
.
- References:
- Bug in Framework on Double arithmetic...?
- From: Lionel
- Bug in Framework on Double arithmetic...?
- Prev by Date: Re: Question regarding thread.
- Next by Date: Re: How to clean the content of a strinbuider object
- Previous by thread: Re: Bug in Framework on Double arithmetic...?
- Next by thread: C# setup and deployment question
- Index(es):
Relevant Pages
|