Reputation: 17
i'm doing a code using function, but when i try to run it i have the error "invalid operands to binary ^ (have 'float' and 'int' )". The operation that i want to do is: Vo*((1-(1+I)^-N)/I). Here's the code. Thanks!
#include <stdlib.h>
float func(int anios, float tipointeres){
float interes_mod, func, porcen;
int n=0;
porcen = tipointeres/100; //El interes es porcentaje, así que hacemos esto para que sea uno.
for(n;n<anios;n++){
interes_mod = (1+porcen)^(-anios);
func = ((1-interes_mod)/porcen);
}
return func;
}
int main()
{
int anios;
float valortotal, constrenta, tipointeres;
printf("Ingresa el importe constante de la renta (Vo): ");
scanf("%f", &constrenta);
fflush(stdin);
printf("Ingresa el tipo de interes (I): ");
scanf("%f", &tipointeres);
fflush(stdin);
printf("Ingresa el numero de años (N): ");
scanf("%d", &anios);
fflush(stdin);
valortotal = func(anios, tipointeres)* constrenta; //Aquí multplico Vo por toda la fracción.
printf("El valor actual de la renta es: %f", valortotal);
return 0;
}
Upvotes: 1
Views: 1339
Reputation: 223872
The ^
operator is not the power operator but the bitwise XOR operator. This operator expects both operands to be integer types, which is why you're getting the error.
To perform exponentiation you want the pow
or powf
function, which raises the first argument to the power of the second. In your case you'll want to use the latter since you're only using float
as opposed to double
:
interes_mod = powf(1+porcen,-anios);
Note that you'll also need to #include <math.h>
to get the declaration of the function and compile with the -lm
flag to link in the math library.
Upvotes: 3