Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 4 months ago.
Calculation always evaluates to 0
Hello,
in the code snippet below, both gain_range and gain_SYSTEM are declared as float earlier in the program.
However, both gain_calc and volume_display always evaluate to 0. I can only get a non zero (and correct answer) if I remove the division in the gain_calc formula.
Any ideas about what I am doing wrong?
Thanks
AndrewR
gain_range = (rint(gain_SYSTEM)) - vol_min; gain_calc = ((lr_volume - vol_min)/gain_range)*16; //((lr_volume - vol_min)/gain_range)*16; volume_display = rint(gain_calc); //normalized count out of 16 printf("gain_calc = %f gain range %d lr_volume %d vol_disp %d \n\r",gain_calc, gain_range, lr_volume, volume_display); vol_disp();
1 Answer
5 years, 4 months ago.
Hellow Andrew,
- Is
gain_calc
declared asfloat
? - Try to first multiply and then divide when calculating
gain_calc
:
gain_calc = ((lr_volume - vol_min)*16.0f)/gain_range;