Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#CCCCFF
short a = 533;
int b = 6789;
long c = 4664382371590123456L;

float d = a / 7.0f;       // d is 76.14286
double e = b / 30.;       // e is 226.3
double f = (double)c * 2; // f is 39.6801976911846074E21328764743180247E18

Compliant Solution 2

In this compliant solution, the decimal error in initialization is eliminated by first storing the integer in the floating-point variable and then performing the 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
bgColor#CCCCFF
short a = 533;
int b = 6789;
long c = 4664382371590123456L;

float d = a;
double e = b;
double f = c;

d /= 7;   // d is 76.14286
e /= 30;  // e is 226.3
f *= 7892; // f is 39.6801976911846074E21328764743180247E18

Exceptions

FLP31-EX1: If it is the programmer's intention to have the operation take place as integers before the conversion (obviating the need for a call to floor(), for example) it should be clearly documented to help future maintainers understand that this behavior is intentional.

...