C is defined for splitting int using integer division, and only when there is a float does it first “push” the other int into the float . Note that this even happens if it is assigned a float - if the right side is all int s, then the division will be integer, and only for the final assignment C will convert the int result to float .
So, with your line:
freq = LAB_Fmin + (((LAB_Fmax) - (LAB_Fmin))/ 255)*x ;
it all depends on what LAB_Fmax and LAB_Fmin . It doesn't matter if freq or x , because the "damage" has already been done due to the brackets that make the division be the first.
If these variables are LAB_F int s, the easiest way to use floating point division is to simply tell C what you want by making the constant 255 a floating point number rather than an integer using the decimal point: 255. (or 255.0 will be less thin) .
If you want to use only integer arithmetic, then the usual suggestion is to do all your multiplications before any divisions. Of course, this can lead to an overflow of the intermediate result - you can use the long type to help you. Define the LAB_F or x variables as long , and split the latter:
freq = LAB_Fmin + (((LAB_Fmax) - (LAB_Fmin)) * x / 255);
John burger
source share