gpt4 book ai didi

c - 递归POW : warning: control may reach end of non-void function

转载 作者:行者123 更新时间:2023-11-30 19:01:15 24 4
gpt4 key购买 nike

这是一个递归函数来实现pow。为什么编译器会抛出警告:“警告:控制可能到达非空函数的末尾”

一种解决方案是在函数底部添加'return 0',但覆盖率不会达到100%,那么如何解决这个问题呢?

谢谢!

double pow_recur( double base, int exponent )
{
if (base == 0) {
return 0;
}
if (exponent == 0) {
return 1.0;
} else if (exponent > 0) {
if (exponent % 2 == 0) {
return pow_recur(base * base, exponent / 2);
}
if (exponent % 2 == 1) {
return base * pow_recur(base * base, (exponent - 1) / 2);
}
} else {
return 1.0 / pow_recur(base, -exponent);
}
}

最佳答案

在检查指数是否为偶数时,将其更改为 if-else,警告就会消失。不需要同时检查偶数和奇数,如果不是偶数就一定是奇数。

关于c - 递归POW : warning: control may reach end of non-void function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053338/

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