Messages in this thread | | | Date | Thu, 25 Apr 2002 10:22:49 -0400 (EDT) | From | "Richard B. Johnson" <> | Subject | Re: FPU, i386 |
| |
On Thu, 25 Apr 2002, rpm wrote:
> On Wednesday 17 April 2002 08:10 pm, Jesse Pollard wrote: > > --------- Received message begins Here --------- > > > > > if (int(1/h * 100) == int(5.0 * 100)) > > > > will give a "proper" result within two decimal places. This is still > > limited since there are irrational numbers within that range that COULD > > still come out with a wrong answer, but is much less likely to occur. > > > > Exact match of floating point is not possible - 1/h is eleveated to a > > float. > > > > If your 1/h was actually num/h, and num computed by summing .01 100 times > > I suspect the result would also be "wrong". > > > > why is exact match of floating point not possible ?
Because many (read most) numbers are not exactly representable in floating-point. The purpose of floating-point it to represent real numbers with a large dynamic range. The trade-off is that few such internal representations are exact.
As a simple example, 0.33333333333..... can't be represented exactly even with paper-and-pencil. However, as the ratio of two integers it can be represented exactly, i.e., 1/3 . Both 1 and 3 must be integers to represent this ratio exactly.
All real numbers (except trancendentials) can represented exactly as the ratio of two integers but floating-point uses only one value, not two integers, to represent the value. So, an exact representation of a real number, when using a single variable in a general-purpose way, is, for all practical purposes, not possible. Instead, we get very close.
When it comes to '==' close is not equal. There are macros in <math.h> that can be used for most floating-point logic. You should check them out. If we wanted to check for '==' we really need to do something like this:
double a, b; some_loop() { if(fabs(a-b) < 1.0e-38) break; } Where we get the absolute value of the difference between two FP variables and compare against some very small number.
To use the math macros, the comparison should be something like:
if (isless(fabs(a-b), 1.0e-38)) break;
Cheers, Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Windows-2000/Professional isn't.
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
| |