...
Here, b is first converted to int so that the + operator can be applied to operands of the same type. The result of (a+b) is then converted to a float, and the comparison operator is finally applied.
Noncompliant Code Example
Here is an example of what could happen.
...
The output is
2.0E9
1.999999999E9
while the expected output is :
2.0E9
2.0E9.
big is first converted to a float, and loses some precision, which explains the 2.0E9. However, big_float doesn't lose any precision since it's a wider type than float, this is why the two output are different.
Compliant solution
In that particular case, it would be sufficient to use a double instead of a float :
...
Note : these output were generated on a Java Hotspot(TM) 64-bit server VM (version 1.5.0), and compiled with javac, version 1.5.0.
Risk assessment
Failing to consider integer promotions while dealing with floating point and integer operands together, can result in loss of precision.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
EXP08-J | low | probable | medium | P4 | L3 |
References
| Wiki Markup |
|---|
\[[JLS 05|AA. Java References#JLS 05]\] 5.6 "Numeric Promotions" |