I happen to come across some similar kind of programming styles mostly when there is an Float or Double operations.
ratio = 1.0 * (top - bottom) / (right - left);
All variables involved are float.
What is the significance of having 1.0 multiplied to the result?
As per my thinking multiplying 1.0 is some extra burden.Since the result wont change.
Or is it similar to write an condition having and 1==1.
P.S: There are some situations where in some variables(except ratio) are assigned to non Float/double values as a long or integer.
C/C++
When one or more of the variables
top,bottometc. are of typedoublethen the multiplication by1.0is pointless. You should simply remove it since it serves no purpose. When all of the variables are of typefloatthen the multiplication by thedoubleliteral1.0forces the expression to be evaluated with double precision arithmetic.On the other hand, when all of the variables are integers, the multiplication by
1.0forces the calculation to be performed with floating point arithmetic. Without the multiplication the calculation would be performed with integer arithmetic which would yield a different result.My guess is that the code originally used integers and the
1.0was needed. At some point in time the code was changed to use floating point variables but the now spurious multiplication was not removed.Delphi
If you saw such an expression in Delphi code then you should simply remove the multiplication. The presence of a division operator forces the expression to be evaluated as a floating point expression.
The rules for Delphi expression evaluation are a little different from C and C++. In C and C++ a single symbol,
/is used for both integer and floating point division, with the context of the expression determined which form of division is used. In Delphi/is floating point division anddivis integer division.