使用" pow()"时非法使用浮点错误在C.

时间:2016-02-17 08:21:59

标签: c

我得到"非法使用浮点"第8,10和12行的错误。

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{   int p,i,c=0;
    scanf("%d",&p);
    for(i=11;i>=0;i--)
    {   if((p==pow(2,i))&&(p%pow(2,i)==0))      //here
            c++;
        else if((p/pow(2,i)!=0)&&(p%pow(2,i)!=0))   //here
        {       c++;
            p=p%pow(2,i);               //here
        }
    }
    printf("%d",c);
    getch();
}

我认为使用pow()函数计算p的余数时存在问题。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

您不能将floatdouble值用于模数(%)。模数的值需要是一个整数,这是你的编译器告诉你什么时候它给你错误“非法使用浮点数”

除了

之外,你不应该使用模运算符
<integer value>%<integer value>

如果您只想评估double,就好像它是int(没有舍入),那么您可以将double转换为int

像这样:

p%(int)(pow(2,i))!=0