Re: C++ Problems BIS
Posted by
Luigi Ballabio-2 on
URL: http://quantlib.414.s1.nabble.com/C-Problems-BIS-tp2463p2466.html
At 02:27 PM 4/11/03 +0200,
[hidden email] wrote:
>just a little C++ question...
>
>how can I catch mathematical exceptions?
>
>I mean, if I compute log(-1), result is -#IND.0000 (or something like
>this).
Andrea,
you're lucky. On at least one platform (I don't remember which)
when I happened to divide by zero, my programs completely crashed...
The only _portable_ way I can think of for avoiding this is to be defensive
and instead of:
double foo(double x, double y) {
return log(x)/y;
}
write:
double foo(double x, double y) {
QL_REQUIRE(x > 0.0, "negative log argument");
QL_REQUIRE(y != 0.0, "division by zero");
return log(x)/y;
}
the above will catch invalid arguments before they can do any actual harm
and will throw an error you can catch. It's a bit pedantic, but it surely
beats checking errno...
Bye,
Luigi