...
Conversion as if by assignment to the type of the function is required if the return expression has a different type than the function, but not if the return expression has a wider value only because of wide evaluation. This allows seemingly inconsistent and confusing behavior. Consider ,for following code example:
Code Block |
---|
float f(float x) { Â return x * 0.1f; } float g(float x) { Â return x * 0.1; } |
...
Forcing the range and precision inside the calcPercentage()
function is a good way to fix the problem once without having to apply fixes in multiple locations (every time calcPercentage()
is called). However, access to the called function may not always be available. This compliant solution shows one way to force the correct range and precision in a situation in which the source of the called function can not cannot be modified. This is accomplished by casting the return value of the calcPercentage()
function to float
.
...