Log in

View Full Version : [Delphi] Bitrate calculator


Sirber
17th September 2004, 23:58
procedure TFBitrate.Button1Click(Sender: TObject);
var
iBitrate: LongWord;
iHours: LongWord;
iMinutes: LongWord;
iSeconds: LongWord;
iMB: LongWord;
begin
// Time
iHours := 0;
iMinutes := 0;
iSeconds := 0;

if (Edit1.Text <> '0') then // Hours
iHours := StrToInt(Edit1.Text) * 60 * 60;
if (Edit2.Text <> '0') then // Minutes
iMinutes := StrToInt(Edit2.Text) * 60;
if (Edit3.Text <> '0') then // Seconds
iSeconds := StrToInt(Edit3.Text);

// MB --> kbps
iMB := StrToInt(Edit5.Text);
iBitrate := (iMB * 1024 * 1024 * 8) div (iHours + iMinutes + iSeconds) div 1000;

// Return
txtBitrate.Text := IntToStr(iBitrate);
end;I'm getting integer overflow. How can I bypass that?

esby
18th September 2004, 03:20
I suppose you are getting integer overflow for ibitrate.

If I were you, I'll try that:

ibitrate:real;

(...)

iBitrate := iMB * 1024 / 1000 * 1024 * 8 / (iHours + iMinutes + iSeconds);

txtBitrate.Text := IntToStr(trunc(iBitrate));

Sirber
18th September 2004, 03:35
Alright! It works! :cool:

Thanks a lot!!! :D

slavickas
21st September 2004, 15:48
or use int64

or simplify 1024*1024*8 div 1000 = 838860,8 =~838861 // still maybe too big for 32bits

esby
21st September 2004, 21:33
838861 is not too big for int32 as far I am remembering.
(2147483648 -2147483647; something like that)
Now, I'll avoid any integer calculation since there is always a risk of error range, especially since an int16 operation could be invoked during the operation with integer.

And I would not use int64 since there are not really standard.
using real is fine, especially since you can get a better precision while not doing an integer division.

esby