2 months, 2 weeks ago.

Calculation always evaluates to 0


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?



  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);

Comment on this question

1 Answer

2 months, 2 weeks ago.

Hellow Andrew,

  • Is gain_calc declared as float?
  • Try to first multiply and then divide when calculating gain_calc :

gain_calc =  ((lr_volume - vol_min)*16.0f)/gain_range;


thank you for your prompt reply - much appreciated.

Indeed, the problem has turned out to be me mixing typecasts - specifically double and float. When I declared gain_range as double, the problem was resolved.

posted by Andrew R 02 Dec 2018

To post an answer, please log in.