简体   繁体   English

从函数返回浮点值

[英]returning a floating point value from a function

hey i really need help with this program.. i doesnt want return a proper answer for a negative power always return 1.. can anyone help?? 嘿,我真的需要帮助这个程序..我不想要一个正确的答案,负面的力量总是返回1 ..任何人都可以帮助?? The program should help the user to enter a base number and power and when executed calculates the value and displays the result 该程序应该帮助用户输入基数和功率,并在执行时计算值并显示结果

float square (float a,int b);

int main(){
    int power;
    float base;

    printf("Please input your base number :\n");
    scanf("%f",&base);
    printf("Please input your power :\n");
    scanf("%d",&power);

    float answer = square(base,power);

    printf("Your Result is :\n%f\n",answer);

    system("pause");
    return 0;
}

 float square (float a,int b){
     int counter;
     float total = 1;
     if (b==0){
         return 1;
     } else if (b>0){
         for(counter=0;counter<b;counter++){
             total = total*a;
         }
         return total;
     } else {
         for (counter=0;counter<b;counter++){
             total = total*a;
         }
         total = 1 / total;
         return total;
     }
}

Declare total as a float instead of an int . total声明为float而不是int

Also, your if's should be on b instead of a . 另外,你的if's应该是b而不是a

After that, remove all those unnecessary (float) castings. 之后,删除所有不必要的(float)铸件。

Also, your last for for should run backwards as your initial value is less than 0 . 此外,您持续for应该倒着跑为初始值小于0

Your square function should look like this: 你的方形函数应如下所示:

float square (float a,int b){
   int counter;
   float total = 1;
   if (b==0){
       return 1;
   } else if (b>0){
       for(counter=0;counter<b;counter++){
           total = (float)total*a;
       }
       return total;
   } else {
       for (counter=0;counter< -b;counter++){
           total = (float)total*a;
       }
       total = 1.0 / (float)total;
       return total;
   }
}

You need a float for total, you need to check b instead of a in your if statements, and you need to count up to -b when it's negative. 你总共需要一个浮点数,你需要在if语句中检查b而不是a,并且当它为负时你需要计算-b。

Declare total to float and initialize it as total声明为float并将其初始化为

float total = 1.0f;   

Use abs function for dealing with -ve b . 使用abs函数处理-ve b

else {
         for (counter=0;counter < abs(b);counter++){
             total = total*a;
     }
     total = 1 / total;
     return total;

} }
and include <stdlib.h> . 并包括<stdlib.h>
See the working code Here . 请参阅此处的工作代码。

This part 这部分

 } else {
     for (counter=0;counter<b;counter++){
         total = total*a;
     }
     total = 1 / total;
     return total;
 }

ignores the fact that b is < 0. 忽略b <0的事实。

So either you should put b = -b; 所以要么你应该把b = -b; in, or 在,或

     for (counter=0;counter>b;counter--){
         total = total / a;
     }
     return total;

might help you. 可能会帮助你。

This does the job ;) 这样做;)

#include <stdio.h>

#include <math.h>


int main(){
   int power;
   float base;

   printf("Please input your base number :\n");
   scanf("%f",&base);
   printf("Please input your power :\n");
   scanf("%d",&power);

   float answer = pow(base,power);

   printf("Your Result is :\n%f\n",answer);

   system("pause");
   return 0;
}

Here's a cool trick: 这是一个很酷的技巧:

y = a^b y = a ^ b

ln(y) = b ln(a) ln(y)= b ln(a)

y = exp(b ln(a)) y = exp(b ln(a))

so the function should look like this: 所以函数应该如下所示:

double Power(double a, double b)
{
    return exp(b*log(a));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM