help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] custom error handling


From: Max
Subject: Re: [Help-gsl] custom error handling
Date: Sat, 23 Aug 2008 11:39:25 +0800

>2008/8/21 Max <address@hidden>:
>> My simple program runs ok under debug mode, but not ok under release
>> mode.
>
>More info needed. Show us the full program and the full output and all
>error messages.
>
>- Jordi G. H.

Hello Jordi,

Thanks for you your quick reply.

A small program that could reproduce the problem is:

#include <gsl/gsl_roots.h>
#include <gsl/gsl_sf.h>
#include <gsl/gsl_errno.h>

// linking to the library
#ifdef _DEBUG
#       pragma comment(lib, "gsl_d.lib")
#else
#       pragma comment(lib, "gsl.lib")
#endif

//_______________GSL error handler_______________
void my_gsl_error_handler (const char * reason,
                                                   const char * file,
                                                   int line,
                                                   int gsl_errno)
{
        throw gsl_errno;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

struct gz_func_param
{
};

double gz_func (double x, void * params)
{
        return 3 * x;
}

double Test()
{
        gsl_function F;
        gz_func_param params;

        F.function = &gz_func;
        F.params = &params;

        //_____save original handler, install new handler_____
        gsl_error_handler_t *old_handler = 
                gsl_set_error_handler (&my_gsl_error_handler);
        
        const gsl_root_fsolver_type * T
                = gsl_root_fsolver_bisection;
        gsl_root_fsolver * s
                = gsl_root_fsolver_alloc (T);

        int status;
        int iter = 0;           // loop counter
        int max_iter = 100;     // max loops of iteration
        double x_lo = 100, x_hi = 200;          // choose to set a WRONG range
        double r;                       // the root to find

        try
        {
        const double tolerance = 0.001;         // degree
        gsl_root_fsolver_set(s, &F, x_lo, x_hi);
        do
        {
        iter++;
        status = gsl_root_fsolver_iterate (s);

        r = gsl_root_fsolver_root (s);

        x_lo = gsl_root_fsolver_x_lower (s);
        x_hi = gsl_root_fsolver_x_upper (s);

        // control convergence with delta trim criterion
        status = gsl_root_test_delta (x_lo, x_hi,
        tolerance, 0);

        if (x_hi - x_lo < tolerance)
                status = GSL_SUCCESS;
        } while (status == GSL_CONTINUE && iter < max_iter);
        }
        catch(int& gsl_errno)
        {
                std::cout << "GSL error!\n";
        }
        catch(...)
        {
                ASSERT(false);
        }

        gsl_root_fsolver_free(s);

        //______restore the original handler______
        gsl_set_error_handler (old_handler);

        return r;
}

int main()
{
        Test();
        return 0;
}

The error I get under Release mode is Runtime error.

Thanks for your help again.

B/Rgds
Max






reply via email to

[Prev in Thread] Current Thread [Next in Thread]