?
Solved

floating point errors with dbeditboxes 1 + 1 = 2

Posted on 2003-03-27
3
Medium Priority
?
235 Views
Last Modified: 2010-04-04
HI

I`m having a problem with floating point errors related to a function
and two procedures which add two data aware controls..ie dbeditboxes.
It seems the error occurs when the user clears a value from the dbeditboxes.
I`ve tried applying error handling to the function to eliminate the error but
once the program is complied I still receive the error. If I run the exe. the
problem goes away. I still would like to find a solution to eliminate this problem.
Another strange problem with this code is if the second dbedit has a value
the total is changed only if the first dbeditbox is modified. Subsequent changes
to the second dbeditbox will not update the third dbedit. Also, if the third
dbedit is clicked the total is changed to zero. As a experiment I setup the Access
table with zero values in the hopes of eliminating the errors. I assume the zero
is showing up from the table. I did attempt to set the third dbedit to read only
but for some reason the value still changes if clicked on.
Note: I`m a newbie to Delphi and the use of data aware controls so it`s more
then likely I`ve setup various data aware setting incorrectly. I`d greatly appreciate
your expertise on this never ending problem.
Database controls used
ADOConnection
ADOTable
ADODataSource

function Add2InputNumbers(EditA, EditB: TsuiDBEdit): Double;
begin
   try
      Result := StrToFloat(EditA.Text) + StrToFloat(EditB.Text);
   except
      result := 0
   end;
end; // function to add two numbers


First dbedt... this procedure will always update the total
procedure TfrmDrw.dbedtTopClearBoxChange(Sender: TObject);
begin
   dbedtTotalHtClearance.Text := FloatToStr(Add2InputNumbers(dbedtTopClearBox,
      dbedtBotClearBox));
end;

second dbedt... this procedure will will not change the total
procedure TfrmDrw.dbedtBotClearBoxChange(Sender: TObject);
begin
   dbedtTotalHtClearance.Text := FloatToStr(Add2InputNumbers(dbedtTopClearBox,
      dbedtBotClearBox));
end;
end.
0
Comment
Question by:LostInSpace2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
grolschisgood earned 1200 total points
ID: 8222408
with the info you give i would suggest the following problems.

1.  the floating point error is generated when you use the strtofloat() function and the editA.text or editB.text are blank.  change your sum function to check if they are blank first.

function Add2InputNumbers(EditA, EditB: TsuiDBEdit): Double;
begin
  try
     if EditA.text='' then EditA.text:='0';
     if EditB.text='' then EditB.text:='0';
     Result := StrToFloat(EditA.Text) + StrToFloat(EditB.Text);
  except
     result := 0
  end;
end; // function to add two numbers

2.  the second procedure may not be updating the total becuase for some reason it has become unlinked from the OnChange event of the second edit box

3.  when clicking on the 3rd edit box and the value is reset to 0, to avoid this you may want to try writing the value direct to the corresponding recordset field rather than assigning it to the text property of the dbedtTotalHtClearance edit box.  use...

 fieldbyname('<your field name>').asfloat:=Add2InputNumbers(dbedtTopClearBox,
     dbedtBotClearBox);
0
 
LVL 2

Expert Comment

by:steve_hsk
ID: 8223513
Hi LostinSpace2 ...

There is a very simploe reason behind what you're seeing !

1) The reason behind why the error is occuring is whenever either of the dbEdit's are changed, an the event handler is called, if the edit box is empty, you are calling :
FloatToStr, or StrToFloat on an empty parameter.

Ass you and Grol suggested, use a try catch statment around the offending code to catch the exception :-

FUNCTION Add2InputNumbers(EditA, EditB: TsuiDBEdit): Double;
BEGIN
  TRY
     Result := StrToFloat(EditA.Text) + StrToFloat(EditB.Text);
  EXCEPT
     result := 0
  END;
END;

PROCEDURE TfrmDrw.dbedtTopClearBoxChange(Sender: TObject);
BEGIN
  TRY
     dbedtTotalHtClearance.Text := FloatToStr(Add2InputNumbers(dbedtTopClearBox,
     dbedtBotClearBox));
   EXCEPT
     dbedtTotalHtClearance.Text := '';
   END;
END;

PROCEDURE TfrmDrw.dbedtBotClearBoxChange(Sender: TObject);
BEGIN
   TRY
      dbedtTotalHtClearance.Text := FloatToStr(Add2InputNumbers(dbedtTopClearBox,
     dbedtBotClearBox));
   EXCEPT
     dbedtTotalHtClearance.Text := '';
   END;
END;

2) The reason behind why this doesn't occur in the .Exe but still occurs in delphi, is a debugger setting.

What ever the exception occurs, eg EFloatingPoint, needs to be added to the exception handled list :

Menu >> Tools >> Debugger Options >> Language Exceptions >> Add (EFloatingPoint);

This means that delphi will now let your code handle the exception. This will not affect the .EXE either way.

As a side note : It would be better programming practice to handle the specific exception, as opposed to all exceptions ... eg :
   
   TRY
      ...
   EXCEPT ON EFloatingPoint DO
     dbedtTotalHtClearance.Text := '';
   END;

This allows for much better error tracking, abd bug fixing later on !

Hope this helps,
STeve
0
 

Author Comment

by:LostInSpace2
ID: 8226191
The code is working fine........Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question