简体   繁体   English

代码块电源功能在c中不起作用

[英]code blocks power function is not working in c

i am using code block for learning c. 我正在使用代码块学习c。 my code is 我的代码是

#include<stdio.h>
#include<math.h>
int main()
{
  int x;
  x = pow(5,2);
  printf("%d", x);
}

Output is 25

When i am using this code 当我使用此代码时

#include<stdio.h>
#include<math.h>
int main()
{
  int x,i,j;
  printf("please enter first value");
  scanf("%d", &i);//5
  printf("please enter second value");//2
  scanf("%d", &j);
  x = pow(i,j);
  printf("%d", x);
}

Output is 24

what is wrong here? 这是怎么了 i am just taking value using scan function and also using pow function in a same way. 我只是使用扫描功能并以相同的方式使用电源功能来获取价值。

I suspect you have a naive implementation of pow in your libm (I get 25, as it should be). 我怀疑您的libmpow的天真实现(我应该得到25,应该如此)。 If that computes pow(x,y) as exp(y*log(x)) for positive x without checking for (small) integral exponents and treating them specially, you get 如果将pow(x,y)计算为正x exp(y*log(x)) ,而不检查(小)积分指数并进行特殊处理,则得到

Prelude> 2 * log 5
3.2188758248682006
Prelude> exp it
24.999999999999996

a double result slightly smaller than 25, so when that is converted to int it is truncated to 24. 小于25的double结果,因此当将其转换为int时,将其截断为24。

To check, assign the result of pow(i,j) to a double and print that out. 要进行检查,请将pow(i,j)的结果分配给double并打印出来。

With hardcoded pow(5,2) , the compiler (most likely, it's what gcc does even without optimisation) computes the result during compilation exactly. 使用硬编码的pow(5,2) ,编译器(最有可能的是,即使没有优化,它也是gcc所做的)精确地计算编译期间的结果。

Try changing initialization to this: 尝试将初始化更改为此:

int x=-1 ,i=-1 ,j=-1;

And last print to this: 最后打印到此:

printf("pow(%d, %d) == %d\n", i, j, x);

That should give good hint about the problem. 这应该可以很好地提示问题。 Also, check return values of scanf , they should return number of items read, ie. 另外,检查scanf返回值,它们应该返回读取的项目数,即。 1 with code above. 1,上面的代码。

It's almost certain, that you entered invalid input for scanf, and i or j were left uninitialized, and that 24 is just garbage value. 几乎可以肯定,您为scanf输入了无效的输入,并且ij未被初始化,而24只是垃圾值。

Also, compile with warnings enabled, and fix them (like, add return 0; to end of main ). 另外,在启用警告的情况下进行编译并进行修复(例如,将return 0;添加到main末尾)。

Your code correctly gives 25 on my windows x64. 您的代码在我的Windows x64上正确显示25。

You probably needs to run it again see if you just read it wrong... 您可能需要再次运行它,看看您是否读错了...
The missing "return 0;" 缺少的“返回0”; is not the problem here. 这不是问题。

If, anything, could ever go wrong, 如果有什么可能出错,
you can try adding 你可以尝试添加

fflush(stdin);//or out

after very scanf and printf. 在非常scanf和printf之后。 If any of the flushes solves your problem, you know what is going wrong. 如果有任何冲洗可以解决您的问题,那么您知道出了什么问题。

It seems that there is nothing wrong with the second program, except that you must add at the end 似乎第二个程序没有什么问题,除了必须在末尾添加

return 0;

If you read the value j with 2 then the result will be just 25. 如果用2读取值j,则结果将仅为25。

Using your code i got result 25 which is correct. 使用您的代码,我得到了正确的结果25。 Although Try changing the data type of result such as float or double. 尽管尝试更改结果的数据类型,例如float或double。

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

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