Conversion from integer to floating point in an assignment statement Integer arithmetic performed in an assignment to a floating point variable may lead to loss of information if . This can be avoided by converting one of the integer types is not converted the integers in the expression to a floating type.
Non-Compliant Code Example
...
In this compliant code, the decimal error in initialization is eliminated by first storing the integer in the floating point variable and then performing the division arithmetic operation. This ensures that at least one of the operands is a floating point number and consequently the operation is performed on floating point numbers.
| Code Block | ||
|---|---|---|
| ||
short a = 533; int b = 6789; long c = 466438237; float d = a; double e = b; double f = c; d /= 7; /* d is 76.14286 */ e /= 30; /* e is 226.3 */ f /*= 789; /* f is 591176.47275 */ |
...