简体   繁体   中英

Error: Invalid operands to binary expression double double, but I'm not using pointers, only using ints

From here and google, I thought the "invalid operands to binary expression double and double" error meant I was trying to operate on doubles or floats when that's not allowed (for some reason) or that I'm messing around with pointers (which I haven't figured out quite yet). Yet I got everything going on here in ints (I think) and I'm not using pointers (that I"m aware of).

I'm just trying to play around with charting out different equations, I'm not really sure what's going on. I wanted to use floats so I could see decimals but I reduced everything down to ints in a desperate attempt to get what I thought would be a simple program to run.

What am I missing here?

#include <stdio.h>

int pricedecrease(int x)
{
    return x - ((0.1 * x) ^ 3);
}

int main(void)
{
    printf("%d", pricedecrease(100));
}

What you're seeing is type promotion . 0.1 is a double , so 0.1 * x is also a double .

You could cast the result like:

return x - ((int)(0.1 * x) ^ 3);

or simply avoid using a double altogether:

return x - ((x / 10) ^ 3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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