سؤال

This is my function for calculating the inverse sine of a number. It results in segmentation fault for values between 0.51-0.8:

double my_asin(double x)
{
    double sum = x;
    if(x < -1.0 || x > 1.0)
    {
        /* error handling */
    }
    else if(x < -0.5)
    {
        sum = -0.5*PI + my_asin(my_sqrt(1-my_pow(x,2))); // SIG_SEGV
        return sum;
    }
    else if(x > 0.5)
    {
        sum = 0.5*PI - my_asin(my_sqrt(1-my_pow(x,2))); // SIG_SEGV
        return sum;
    }

    /* variable initialization */

    while(my_abs(b - a) > EPSILON2)
    {
        /*code*/
    }
    /* return result */
}

GDB and valgrind both tell me the error occurs in the function my_pow, which does exactly what you think it does so there's no need to post it here. Can you please take a look and point me in the right direction? Thanks so much.

هل كانت مفيدة؟

المحلول

Suppose x is sqrt(2)/2 (about 0.707). Your function recursively calls itself with the parameter sqrt(1-x*x), which is equal to x. This will lead to a stack overflow due to infinite recursion.

The same will happen for other values of x around that value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top