Problems comparing floating point numbers.

Abstract: Floating point numbers cannot be accurately compared for equality.

Because of the way floating point numbers are represented by the computer there can be inconsistencies when comparing two numbers that appear to be identical. Unlike integers, IEEE floating point numbers are only approximates, not exact numbers. The need to convert the numbers to a form the computer can store in binary leads to minor precision or round-off deviations. For example 1.3 may really be represented as 1.29999999999.

There may particularly be problems when using conversion functions such as StrToFloat, TextToFloat and StrToCurr. Therefore you should never use = or <> to compare two floating point numbers. Instead, subtract the two numbers and compare them against a very small number.


procedure TForm1.Button1Click(Sender: TObject);
  f1, f2 : double;
  f1 := 1.3000000;
  f2 := 1.2999999;
  if abs(f1-f2) < 0.00001
  then ShowMessage('f1 and f2 are equal')
  else ShowMessage('f1 and f2 are not equal');

Server Response from: ETNASC04