...
Non-Compliant Code Example (int-float)
The following NCCE leads to undefined behavior if the integral part of f1
cannot be represented as an integer.
Code Block |
---|
|
float f1;
int i1;
/* initializations */
i1 = f1; /* Undefined if the integral part of f1 > INT_MAX */
|
Compliant Solution (int-float)
This compliant solution assumes that the range of floating point values is greater than that of integers (this is the case in almost all implementations). Unfortunately, there is no safe way to inquire about this assumption in the code short of already knowing the implementation.
Code Block |
---|
|
float f1;
int i1;
/* initializations */
if (f1 > (float) INT_MAX || f1 < (float) INT_MIN) {
/* Handle Error */
} else {
i1 = f1;
}
/* XXX NOT DONE */
|
Compliant Solution (int-float)
Non-Compliant Code Example (demotions)
...