C++ Problems BIS

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

C++ Problems BIS

andrea.odetti-2
Hi,

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).

I would like an exception to be thrown and catched in my exception handler.
I can check "errno" after every calculation but it is not so pretty...

any idea?



Reply | Threaded
Open this post in threaded view
|

Re: C++ Problems BIS

Rod Pienaar
I have never written my own code for this but I extracted the following information from the Borland help file.  They talk about creating error handers based on custom defined _matherr or _matherrl.  I hope that I have not contravened any of their copyright laws by posting this.  I hope it helps.

_matherr and _matherrl are useful for information on trapping domain and range errors caused by the math functions. They do not trap floating-point exceptions, such as division by zero. See signal for information on trapping such errors.  You can define your own _matherr or _matherrl routine to be a custom error handler (such as one that catches and resolves certain types of errors); this customized function overrides the default version in the C library. The customized _matherr or _matherrl should return 0 if it fails to resolve the error, or nonzero if the error is resolved. When _matherr or _matherrl return nonzero, no error message is printed and the global variable errno is not changed.




                                                                                                                                                     
                      [hidden email]                                                                                                          
                      Sent by:                               To:       [hidden email]                                          
                      [hidden email]        cc:                                                                                    
                      ceforge.net                            Subject:  [Quantlib-users] C++ Problems BIS                                            
                                                                                                                                                     
                                                                                                                                                     
                      11/04/2003 13:27                                                                                                              
                                                                                                                                                     
                                                                                                                                                     





Hi,

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).

I would like an exception to be thrown and catched in my exception handler.
I can check "errno" after every calculation but it is not so pretty...

any idea?



-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger
for complex code. Debugging C/C++ programs can leave you feeling lost and
disoriented. TotalView can help you find your way. Available on major UNIX
and Linux platforms. Try it free. www.etnus.com
_______________________________________________
Quantlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users





--

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.




Reply | Threaded
Open this post in threaded view
|

Re: C++ Problems BIS

Rod Pienaar
In reply to this post by andrea.odetti-2
I found an excellent posting on Google that discusses this at length.  Have a look its an interesting read.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=EzwT5.39687%241c.580162%40e420r-sjo2.usenetserver.com&rnum=2

Rgds
Rod



                                                                                                                                       
                      Rod Pienaar                                                                                                      
                                               To:      [hidden email]                                          
                      11/04/2003 13:48         cc:                                                                                    
                                               Subject: Re: [Quantlib-users] C++ Problems BIS(Document link: Rod Pienaar)              
                                                                                                                                       



I have never written my own code for this but I extracted the following information from the Borland help file.  They talk about creating error handers based on custom defined _matherr or _matherrl.  I hope that I have not contravened any of their copyright laws by posting this.  I hope it helps.

_matherr and _matherrl are useful for information on trapping domain and range errors caused by the math functions. They do not trap floating-point exceptions, such as division by zero. See signal for information on trapping such errors.  You can define your own _matherr or _matherrl routine to be a custom error handler (such as one that catches and resolves certain types of errors); this customized function overrides the default version in the C library. The customized _matherr or _matherrl should return 0 if it fails to resolve the error, or nonzero if the error is resolved. When _matherr or _matherrl return nonzero, no error message is printed and the global variable errno is not changed.




                                                                                                                                                     
                      [hidden email]                                                                                                          
                      Sent by:                               To:       [hidden email]                                          
                      [hidden email]        cc:                                                                                    
                      ceforge.net                            Subject:  [Quantlib-users] C++ Problems BIS                                            
                                                                                                                                                     
                                                                                                                                                     
                      11/04/2003 13:27                                                                                                              
                                                                                                                                                     
                                                                                                                                                     





Hi,

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).

I would like an exception to be thrown and catched in my exception handler.
I can check "errno" after every calculation but it is not so pretty...

any idea?



-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger
for complex code. Debugging C/C++ programs can leave you feeling lost and
disoriented. TotalView can help you find your way. Available on major UNIX
and Linux platforms. Try it free. www.etnus.com
_______________________________________________
Quantlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users







--

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.




Reply | Threaded
Open this post in threaded view
|

Re: C++ Problems BIS

Luigi Ballabio-2
In reply to this post by andrea.odetti-2
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



Reply | Threaded
Open this post in threaded view
|

Re: C++ Problems BIS

Kris .
In reply to this post by andrea.odetti-2
Another idea is to use DBC
http://www.eventhelix.com/RealtimeMantra/Object_Oriented/design_by_contract.htm

You could use assertions and conditional compilations to build robust code.


----- Original Message -----
From: "Rod Pienaar" <[hidden email]>
Date: Fri, 11 Apr 2003 13:48:16 +0100
To: [hidden email]
Subject: Re: [Quantlib-users] C++ Problems BIS

>
> I have never written my own code for this but I extracted the following information from the Borland help file.  They talk about creating error handers based on custom defined _matherr or _matherrl.  I hope that I have not contravened any of their copyright laws by posting this.  I hope it helps.
>
--
__________________________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup



Reply | Threaded
Open this post in threaded view
|

Re: C++ Problems BIS

Luigi Ballabio-2
At 11:10 AM -0500 4/11/03, Kris . wrote:
>Another idea is to use DBC
>http://www.eventhelix.com/RealtimeMantra/Object_Oriented/design_by_contract.htm
>
>You could use assertions and conditional compilations to build robust code.

Which also was my suggestion---but probably I phrased it wrongly.
I wasn't suggesting that you reimplemented log, sqrt and such adding
range checking; I merely suggested that you checked the arguments
passed to *your* functions. An example from the library:

SingleAssetOption::SingleAssetOption(Option::Type type, double underlying,
                                      double strike, Spread dividendYield,
                                      Rate riskFreeRate, Time residualTime,
                                      double volatility)
: underlying_(underlying), payoff_(type, strike),
dividendYield_(dividendYield),
   residualTime_(residualTime), hasBeenCalculated_(false),
   rhoComputed_(false), dividendRhoComputed_(false),
   vegaComputed_(false), thetaComputed_(false) {
     QL_REQUIRE(strike > 0.0,
                "SingleAssetOption::SingleAssetOption : strike ("+
                DoubleFormatter::toString(strike)+
                ") must be positive");
     QL_REQUIRE(underlying > 0.0,
                "SingleAssetOption::SingleAssetOption : underlying ("+
                DoubleFormatter::toString(underlying)+
                ") must be positive");
     QL_REQUIRE(residualTime > 0.0,
                "SingleAssetOption::SingleAssetOption : residual time ("+
                DoubleFormatter::toString(residualTime)+
                ") must be positive");
     //! Checks on volatility values are in setVolatility
     setVolatility(volatility);
     //! Checks on the risk-free rate are in setRiskFreeRate
     setRiskFreeRate(riskFreeRate);
}

The three preconditions (QL_REQUIRE) will prevent math exceptions to
occur later, for example, in:

double EuropeanOption::theta() const {
     return -underlying_ * NID1() * volatility_ *
            dividendDiscount()/(2.0*QL_SQRT(residualTime_)) +
            dividendYield_*underlying_*alpha()*dividendDiscount() -
            riskFreeRate_*payoff_.strike()*riskFreeDiscount()*beta();
}

where the square root of the residual time is taken. Moreover, and
possibly more importantly, they will greatly aid your diagnosis of
the error by telling you exactly what the problem is, and exactly at
the time when the problem is introduced---two things that _matherr
cannot do, since it has to be a generic error handling routine. In
short, it's a much robust way of coding---I'm sure the link above
will explain it better than I did.

Bye,
        Luigi