Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2007 Aug : Wrong floating point results

www.cryer.info
Managed Newsgroup Archive

Wrong floating point results

Subject:Wrong floating point results
Posted by:"Chaim" (chaim..@mer.co.il)
Date:15 Aug 2007 02:21:04

Hi All,
In our program (Delphi 6 on XP, Pentium 4) we are transferring a TDateTime value between computers. The client computer receives the value as a TDateTime in a string for example: '39308.747399537'. Than the client converts it to float and assigns it to a TDateTimeField.
Usually (over 90% of the times) this operation works fine, but there are occasions when the StrToFloat operation returns a different number from the string TDateTime. In the example we got 39308.7467198087 instead of the above value. This of course gives a wrong DateTime our application. Later we found out that every floating operation of divide/multiply returns a wrong value when the error happens.
Here is a summery of the code:
var
  sDateTime     : String;
  DateTimeValue : TDateTime;
begin
  // recieves TDateTime string like '39308.747399537'
  sDateTime := GetDateTimeString(Msg);
  DateTimeValue := StrToFloat(sDateTime);
  // We get the a different value: 39308.7467198087
  WriteToDebug(FloatToStr(DateTimeValue);
end;

Does anyone know why this could happen and how to fix it ???

Thanks in advance,
Chaim.

Replies:

www.cryer.info
Managed Newsgroup Archive