Re: Precision issue with Float and Double (C++)
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 28 Aug 2008 13:14:57 -0400
Rule 1: There is no such thing as "decimal precision" in floating point
Rule 2: If you are having problems with "decimal precision", consult rule 1
Rule 3: If are convinced double should work like decimal arithmetic, consult Rule 1.
Rule 4: If you still believe double should work like decimal arithmetic, choose another
profession.
The results you are seeing are absolutely correct, but you are harboring the delusion that
double is decimal and should exhibit the same behavior.
You can download my Floating Point Explorer from my MVP Tips site and prove that it is not
possible to get the representations you want.
If the error is "significant enough to affect my calculation" then your calculation is not
designed correctly. One of the first things we teach students in computing is that you
must *not ever* create a computation which is sensitive to the binary representation
issues of floating point, and in fact, numerical analysts have known about this problem
since the 1940s (John von Neumann, the inventor of the modern stored-program computer,
identified all of these issues AND how to deal with them, before I was born, which was
1947, and they were known to a lesser degree in the 1920s with mechanical calculators
(decimal arithmetic has EXACTLY the same problems), and for all I know the ancient Chinese
and Greek mathematicians knew about them).
My first programming exercise in the course I took in numeric programming in the summer of
1967 was an algorithm that was guaranteed to converge if the starting point was greater
than 1/2 the interval between two points. With a pencil and paper you could show it
converging in about four iterations. Then we wrote a FORTRAN program that failed to
converge, because of the roundoff error; divide-by-2 came out a tiny bit smaller than 1/2
the interval. Then we had to learn how to deal with floating point precision. (We had
spent six weeks doing numerical algorithms without programming, doing the theory of
numerical algorithms. Then we spent six weeks studying why none of the theoretical models
worked when confronted with real floating point computations).
The workaround is to design your code to work with binary floating point representations,
and to do this, I would suggest any good introductory text on numerical programming.
"Numerical Recipes in C" (see http://www.nr.com/) would be a good start. There is no
"simple fix" if you are having what are known as "LSB errors" (Least Significant Bit).
Note that if you do not handle these properly, your Jupiter probe will miss the planet by
several million miles. So they *can* be handled, but naive attempts to simulate decimal
arithmetic are doomed.
joe
On Thu, 28 Aug 2008 02:22:01 -0700, Ranjith <Ranjith@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I am facing a precision issue with data types float and double.In short ifJoseph M. Newcomer [MVP]
you initialize as follows:
float flTest = 0.05;
double dblTest = 0.05;
internally this means flTest = 0.0500001
and dblTest= 0.050000000000000003
the error introduced is significant enough to effect my calculation.
Is there a way to discard all numbers beyond nth position after the decimal
point. In this particular case i need an accuracy of 10 lakh ie it should
show accurately till atleast 19.99999
I am using VS2005. Please suggest any work around.
Thanks in Advance!!
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Precision issue with Float and Double (C++)
- From: David Webber
- Re: Precision issue with Float and Double (C++)
- Prev by Date: Re: I know i screwed it up somewhere!
- Next by Date: Re: Precision issue with Float and Double (C++)
- Previous by thread: Re: Precision issue with Float and Double (C++)
- Next by thread: Re: Precision issue with Float and Double (C++)
- Index(es):
Relevant Pages
|