How to Produce a Float Result from Integer Division
In C , integer division truncates the result, even when the output is assigned to a float variable. To prevent this truncation, the operands must be explicitly cast to floats:
float ans = (float)a / (float)b;
In this code:
By casting the operands to floats, the output of the division is retained as a float, resulting in the desired precision:
3 3.333
The above is the detailed content of How to Avoid Integer Truncation in C Float Division?. For more information, please follow other related articles on the PHP Chinese website!