Re: Rounding of the double
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 31 May 2007 16:10:40 -0400
actually, it doesn't store it in hex, it stores it in binary, with an implied high-order
bit. Since a normalized floating-point number ALWAYS has a 1 in the high order position,
that is assumed, and the first mantissa bit we see is actually the SECOND mantissa bit,
since the first bit is always implied.
IBM got into trouble with numerical analysts when they introducted the /360, because it
did store floating point values in hex, that is, the normalization was to 4 bits of
precision (the high-order nybble being nonzero) and there was no implied decimal point, so
it was intrinsically less precise than the binary representations that were in use at the
time.
Otherwise, I agree completely with your points. Floating point always seems to come as
such a surprise to programmers.
In my numerical analysis course, the professor spent 6 weeks telling us how to do
numerical analysis right, then spent the next 6 weeks telling us how all computers did it
wrong and if we wanted to write numerical algorithms we had to take these errors into
account. The theory of numerical analysis assumes essentially infinite precision.
When I was working on my PhD dissertation, I went to one of the foremost numerical
analysts (who was on the CMU faculty at the time) and asked him what techniques should be
used to optimize floating point computations. His answer was "none". The explanation was
that either the code was written by someone who was clueless, in which case optimization
didn't matter, or it was written by a numerical analyst, in which case any transformation
from the original computation would distort the results and introduce significant errors.
"We work with algorithms whose errors are sometimes six orders of magnitude larger than
the result during certain points of the computation, but which then all cancel out,
leaving us with a result to the intended precision". Which made my life easier, because I
ended up with a whole lot of work I didn't need to do.
joe
On Thu, 31 May 2007 18:35:14 +0100, "David Webber" <dave@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Joseph M. Newcomer [MVP]
"Alex" <alsim123@xxxxxxxxxxx> wrote in message
news:1180624216.737392.276140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have double d1 = 80.00
double d2 = 100.00
double result = d1/d2;
result = 0.80000000000000004 !!??
....
Is there any way actually if I divide 80 by 100 to get 0.80 ?
1. Some unpleasant truths about floating point numbers:
(In what follows I'll use an ellipsis to mean recurring indefinitely.)
0.80000000000000004 *is* 0.80
"0.80" means a number between
0.79500000000000...
and
0.80499999999999...
"0.800" means a number between
0.799500000000000...
and
0.800499999999999...
You want the answer
0.8000000000000...
But in fact the numbers you divided were only specified here to two decimal
places, and so technically the answer is not known to that accuracy.
2. To the point: I suspect you are using a convention where you can write
0.80 and assume that this means 0.80000... with zeroes going on for ever.
This is a very dangerous way of looking at things, and runs into deep
trouble in various ways. One of the ways it runs into trouble is that the
computer stores numbers hexadecimally and the number you want (8 tenths +
no hundredths + no thousandths + no ten-thousandths +...) in general has no
particular reason to be represented by a finite string of hexadecimal
digits.
Another problem is when you divide two numbers an inaccuracy can occur in
the final significant figure stored by the computer. It would have to
store an infinite number of digits to avoid this.
The upshot of these and other considerations is that you should *never*
trust the least significant digits of floating point variables. A
corollary is that you should also never test them for equality.
So if you're using floating point, you have no right to make demands on the
least significant figures and you must write your code in a way which
manages them.
Dave
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Rounding of the double
- From: Alex
- Re: Rounding of the double
- From: David Webber
- Rounding of the double
- Prev by Date: Re: test if a string is a valid 'number'?
- Next by Date: Re: Rounding of the double
- Previous by thread: Re: Rounding of the double
- Next by thread: Re: Rounding of the double
- Index(es):
Relevant Pages
|