C语言 pow() 是否适用于 C 中的 int 数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29787310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:52:39  来源:igfitidea点击:

Does pow() work for int data type in C?

cpowexponentiation

提问by basanta

I was simply writing a program to calculate the power of an integer. But the output was not as expected. It worked for all the integer numbers except for the power of 5.

我只是在编写一个程序来计算整数的幂。但输出并不如预期。它适用于除5以外的所有整数

My code is:

我的代码是:

#include <stdio.h>
#include <math.h>

int main(void)
{
  int a,b;
  printf("Enter the number.");
  scanf("\n%d",&a);
  b=pow(a,2);
  printf("\n%d",b);
}

The output is something like this:

输出是这样的:

"Enter the number. 2
 4
"Enter the number. 5
 24
"Enter the number. 4
 16
"Enter the number. 10
 99

Can't we use pow()function for int data type??

我们不能pow()对 int 数据类型使用函数吗??

回答by haccks

Floating point precision is doing its job here. The actual working of powis using log

浮点精度在这里发挥作用。的实际工作pow是使用log

pow(a, 2) ==> exp(log(a) * 2)

Look at math.hlibrary which says:

看看math.h图书馆说:

<math.h>

<math.h>

/* Excess precision when using a 64-bit mantissa for FPU math ops can cause unexpected results with some of the MSVCRT math functions. For example, unless the function return value is stored (truncating to 53-bit mantissa), calls to pow with both x and y as integral values sometimes produce a non-integral result. ... */

/* 将 64 位尾数用于 FPU 数学运算时,精度过高可能会导致某些 MSVCRT 数学函数出现意外结果。例如,除非存储函数返回值(截断为 53 位尾数),否则以 x 和 y 作为整数值调用 pow 有时会产生非整数结果。... */

Just add 0.5to the return value of powand then convert it to int.

只需添加0.5到 的返回值,pow然后将其转换为int.

b = (int)(pow(a,2) + 0.5);  

So, the answer to your question

所以,你的问题的答案

Does pow() work for intdata type in C?

pow() 是否适用int于 C 中的数据类型?

Not always. For integer exponentiation you could implement your own function (this will work for 0 and +ve exponly):

不总是。对于整数取幂,您可以实现自己的函数(这exp仅适用于 0 和 +ve ):

int int_pow(int base, int exp)
{
    int result = 1;
    while (exp)
    {
        if (exp % 2)
           result *= base;
        exp /= 2;
        base *= base;
    }
    return result;
}

回答by Keith Nicholas

there is no int based pow. What you are suffering from is floating point truncation.

没有基于 int 的 pow。你所遭受的是浮点截断。

an int based pow is too constrained (the range of inputs would quickly overflow an int). In many cases int based pow, like in your case where its powers of 2 can be done efficiently other ways.

基于 int 的 pow 过于受限(输入范围会很快溢出 int)。在许多情况下,基于 int 的 pow,就像在你的情况下,它的 2 的幂可以通过其他方式有效地完成。

回答by Carl Norum

printf("%a", pow(10, 2))and see what you get; I expect you'll see you don't quiteget 100. Call lroundif you want to round instead of truncating.

printf("%a", pow(10, 2))看看你得到了什么;我想你会看到你不得100电话lround,如果你想圆的,而不是截断。

回答by ANjaNA

The C library function double pow(double x, double y)

C 库函数 double pow(double x, double y)

It takes double type

它需要双重类型