Page 1 of 1

weird asfloat and value issue

Posted: Thu 27 Dec 2007 19:44
by ben
Hello,

I have a table with a value that I can see on the grid its 1.7

I assign the value to a variable

var
TotalValue: Real;
begin
TotalValue := DM.MemberSearch.FieldByName('debt').AsFloat;
ShowMessage(FloatToStr(TotalValue));

and it says 1.70

then I execute the code:

ShowMessage(FloatToStr(TotalValue - 1.70));

and instead of 0 I receive

-4,44522890719057E-17

Very weird because If I change the first line with

TotalValue := StrToFloatDef(DM.MemberSearch.FieldByName('debt').AsString, 0);

The result is 0. Which means that something is wrong with "AsFloat" assignment.

[EDIT] debt field in mysql is Decimal 6,2

Any idea?

Posted: Fri 28 Dec 2007 12:51
by Dimon
This problem is not concerned with float fields, but with peculiarity of how FPU works with Real type.

The following code demonstrate it:

Code: Select all

var
  TotalValue: Real;
begin
  TotalValue := 1.70;
  ShowMessage(FloatToStr(TotalValue - 1.70));
end;