开发者

function getting wrong values

开发者 https://www.devze.com 2023-02-04 10:58 出处:网络
so i have this function in C to calculate a power, and i\'m using visual c++ 2010 power.h void power();

so i have this function in C to calculate a power, and i'm using visual c++ 2010

power.h

void power();  
float get_power(float a, int n);  

power.c

void power()
{
    float a, r;
    int n;
    printf("-POWER-\n");
    printf("The base: ");
    scanf("%f", &a);
    n = -1;
    while (n < 0)
    {
        printf("The power: ");
        scanf("%d", &n);
        if (n < 0)
        {
            printf("Power must be equal or larger than 0!\n");
        }
        else
        {
            r = get_power(a, n);
            printf("%.2f ^ %d = %.2f", a, n, r);
        }
    };
}

float get_power(float a, int n)
{
    if (n == 0)
    {
        return 1;
}
    return a * get_power(a, n-1);
}

not the best way to do it, i know, but that's not it

when i debug it the values are scanned correctly (that is, the values are correct until just before the function call) but then upon entering the function a becomes 0 and n becomes 1074790400, and you can guess what happens next...

the first function is being called from the main file, i included the full code because i really have no idea what could be going on, and i can't even think on how to google for it...

strangely, i wrote the function in a single file and it works fine, but it d开发者_如何学JAVAefinitely should work both ways

any idea why this is happening?


Do you have

#include "power.h"

at the top of power.c?

If not, the compiler doesn't know what the prototype of get_power() is at the point of the call, so it will resort to promoting the first argument to a double instead of passing it as a float. It'll also incorrectly assume that the result is an int instead of the float that's being returned.

If the compiler sees the prototype before the call, things will work better.


You can use pow from math.h. In other way you have error in multiplication in get_power, better write this on for.


Was there a specific reason why you wanted to use recursion?

What about something simple like (untested):

float get_power(float a, int n)
{
    float result = 1.0;
    for (int i = 0; i < n; i++)
    {
        result = result * a;
    }
    return result;
}


Just to add to the existing answer, I ran this on xlc to see if it was a visual studio only problem. I got this answer:

"power.c", line 25.7: 1506-343 (S) Redeclaration of get_power differs from previous declaration on line 19 of "power.c".
"power.c", line 25.7: 1506-050 (I) Return type "float" in redeclaration is not compatible with the previous return type "int".
"power.c", line 25.7: 1506-379 (I) Prototype for function get_power must contain only promoted types if prototype and nonprototype declarations are mixed.
"power.c", line 25.7: 1506-380 (I) Parameter 1 has type "float" which promotes to "double".

I also tried it in a few other compilers I had around and got variations on this theme. You can see this as a warning in VS if you turn up the warning level.

So in conclusion, it SHOULDN'T compile, and VS is the only one that does compile and link it.

0

精彩评论

暂无评论...
验证码 换一张
取 消