Re: CDC Floating point = tests?
- From: "stocksami@xxxxxxxxxxxxx" <stocksami@xxxxxxxxxxxxx>
- Date: 31 Jan 2007 09:51:10 -0800
On Jan 30, 8:54 pm, nos...@xxxxxxxxxxxxx (Richard Maine) wrote:
stocks...@xxxxxxxxxxxxx <stocks...@xxxxxxxxxxxxx> wrote:
On the old CDC machines running EKS you could use a regular if test to
see if two floating point values were equal, and it worked! I've
ported lots of code from EKS to other platforms and I've had to change
those lines of codes many many times.
I'm not sure exactly what your question really is. First, it sounds as
though you are surprised that testing floating point values for equality
can ever work. Well, it can. No special compiler tricks are necessary.
It is a simple operation that any Fortran compiler can do - and can do
correctly. I half wonder whether you might be seeing warning messages
from other compilers and overreacting to them. There are compilers that
will give you a warning for every attempt to compare two floating point
numbers for equality. That is only a warning, suggesting that this might
be a bad idea. It does *NOT* mean that it won't work; it will work.
Whether it is a good idea is a separate matter. Sometimes it is a
perfectly fine idea. Other times it is not.
The other posters have concentrated on the question of why it is
sometimes not a good idea, what can be done about those cases, why the
results of the problematic cases can vary among compilers, and related
matters.
But your question seemed to me to have a hint of suprise that any code
could ever work with floating point equality tests. Perhaps I misread
that hint, but I thought it worth clarifying the sim ple case first. I
have seen people who had perfectly valid and "safe" code, but thought
that they had to change it because the compiler gave them a warning
message. The people did not understand the distinction between a warning
and an error. That might not be the case for you, in which case I
apologize for making a "trivial" point. But it seemed worth making just
in case.
My original question was prompted by results I have gotten on other
platforms, mostly AIX. I have never gotten any warnings, but the very
same code and input files that worked on EKS would fail on AIX because
the IF tests weren't reliable. I investigated and found that on AIX a
number such as 101.1 might have all sorts of internal representations
such as 101.099999999999999999 or 101.100000000000000001 resulting in
erratic results. Previous posts have mentioned that such tests can be
reliable if all of the library functions work similarly in how they
store numbers. The numbers were read in using formatted reads. One
set was from a file and another from internal reads of character
strings. There were also some floating point values that were
hardcoded, such as 0.0.
To get around the problem I replaced all such if tests with a function
call that returned true or false. The function would use a tolerance
value to determine equality.
.
- Follow-Ups:
- Re: CDC Floating point = tests?
- From: Paul van Delst
- Re: CDC Floating point = tests?
- References:
- CDC Floating point = tests?
- From: stocksami@xxxxxxxxxxxxx
- Re: CDC Floating point = tests?
- From: Richard Maine
- CDC Floating point = tests?
- Prev by Date: Re: Huge program, no interfaces, ugh
- Next by Date: Re: What is going on with Lahey?
- Previous by thread: Re: CDC Floating point = tests?
- Next by thread: Re: CDC Floating point = tests?
- Index(es):
Relevant Pages
|