Re: Float comparison
- From: Flash Gordon <smap@xxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Apr 2009 07:06:50 +0100
CBFalconer wrote:
Keith Thompson wrote:
.... snip ...
Two different computations, due to the limited precision of type
double, yielded exactly the same result. That identical result
was stored in two different objects.
Perhaps the program *tried* to store a value greater than 1.0 in
b, but it did not actually do so. This "real value" you're
talking about just doesn't exist.
Oh, it exists, but not in that floating representation.
The program when running does not work with "real vaus" and then round them to floating point representations. It works with floating point representations only. So the "real value" never exists in the computer.
And that
is how programs accumulate relatively large 'rounding errors'.
Keith has already mentioned the need to do an error analysis if you want to know what the possible error.
You
have to keep track of what is going on.
Earlier you said that you know what the range is *without* examining the code. Which is it?
Watching every little
detail is excessive, and will not be done.
Wrong. Some people spend a long time doing exactly that because it is very important and they are being paid to do it. It is called "Error Analysis" and gets taught in universities. I've done done an approximate error analysis on software that used integer arithmetic (for speed) in the past, and in that program the integers where all storing approximate values (altitude in units of 4 feet, the roll and pitch of an aircraft etc).
Always remembering that
a floating point value represents a known range is very helpful.
Apart from the fact that this is NOT true. You need to analyse th program to know whether it is
I know of code which carefully uses doubles to do integer arithmetic, staying within the range where integers can be represented exactly, in order to use the greater range of exact integral values that a double can represent on that specific implementation than an int. In this case the stored doubles always have an exact number of pennies in them (well, it also uses exact numbers of other things).
Now you have a description of real software in which floating point variables always have exact values stored in them. I'm sure that over the years I've seen other people mention similar things.
Everything you are saying applies to integer arithmetic in C as well, because people DO use integers to hold approximate values (unless you think planes fly at exact multiples of 4 feet and do things like jumping from 80 feet to 84 feet without ever going through the intervening altitudes).
So either integer types cannot store exact values and you need to do a full error analysis to determine the range of values they might represent or floating point variables can store exact values which are exactly the mathematically correct values the program is intended to store.
--
Flash Gordon
.
- Follow-Ups:
- Re: Float comparison
- From: CBFalconer
- Re: Float comparison
- References:
- Float comparison
- From: Alessio Ribeca
- Re: Float comparison
- From: CBFalconer
- Re: Float comparison
- From: Richard Heathfield
- Re: Float comparison
- From: CBFalconer
- Re: Float comparison
- From: Richard Heathfield
- Re: Float comparison
- From: CBFalconer
- Re: Float comparison
- From: Phil Carmody
- Re: Float comparison
- From: CBFalconer
- Re: Float comparison
- From: Keith Thompson
- Re: Float comparison
- From: CBFalconer
- Float comparison
- Prev by Date: Re: Working with char
- Next by Date: Re: Could you please give me some advice on GC in C?
- Previous by thread: Re: Float comparison
- Next by thread: Re: Float comparison
- Index(es):
Relevant Pages
|