QifengSun
QifengSun

Reputation: 33

what is wrong with error: invalid operands to binary expression ('double' and 'double')

for output from one line of my code

double _double = pow(((15) ^ 17)/11 ^ 1.5,2)/9.8;

when I try to compile it, it returns error: invalid operands to binary expression ('double' and 'double') I think both 15 ^ 17 and 11 ^ 1.5 would be double, so why it gave me this error?

Upvotes: 1

Views: 1452

Answers (3)

DWilches
DWilches

Reputation: 23035

You cannot operate two doubles with XOR (^). It only works with int, long, bool, short, char and their variations. No floating-point datatypes.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726799

The compiler gives you an error because ^ in C++ does not do what you think it does. It is a XOR operator, not a power operator, and it works only on integral data types. For example, 15 ^ 17 is 30:

    01111 // 15
XOR 10001 // 17
---------
    11110 // 30

In fact, C++ lacks the power operator altogether. You should use std::pow(double,double) instead:

double _double = pow(pow(15, 17)/pow(11, 1.5), 2)/9.8;

Upvotes: 1

Marcus Müller
Marcus Müller

Reputation: 36402

double _double = pow(((15) ^ 17)/11 ^ 1.5,2)/9.8;

The ^ operator is the logical exclusive or operator.

To exponentiate numbers, use pow, like you do.

Upvotes: 0

Related Questions