SilverShotBee SilverShotBee - 1 month ago 11
Android Question

Android Math Calculation Not Giving Expected Value

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:


  1. ((20 - 0) / (20 - 4)) * (12 - 4) + 0

  2. ((20) / (16)) * (12 - 4) + 0

  3. (1.25) * (8) + 0

  4. Result = 10



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

Answer

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.

Comments