Discussion:
Error Condition Testing
(too old to reply)
Dan
2008-09-24 22:27:04 UTC
Permalink
I sometime write code as I've shown below. Here, xout, is calculated
within a subroutine or it is assigned using a function result. There
are times when xout cannot be calculated for whatever reason. Thus,
in the subroutine or function, I'll often set xout=HUGE(xout).
Then... later, as shown below, I test to see if xout=HUGE(xout).
Generally, I don't test for equality with real numbers for the normal
reasons. However, I've been doing the test as described here under
the assumption that it is a safe test since HUGE() has a well defined
value.

My question is: Am I safe doing this or might there be pitfalls that
will get me slapped in the face when I'm not looking?
Alternatively, any of you have any ways of handling these error
conditions that you feel are particularly nice and also safe?

REAL :: xin, xout
:::::: assign value to xin ::::::

CALL Evaluate( xin, xout )
or
xout = Evaluate( xin)

IF( xout == HUGE(xout) )THEN
::::: error processing code or whatever
END IF

Thanks, Dan
Dan Nagle
2008-09-24 23:26:19 UTC
Permalink
Hello,
Post by Dan
IF( xout == HUGE(xout) )THEN
::::: error processing code or whatever
END IF
In-band signaling is generally a Bad Idea.
What happens when (not if) your software is generalized
to operate in an enlarged domain?

Use a separate predicate or return a logical variable.
Put not thy faith in magic numbers.
--
Cheers!

Dan Nagle
Richard Maine
2008-09-24 23:39:30 UTC
Permalink
Dan <***@aol.com> wrote:
[using HUGE(X) as a flag value for failure]
Post by Dan
Generally, I don't test for equality with real numbers for the normal
reasons. However, I've been doing the test as described here under
the assumption that it is a safe test since HUGE() has a well defined
value.
My question is: Am I safe doing this...
Yes, I'd say you were safe. The guidance against testing for equality of
reals is good general guidance, but in a case like this, where you set a
specific flag value (be it HUGE or any other value that isn't the result
of a floating point computation), it should be fine. Yes, there are
compilers that will nag you with warning messages about it. Ignoring the
nags is "safe" in cases like this.

I'd say that the biggest potential problem here is the possibility that
the calculation could give HUGE as a result of, say, an overflow and
thus indicate an error even though you didn't explicitly set it. But
then, perhaps you'd consider that an error in its own right anyway.

I'm not generally a big fan of special flag values, usually preferring
to use separate error flag variables for the purpose. If you are going
to do
Post by Dan
CALL Evaluate( xin, xout )
...
Post by Dan
IF( xout == HUGE(xout) )THEN
it seems to me just as simple and more clear to do

call evaluate(xin, xout, error)
...
if (error) then

But then that doesn't work so well with functions as in your
Post by Dan
xout = Evaluate( xin)
as I'm even less a fan of having functions return values in arguments.
I'd probably recast the function as a subroutine if an error return was
a possibility. After all, to me most of the point of functions is to be
able to use them in the middle of expressions, and if your function is
instead invoked in the middle of an expression such as

something = other_stuff + whatever*evaluate(xin)

then you've "lost" your opportunity to test for the error. It is both
too late in that you have already used the "bad" result, plus the
appropriate test on something is now non-trivial. That's probably a
bigger caveat than the others mentioned above, but I also presume it is
obvious. In my view, if a function can't be used in an expression like
this, then there isn't much point in it being a function. But that's
more my personal style choice than a real caveat.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
robin
2008-10-12 12:44:29 UTC
Permalink
Post by Dan
My question is: Am I safe doing this or might there be pitfalls that
will get me slapped in the face when I'm not looking?
Alternatively, any of you have any ways of handling these error
conditions that you feel are particularly nice and also safe?
REAL :: xin, xout
CALL Evaluate( xin, xout )
or
xout = Evaluate( xin)
IF( xout == HUGE(xout) )THEN
::::: error processing code or whatever
END IF
And what happens when HUGE(xout) is produced as a result of
the calculation?

Loading...