Reputation: 1571
I'm trying to run a calculation but I'm not getting the correct result and struggling to understand why.
Calculation
float Signal = ((20 - 0) / (20 - 4)) * (F5 - 4) + 0;
F5
= 12 and is declared as a "Float" type
When run through a calculator, you end up with the following:
However, when this is run through the Android code, I get the result of 8.
Why is this, I'd like to understand what's going on
Upvotes: 0
Views: 49
Reputation: 86774
The first term ((20 - 0) / (20 - 4))
is calculated using integer arithmetic, giving a value of 1
. This makes the final result 8
regardless of the type of F5
. If you want it to happen in floating point, use floating point constants
float Signal = ((20f - 0f) / (20f - 4f)) * (F5 - 4f) + 0f;
Technically you don't need all the constants to be floats due to numeric promotion, but it is much clearer to those reading your code that you intended for everything to be a float.
Upvotes: 2