Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

floating point errors with dbeditboxes 1 + 1 = 2

Posted on 2003-03-27
3
Medium Priority
?
237 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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

578 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