Tag Archives: four hundred and eight

[Error] invalid operands to binary ^ (have ‘double‘ and ‘float‘)

C. It cannot be used directly in C + +^

In C and C + +, you can’t use ^ to represent the index, only * can be used. If you want to use the index, you can only establish a cycle to multiply multiple times or write multiple directly by multiplication. The following is my code. The comment part is the original index form, and the above error will be reported.

Or reference mathematical functions and add #include & lt; math.h>;

Pow (x, y) is used to solve the Y power of X;

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	double p0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;
	p1=p0*(1+r1)*(1+r1)*(1+r1)*(1+r1)*(1+r1)*(1+r1);
	//p1=p0*(1+r1)^6;
	p2=p0*(1+r2)*(1+r2)*(1+r2);
	//p2=p0*(1+r2)^3;
	p3=p0*(1+r3)*(1+r3);
	//p3=p0*(1+r3)^2;
	double x=1,y=2,p;
	p = pow(x+1,y);  //Find the yth power of x+1, which is the exponent 
	printf("The square of 1+1 is %lf",p);
	printf("deposit 1 year %lf, deposit 2 years %lf, deposit 3 years %lf",p1,p2,p3);
	return 0;
}