[...]
my problem is that I have a black-box routine which gives me the
results. So I can not check before anything. I can only work with the
results.
If not, of course that the solution would be obvious and I would fix
the program for not to give the "infinity" result.
So, any of you know how to solve this programming puzzle ? Can you
handle the infinity ? ....
The <math.h> header declares a number of classification macros:
fpclassify() yields an int value for a floating-point argument, one of
FP_INFINITE
FP_NAN
FP_NORMAL
FP_SUBNORMAL
FP_ZERO
Other macros, yielding a true or false result, are:
isfinite()
isinf()
isnan()
isnormal()
These are new in C99, so your implementation may or may not support
them. Even if it does, they may or may not work as you want them to;
the standard doesn't require implementations to support NaNs and
Infinities.
If your black-box routine divides by zero internally (and you're not
able to detect this in advance by checking the arguments you feed into
the black box), then strictly speaking it's already invoked undefined
behavior, and you can't reliably do anything about it. But *if* your
implementation supports the classification macros, and *if* a
floating-point division by zero yields Infinity or NaN (NaN for
0.0/0.0, Infinity for nonzero/0.0), then that's probably your best
bet.
Consult your implementation's documentation to find out how it behaves
on floating-point division by zero and whether it supports the
classification macros.
See also section 7.12.3 of the C99 standard; the latest draft is
available at
<
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.