invalid operands of typs ‘double’ & ‘double’ to binary ‘operator^’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int A,B,C,t,theta;
float pi=22/7;
theta=140.0;
t=theta/180.0*pi;
C=sqrt(A^2+B^2-2*A*B*cos(theta));
cout<<C<<endl;
return 0;
}


um havin error massage "test2.cpp:11: error: invalid operands of types ‘double’ and ‘double’ to binary ‘operator^’ " can somebody tell me what's wrong in here.
A and B need to be double not int
^ is not the power operator. It's a binary XOR operator. IE: A^2 does not square A, instead it toggles bit 1 of A.

C++ does not have a built in power operator. For squaring stuff, you're betting off multiplying it by itself:

 
sqrt( A*A + B*B - 2*A*B*cos(theta) );
Last edited on
Ooops I guess that is what I get for learning other programming languages at the same time. If you need a power other than 2 use doubles with the math pow function.
http://cplusplus.com/reference/clibrary/cmath/pow/
heh.

I intentionally didn't mention pow() because it's so often abused.

Don't use pow if you can [reasonably] avoid it. Even raising things to the 3rd or 4th power you're probably better off multiplying. pow is for tricky stuff like raising to 2.16.
Topic archived. No new replies allowed.