code块幂函数是不是在C工作 [英] code blocks power function is not working in c

查看:169
本文介绍了code块幂函数是不是在C工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用code块学习c。我的code是

i am using code block for learning c. my code is

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

Output is 25

当我用这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

这里有什么问题?我使用扫描功能,只是走的价值,也以同样的方式使用POW功能。

what is wrong here? i am just taking value using scan function and also using pow function in a same way.

推荐答案

我怀疑你有一个天真的实施 POW 在你的的libm (我得到25,因为它应该是)。如果计算 POW(X,Y) EXP(Y *日志(X)) X 不检查(小)整体指数和特别对待他们,你得到

I suspect you have a naive implementation of pow in your libm (I get 25, as it should be). 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

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

双击结果超过25略小,因此,当被转换为 INT 它被截断为24。

a double result slightly smaller than 25, so when that is converted to int it is truncated to 24.

要检查,分配 POW(I,J)双击并打印了出来的结果。

To check, assign the result of pow(i,j) to a double and print that out.

使用硬codeD POW(5,2),编译器(最有可能的,这是什么做的gcc即使没有优化)正是在编译过程中计算结果。

With hardcoded pow(5,2), the compiler (most likely, it's what gcc does even without optimisation) computes the result during compilation exactly.

这篇关于code块幂函数是不是在C工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆