gpt4 book ai didi

c - 此段错误背后的原因

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:29 26 4
gpt4 key购买 nike

这是我计算一个数的反正弦的函数。它会导致 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 和 valgrind 都告诉我错误发生在函数 my_pow 中,它完全按照您的想法进行操作,因此无需在此处发布。你能看看并指出正确的方向吗?非常感谢。

最佳答案

假设 x 是 sqrt(2)/2(大约 0.707)。您的函数使用等于 x 的参数 sqrt(1-x*x) 递归调用自身。这将导致无限递归导致堆栈溢出。

x 围绕该值的其他值也会发生同样的情况。

关于c - 此段错误背后的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com