...
Code Block | ||
---|---|---|
| ||
long double ld; double d1; double d2; float f1; float f2; .../* initializations */ f1 = (float)d1; f2 = (float)ld; d2 = (double)ld; |
...
This compliant code properly checks to see whether the values to be stored are too large to can be represented properly in the new type.
Code Block | ||
---|---|---|
| ||
#include <float.h> ... long double ld; double d1; double d2; float f1; float f2; .../* initializations */ if(d1 > FLT_MAX || d1 < -FLT_MAXMIN) { /* Handle error condition */ } else { f1 = (float)d1; } if(ld > FLT_MAX || ld < -FLT_MAXMIN) { /* Handle error condition */ } else { f2 = (float)ld; } if(ld > DBL_MAX || ld < -DBL_MAXMIN) { /* Handle error condition */ } else { d2 = (double)ld; } |
...