Fix double to int truncation in C++ code

Wfloat-conversion enabled for C++ part of the code
Fix warnings raised by the flag using lrint

Original issue reported on the mailing list:
The ascent/descent rates are sometimes not what is expected.
E.g. setting the ascent rate to 10m/min results in an actual
ascent rate of 9m/min.
This is due to truncating the ascent rate preference,
then effectively rounding up the time to reach each stop to 2s intervals.
The result being that setting the ascent rate to 10m/min
results in 20s to ascend 3m (9m/min), when it should be exactly 18s.

Reported-by: John Smith <noseygit@hotmail.com>
Reported-by: Rick Walsh <rickmwalsh@gmail.com>
Signed-off-by: Jeremie Guichard <djebrest@gmail.com>
This commit is contained in:
Jeremie Guichard 2017-03-23 08:13:49 +07:00 committed by Dirk Hohndel
parent d83449f3b5
commit 597539ce39
25 changed files with 73 additions and 72 deletions

View file

@ -494,11 +494,11 @@ void ConfigureDiveComputerDialog::populateDeviceDetailsOSTC3()
deviceDetails->dynamicAscendRate = ui.dynamicAscendRate->isChecked();
deviceDetails->graphicalSpeedIndicator = ui.graphicalSpeedIndicator->isChecked();
deviceDetails->alwaysShowppO2 = ui.alwaysShowppO2->isChecked();
deviceDetails->tempSensorOffset = ui.tempSensorOffsetDoubleSpinBox->value() * 10;
deviceDetails->tempSensorOffset = lrint(ui.tempSensorOffsetDoubleSpinBox->value() * 10);
deviceDetails->safetyStopLength = ui.safetyStopLengthSpinBox->value();
deviceDetails->safetyStopStartDepth = ui.safetyStopStartDepthDoubleSpinBox->value() * 10;
deviceDetails->safetyStopEndDepth = ui.safetyStopEndDepthDoubleSpinBox->value() * 10;
deviceDetails->safetyStopResetDepth = ui.safetyStopResetDepthDoubleSpinBox->value() * 10;
deviceDetails->safetyStopStartDepth = lrint(ui.safetyStopStartDepthDoubleSpinBox->value() * 10);
deviceDetails->safetyStopEndDepth = lrint(ui.safetyStopEndDepthDoubleSpinBox->value() * 10);
deviceDetails->safetyStopResetDepth = lrint(ui.safetyStopResetDepthDoubleSpinBox->value() * 10);
//set gas values
gas gas1;
@ -612,7 +612,7 @@ void ConfigureDiveComputerDialog::populateDeviceDetailsOSTC()
deviceDetails->desaturation = ui.desaturationSpinBox_3->value();
deviceDetails->lastDeco = ui.lastDecoSpinBox_3->value();
deviceDetails->samplingRate = ui.samplingRateSpinBox_3->value();
deviceDetails->salinity = ui.salinityDoubleSpinBox_3->value() * 100;
deviceDetails->salinity = lrint(ui.salinityDoubleSpinBox_3->value() * 100);
deviceDetails->dateFormat = ui.dateFormatComboBox_3->currentIndex();
deviceDetails->syncTime = ui.dateTimeSyncCheckBox_3->isChecked();
deviceDetails->safetyStop = ui.safetyStopCheckBox_3->isChecked();
@ -629,9 +629,9 @@ void ConfigureDiveComputerDialog::populateDeviceDetailsOSTC()
deviceDetails->decoGasConsumption = ui.decoGasConsumption_3->value();
deviceDetails->graphicalSpeedIndicator = ui.graphicalSpeedIndicator_3->isChecked();
deviceDetails->safetyStopLength = ui.safetyStopLengthSpinBox_3->value();
deviceDetails->safetyStopStartDepth = ui.safetyStopStartDepthDoubleSpinBox_3->value() * 10;
deviceDetails->safetyStopEndDepth = ui.safetyStopEndDepthDoubleSpinBox_3->value() * 10;
deviceDetails->safetyStopResetDepth = ui.safetyStopResetDepthDoubleSpinBox_3->value() * 10;
deviceDetails->safetyStopStartDepth = lrint(ui.safetyStopStartDepthDoubleSpinBox_3->value() * 10);
deviceDetails->safetyStopEndDepth = lrint(ui.safetyStopEndDepthDoubleSpinBox_3->value() * 10);
deviceDetails->safetyStopResetDepth = lrint(ui.safetyStopResetDepthDoubleSpinBox_3->value() * 10);
//set gas values
gas gas1;

View file

@ -228,7 +228,7 @@ void DivePlannerWidget::heightChanged(const int height)
void DivePlannerWidget::salinityChanged(const double salinity)
{
/* Salinity is expressed in weight in grams per 10l */
plannerModel->setSalinity(10000 * salinity);
plannerModel->setSalinity(lrint(10000 * salinity));
}
void PlannerSettingsWidget::bottomSacChanged(const double bottomSac)
@ -478,27 +478,27 @@ void PlannerSettingsWidget::printDecoPlan()
void PlannerSettingsWidget::setAscRate75(int rate)
{
SettingsObjectWrapper::instance()->planner_settings->setAscrate75(rate * UNIT_FACTOR);
SettingsObjectWrapper::instance()->planner_settings->setAscrate75(lrint(rate * UNIT_FACTOR));
}
void PlannerSettingsWidget::setAscRate50(int rate)
{
SettingsObjectWrapper::instance()->planner_settings->setAscrate50(rate * UNIT_FACTOR);
SettingsObjectWrapper::instance()->planner_settings->setAscrate50(lrint(rate * UNIT_FACTOR));
}
void PlannerSettingsWidget::setAscRateStops(int rate)
{
SettingsObjectWrapper::instance()->planner_settings->setAscratestops(rate * UNIT_FACTOR);
SettingsObjectWrapper::instance()->planner_settings->setAscratestops(lrint(rate * UNIT_FACTOR));
}
void PlannerSettingsWidget::setAscRateLast6m(int rate)
{
SettingsObjectWrapper::instance()->planner_settings->setAscratelast6m(rate * UNIT_FACTOR);
SettingsObjectWrapper::instance()->planner_settings->setAscratelast6m(lrint(rate * UNIT_FACTOR));
}
void PlannerSettingsWidget::setDescRate(int rate)
{
SettingsObjectWrapper::instance()->planner_settings->setDescrate(rate * UNIT_FACTOR);
SettingsObjectWrapper::instance()->planner_settings->setDescrate(lrint(rate * UNIT_FACTOR));
}
void PlannerSettingsWidget::sacFactorChanged(const double factor)

View file

@ -126,7 +126,7 @@ void DownloadFromDCWidget::updateProgressBar()
} else {
ui.progressBar->setFormat("%p%");
}
ui.progressBar->setValue(progress_bar_fraction * 100);
ui.progressBar->setValue(lrint(progress_bar_fraction * 100));
}
void DownloadFromDCWidget::updateState(states state)

View file

@ -267,7 +267,7 @@ void GlobeGPS::centerOnDiveSite(struct dive_site *ds)
// otherwise check to make sure we aren't still running an animation and then remember
// the current zoom level
if (currentZoomLevel == -1) {
currentZoomLevel = zoomFromDistance(3.0);
currentZoomLevel = lrint(zoomFromDistance(3.0));
centerOn(longitude, latitude);
fixZoom(true);
return;

View file

@ -139,9 +139,9 @@ QSize GroupedLineEdit::sizeHint() const
{
QSize rs(
40,
document()->findBlock(0).layout()->lineAt(0).height() +
lrint(document()->findBlock(0).layout()->lineAt(0).height() +
document()->documentMargin() * 2 +
frameWidth() * 2);
frameWidth() * 2));
return rs;
}

View file

@ -177,8 +177,8 @@ void KMessageWidgetPrivate::updateSnapShot()
void KMessageWidgetPrivate::slotTimeLineChanged(qreal value)
{
q->setFixedHeight(qMin(value * 2, qreal(1.0)) * content->height());
q->update();
q->setFixedHeight(lrint(qMin(value * 2, qreal(1.0)) * content->height()));
q->update();
}
void KMessageWidgetPrivate::slotTimeLineFinished()

View file

@ -156,8 +156,8 @@ void LocationInformationWidget::acceptChanges()
if (!ui.diveSiteCoordinates->text().isEmpty()) {
double lat, lon;
parseGpsText(ui.diveSiteCoordinates->text(), &lat, &lon);
currentDs->latitude.udeg = lat * 1000000.0;
currentDs->longitude.udeg = lon * 1000000.0;
currentDs->latitude.udeg = (int)(lat * 1000000.0);
currentDs->longitude.udeg = (int)(lon * 1000000.0);
}
if (dive_site_is_empty(currentDs)) {
LocationInformationModel::instance()->removeRow(get_divesite_idx(currentDs));
@ -232,8 +232,8 @@ void LocationInformationWidget::on_diveSiteCoordinates_textChanged(const QString
if (!same_string(qPrintable(text), coords)) {
double latitude, longitude;
if (parseGpsText(text, &latitude, &longitude)) {
displayed_dive_site.latitude.udeg = latitude * 1000000;
displayed_dive_site.longitude.udeg = longitude * 1000000;
displayed_dive_site.latitude.udeg = (int)(latitude * 1000000);
displayed_dive_site.longitude.udeg = (int)(longitude * 1000000);
markChangedWidget(ui.diveSiteCoordinates);
emit coordinatesChanged();
ui.geoCodeButton->setEnabled(latitude != 0 && longitude != 0);

View file

@ -629,7 +629,7 @@ void MainTab::updateDiveInfo(bool clear)
continue;
volumes.append(get_volume_string(gases[i], true));
if (duration[i]) {
sac.mliter = gases[i].mliter / (depth_to_atm(mean[i], &displayed_dive) * duration[i] / 60);
sac.mliter = lrint(gases[i].mliter / (depth_to_atm(mean[i], &displayed_dive) * duration[i] / 60));
SACs.append(get_volume_string(sac, true).append(tr("/min")));
}
}

View file

@ -1041,8 +1041,8 @@ void MainWindow::on_actionYearlyStatistics_triggered()
QTreeView *view = new QTreeView();
view->setModel(m);
l->addWidget(view);
d.resize(width() * .8, height() / 2);
d.move(width() * .1, height() / 4);
d.resize(lrint(width() * .8), height() / 2);
d.move(lrint(width() * .1), height() / 4);
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), &d);
connect(close, SIGNAL(activated()), &d, SLOT(close()));
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), &d);
@ -1105,19 +1105,19 @@ void MainWindow::on_actionViewAll_triggered()
const int appH = qApp->desktop()->size().height();
const int appW = qApp->desktop()->size().width();
if (mainSizes.empty()) {
mainSizes.append(appH * 0.7);
mainSizes.append(appH * 0.3);
mainSizes.append(lrint(appH * 0.7));
mainSizes.append(lrint(appH * 0.3));
}
static QList<int> infoProfileSizes;
if (infoProfileSizes.empty()) {
infoProfileSizes.append(appW * 0.3);
infoProfileSizes.append(appW * 0.7);
infoProfileSizes.append(lrint(appW * 0.3));
infoProfileSizes.append(lrint(appW * 0.7));
}
static QList<int> listGlobeSizes;
if (listGlobeSizes.empty()) {
listGlobeSizes.append(appW * 0.7);
listGlobeSizes.append(appW * 0.3);
listGlobeSizes.append(lrint(appW * 0.7));
listGlobeSizes.append(lrint(appW * 0.3));
}
QSettings settings;

View file

@ -526,18 +526,18 @@ print_part:
if (option.state & QStyle::State_Selected) {
painter->setPen(QPen(opt.palette.highlight().color().darker()));
painter->setBrush(opt.palette.highlight());
const qreal pad = 1.0;
const qreal pad2 = pad * 2.0;
const qreal rounding = 5.0;
const int pad = 1;
const int pad2 = pad * 2;
const int rounding = 5;
painter->drawRoundedRect(option.rect.x() + pad,
option.rect.y() + pad,
option.rect.width() - pad2,
option.rect.height() - pad2,
rounding, rounding);
option.rect.y() + pad,
option.rect.width() - pad2,
option.rect.height() - pad2,
rounding, rounding);
}
painter->setPen(textPen);
painter->setFont(fontBigger);
const qreal textPad = 5.0;
const int textPad = 5;
painter->drawText(option.rect.x() + textPad, option.rect.y() + fmBigger.boundingRect("YH").height(), diveSiteName);
double pointSize = fontSmaller.pointSizeF();
fontSmaller.setPointSizeF(0.9 * pointSize);

View file

@ -104,7 +104,7 @@ void Printer::flowRender()
webView->page()->mainFrame()->scroll(0, dontbreakElement.geometry().y() - start);
// rendering progress is 4/5 of total work
emit(progessUpdated((end * 80.0 / fullPageResolution) + done));
emit(progessUpdated(lrint((end * 80.0 / fullPageResolution) + done)));
// add new pages only in print mode, while previewing we don't add new pages
if (printMode == Printer::PRINT)
@ -183,7 +183,7 @@ void Printer::render(int Pages = 0)
viewPort.adjust(0, pageSize.height(), 0, pageSize.height());
// rendering progress is 4/5 of total work
emit(progessUpdated((i * 80.0 / Pages) + done));
emit(progessUpdated(lrint((i * 80.0 / Pages) + done)));
if (i < Pages - 1 && printMode == Printer::PRINT)
static_cast<QPrinter*>(paintDevice)->newPage();
}

View file

@ -17,7 +17,7 @@ TemplateEdit::TemplateEdit(QWidget *parent, struct print_options *printOptions,
// restore the settings and init the UI
ui->fontSelection->setCurrentIndex(templateOptions->font_index);
ui->fontsize->setValue(templateOptions->font_size);
ui->fontsize->setValue(lrint(templateOptions->font_size));
ui->colorpalette->setCurrentIndex(templateOptions->color_palette_index);
ui->linespacing->setValue(templateOptions->line_spacing);
ui->borderwidth->setValue(templateOptions->border_width);

View file

@ -105,7 +105,7 @@ QString TemplateLayout::generate()
DiveObjectHelper *d = new DiveObjectHelper(dive);
diveList.append(QVariant::fromValue(d));
progress++;
emit progressUpdated(progress * 100.0 / totalWork);
emit progressUpdated(lrint(progress * 100.0 / totalWork));
}
Grantlee::Context c;
c.insert("dives", diveList);