Re: Precision issue with Float and Double (C++)



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 if
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!!
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: This calculation is just wrong / computer cant count!
    ... the path about floating point inaccuracy, but most of them can actually be educated about ... Therefore specific programming is required to cater for this. ... By analogy ALL numbers in the 17 decimal places take part in the math so 7 ... what tolerance is GT prepared to accept before black becomes grey or any ...
    (microsoft.public.vc.mfc)
  • Re: demonic problem descriptions
    ... >> do not indicate a floating point number? ... to say that the existing mechanism and notation serves a nearly ... programming languages and is not likely to change any time in the next ... why MUST i care about float ...
    (comp.lang.lisp)
  • Re: why system providing routies will provide different result in C++ and Fortran
    ... > my main occupation is teaching programming ... > to the complete beginner to programming the actual ... even good trig functions can only return closest floating point ... >> Ian Chivers wrote: ...
    (comp.lang.fortran)
  • Re: demonic problem descriptions
    ... I suspect that of all the people who have ever written floating point ... FP hardware wanted inexact math without error bounds calculation by ... Lisp enables you to do many things, ... yes, Lisp is a programmable programming language, but you shouldn't ...
    (comp.lang.lisp)
  • Re: (* 2.4 3) => 7.2000003 WTF?! Lets Fix Lisp! Noob Programming Challenge
    ... Lisp covers up the fact that there is something funky going on under the ... it would be nice if the emerging IEEE decimal floating point ... "real" number representation of programming languages were a base 10 ...
    (comp.lang.lisp)