Topic: floating point error

Hi All!

Hope all of y'all are safe. I am using 6.3 and I get a floating point error.

My code is :

procedure CalculateTotal;
begin
    BA.edBA.Value := BA.edH.Value / BA.edAB.Value; // calculate
end;

procedure BA_edAB_OnChange (Sender: string);
begin
    CalculateTotal;
end;

procedure BA_edH_OnChange (Sender: string);
begin
    CalculateTotal;
end;

I am trying to get the BA of my daughter for her softball games.

example :

3 At Bats (AB)
1 Hit (H)

1 H / 3 AB = .333 Batting Average (BA) =  very good by the way. smile

The error and I also have a formatting issue on the tablegrid.
Thanks,

scribtx

Post's attachments

Attachment icon error1.png 3.13 kb, 89 downloads since 2021-04-03 

Re: floating point error

I would do it like this.

procedure CalculateTotal;
begin
    frm_main.labBA.caption := StrToInt(frm_main.edH.text) / StrToInt(frm_main.edAB.text); // Calculate
end;

procedure frm_main_edAB_OnChange (Sender: string);
begin
If (frm_main.edAB.text = '') then
begin
    ShowMessage('Division x 0 is not permitted');
end Else
begin
    CalculateTotal;
end;
end;

procedure frm_main_edH_OnChange (Sender: string);
begin
If (frm_main.edAB.text = '') then
begin
    ShowMessage('Division x 0 is not permitted');
end Else
begin
    CalculateTotal;
end;
end;

of course, you know that division by zero is going to give you the same error, you must manage this detail.

3 (edited by derek 2021-04-04 20:43:11)

Re: floating point error

Hi Scribtx, Hi Wen,
To avoid possible data redundancy, I'd think about using calculated fields instead (please see attachment).
Derek.

Post's attachments

Attachment icon batting average.zip 340.6 kb, 202 downloads since 2021-04-04 

Re: floating point error

Thanks! Y'all do know that y'all are awesome! Right? I think so. smile