Skip to content

Commit

Permalink
nonzero() macro to prevent FP zerodiv
Browse files Browse the repository at this point in the history
  • Loading branch information
mitrich-k committed Nov 8, 2016
1 parent 8555848 commit cbcb721
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions idp/downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,8 @@ void Downloader::updateSpeed(NetFile *file, Timer *timer)
{
if(ui)
{
double speed = (double)file->bytesDownloaded / ((double)timer->totalElapsed() / 1000.0);
double rtime = (double)(filesSize - (downloadedFilesSize + file->bytesDownloaded)) / speed * 1000.0;
double speed = (double)file->bytesDownloaded / nonzero((double)timer->totalElapsed() / 1000.0);
double rtime = (double)(filesSize - (downloadedFilesSize + file->bytesDownloaded)) / nonzero(speed) * 1000.0;

if((filesSize == FILE_SIZE_UNKNOWN) || ((downloadedFilesSize + file->bytesDownloaded) > filesSize))
ui->setSpeedInfo(f2i(speed));
Expand Down
4 changes: 2 additions & 2 deletions idp/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ void Ui::setProgressInfo(DWORDLONG totalSize, DWORDLONG totalDownloaded, DWORDLO
{
if(!(totalSize == FILE_SIZE_UNKNOWN))
{
double totalPercents = 100.0 / ((double)totalSize / (double)totalDownloaded);
double totalPercents = 100.0 / nonzero((double)totalSize / i2d(totalDownloaded));
setProgressBarPos(controls["TotalProgressBar"], f2i(totalPercents));
}

if(!(fileSize == FILE_SIZE_UNKNOWN))
{
double filePercents = 100.0 / ((double)fileSize / (double)fileDownloaded);
double filePercents = 100.0 / nonzero((double)fileSize / i2d(fileDownloaded));
setProgressBarPos(controls["FileProgressBar"], f2i(filePercents));
}
}
Expand Down
2 changes: 2 additions & 0 deletions idp/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ class Ui
HWND uiMainWindow();

#define f2i(x) (_isnan(x) ? 0 : (int)(x))
#define i2d(x) ((x == 0) ? FLT_MIN : (double)x)
#define nonzero(x) ((x == 0.0) ? FLT_MIN : (double)x)

extern "C" void idpReportError();

0 comments on commit cbcb721

Please sign in to comment.