Versions Compared

Key

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

Computers can represent only represent a finite number of digits. As a result, it It is therefore impossible to precisely represent repeating binary-representation values such as 1/3 or 1/5 with the most common floating-point representation: binary floating point.

Wiki Markup
When precise computation is necessary, consider alternative representations that may be able to completely represent values.  For example, if you are performing arithmetic on decimal values and need an exact decimal rounding, represent the values in binary-coded decimal instead of using floating-point pointvalues.  Another option is decimal floating-point arithmetic as specified by ANSI/IEEE 754-2007.  ISO/IEC WG14 has drafted a proposal to add support for decimal floating-point arithmetic to the C language \[[ISO/IEC DTR 24732|AA. C References#ISO/IEC DTR 24732]\].

Wiki Markup
When precise computation is necessary, carefully and methodically estimate the maximum cumulative error of the computations, regardless of whether decimal or binary is used, to ensure that the resulting error is within tolerances.  Consider using numerical analysis to properly understand the numerical properties of the problem.  A usefulAn introduction can be found in \[[Goldberg 91|AA. C References#Goldberg 91]\].

...

This noncompliant code example takes the mean of ten 10 identical numbers , and then checks to see if the mean matches the first this number. It should, because the ten numbers are all 10.1. Yet, due to because of the imprecision of floating-point arithmetic, the computed mean does not match the numbersthis number.

Code Block
bgColor#ffcccc
#include <stdio.h>

/* Returns the mean value of the array */
float mean(float array[], int size) {
  float total = 0.0;
  int i;
  for (i = 0; i < size; i++) {
    total += array[i];
    printf("array[%d] = %f and total is %f\n", i, array[i], total);
  }
  if (size != 0)
    return total / size;
  else
    return 0.0;
}

enum {array_size = 10};
float array_value = 10.1;

int main(void) {
  float array[array_size];
  float avg;
  int i;
  for (i = 0; i < array_size; i++) {
    array[i] = array_value;
  }

  avg = mean( array, array_size);
  printf("mean is %f\n", avg);
  if (avg == array[0]) {
    printf("array[0] is the mean\n");
  } else {
    printf("array[0] is not the mean\n");
  }
  return 0;
}

On a 64-bit Linux machine using gcc GCC 4.1, this program yields the following output:

...

This code may be fixed by replacing the floating-point numbers with integers for the internal additions. Floats are used only when printing results , and when doing the division to compute the mean.

Code Block
bgColor#ccccff
#include <stdio.h>

float nearbyintf( float);

/* Returns the mean value of the array */
float mean(int array[], int size) {
  int total = 0;
  int i;
  for (i = 0; i < size; i++) {
    total += array[i];
    printf("array[%d] = %f and total is %f\n", i, array[i] / 100.0, total / 100.0);
  }
  if (size != 0)
    return ((float) total) / size;
  else
    return 0.0;
}

enum {array_size = 10};
int array_value = 1010;

int main(void) {
  int array[array_size];
  float avg;
  int i;
  for (i = 0; i < array_size; i++) {
    array[i] = array_value;
  }

  avg = mean( array, array_size);
  printf("mean is %f\n", avg / 100.0);
  if (((int) nearbyintf( avg)) == array[0]) {
    printf("array[0] is the mean\n");
  } else {
    printf("array[0] is not the mean\n");
  }
  return 0;
}

On a 64-bit Linux machine using gcc GCC 4.1, this program yields the following expected output:

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[Goldberg 91|AA. C References#Goldberg 91]\]
\[[IEEE 754 2006|AA. C References#IEEE 754 2006]\]
\[[ISO/IEC JTC1/SC22/WG11|AA. C References#ISO/IEC JTC1/SC22/WG11]\]
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "PLF Floating Point Arithmetic"
\[[ISO/IEC DTR 24732|AA. C References#ISO/IEC DTR 24732]\]
\[[Goldberg 91|AA. C References#Goldberg 91]\]

...

FLP01-C. Take care in rearranging floating point expressions      05. Floating Point (FLP)       FLP03-C. Detect and handle floating point errors