Question:
How do I fix this c++ math equation?
bahmtf
2010-02-09 06:26:37 UTC
(salary * .10) * (2.718 ^ (((age - 50) * (age - 50)) / 1250) - (copay * 3)

all the variables are declared as floats
i get this error message when i try to compile:
"invalid operands of types *double* and *float* to binary operator"
i dont know what is wrong with this equation but it is definitely the problem, please help!
Five answers:
2010-02-09 06:47:07 UTC
Hi,



I am assuming you are using ^ operator to calculate power. Use the math.h function pow(double x, double y). Check out this small program to see how it works:



#include

#include



int main(){

double salary = 4.0, age = 23.0, copay = 1.0, ans = 0.0;

ans = (salary * .10) * (pow(2.718,(((age - 50) * (age - 50)) / 1250) - (copay * 3)));

return 0;

}



Varun
Scooter_MacGyver
2010-02-09 06:45:03 UTC
The ^ operator, that's your problem. It is NOT exponentiation, it is bitwise xor (google it) and is only defined for integer data types (char, byte, short, int, long...), one side of the ^ as type promoted to a double because it didn't fit into a float, but they float ^ double generated an error.



Use the pow function instead inside the math library
lansingstudent09101
2010-02-09 06:38:33 UTC
All the number literals are declared as doubles. You have to add an f to the end to get them to be floats for example:



(salary * .10f)



Ah! I can't believe I missed that ^ operator. I was thinking the whole time "they ought to be promoted to doubles". yeah,

#include and the pow() function.
2010-02-09 06:31:21 UTC
What's the data type of salary, age and copay? All should be of the same data type before this could be equated.
?
2016-12-11 11:04:36 UTC
I see this project each and every few days right here on YA and the most effortless blunders are: misusing information varieties on your cas you get int coeff. yet pass then to a function that calls for double! do not attempt this. you should forged them: getCalc( (double) a, (double) b, (double) c ); is the most proper fix


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...