Re: Float comparison



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
.



Relevant Pages

  • Re: Float point comparison-Newbie
    ... > There is the problem that an floating point is never prezise enough to ... > as this types are too far from to be exact from that in any case. ... implement efficient multiplication of numbers with HUGE numbers of digits. ... Perform error analysis to prove that the floating point ...
    (comp.lang.c)
  • Re: Float comparison
    ... staying within the range where integers can be ... represented exactly, in order to use the greater range of exact ... The only way to determine the range of values represented, whether it is stored as an integer or floating point, IS to use the additional information and do an error analysis. ...
    (comp.lang.c)
  • Re: Cant get Access to run a query with a time field
    ... Since the user enters an exact value, why isn't it saved to the ... Second, a date time value is a floating point value, ... >> Vanderghast, Access MVP ...
    (microsoft.public.access.queries)
  • Re: Wrong results when comparing negative double variables in an if statement
    ... debugging purposes, not because floating-point numbers have hundreds ... >The output as copied from the emulated DOS window is: ... There is no exact value of 0.001 in binary floating point, ...
    (comp.lang.c)
  • Re: for problem...
    ... DC>>Removing the braces should not make any difference. ... since '0.01' is never an exact value. ... in the *nature* of floating point operations. ... with the roundoff error 'contained' ...
    (comp.lang.tcl)