简体   繁体   English

在C语言中使用“ pow()”时非法使用浮点错误

[英]Illegal use of Floating point error while using “pow()” in C

I am getting "Illegal use of floating point" error in line no 8,10 and 12. 我在第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();
}

I think there is a problem while calculating the remainder of p using the pow() funciton. 我认为使用pow()函数计算p的余数时出现问题。 Please help me. 请帮我。

You cannot use a float or double value for modulo ( % ). 不能将floatdouble float值用作模( % )。 The value for modulo needs to be an integer, which is what your compiler is telling you about when it gives you the error "Illegal use of floating point" 取模的值必须是整数,这是编译器告诉您的,它会在错误“非法使用浮点”时提示您

You are not supposed to use the modulo operator for anything else then 您不应该再对其他任何运算使用模运算符

<integer value>%<integer value>

If you just want to evaluate your double as if it was an int (without rounding), then you can just cast the double to int . 如果只想将double当作int来求值(不舍入),则可以将doubleint

Like this: 像这样:

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

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

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