[PATCH] O2/He percentages aren't integral

Linus Torvalds torvalds at linux-foundation.org
Wed Jun 19 18:48:44 PDT 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 19 Jun 2013 15:40:15 -1000
Subject: [PATCH] O2/He percentages aren't integral

We do gas mixes in permille, not in percent.  Some people really like
using the value they got from the analyzer, which is generally something
like 29.4% or whatever.  So don't truncate percentages to integers.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

This isn't exactly heavily tested, especially since I'm not personally one 
of those people who actually care about the gasmix percentages to within 
one decimal digit (or even a few percent). But I hate seeing us throwing 
the information away for no good reason.

I do note that we don't properly round our floating point to integer 
things in many places. Using +0.5 is probably wrong too, since it does the 
wrong thing for negative values. Not that it matters for gasmixes, but 
we've had that issue in some other places (temperatures) where negative 
values really do happen.

So maybe we should go through our "+0.5" usage and use "rint()"? And make 
the cuft->mliter conversions do rounding too etc. Not that it much 
matters, but still.. Not doing rounding right can cause some annoying 
effects even if mliter is sufficient precision - getting "79.999" cuft 
instead of "80.0" or similar effects.

This does *not* do that. I only did the percentages. I'd also like to 
print "30" as just that, not as "30.0" (for us non-anal people), but I 
don't know how to make QString do that. We should also do 0 as the empty 
string, not as "0.0%".

Details, details. In the meantime, this is a fairly minimal patch fixing a 
very particular issue. Maybe somebody else would want to get their hands 
dirty and get aquainted with subsurface with the proper conversion logic?

 qt-ui/models.cpp | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 1b882f3bf166..489f91bb6eb7 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -85,8 +85,8 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
 			// sizes take working pressure into account...
 			if (cyl->type.size.mliter) {
 				if (prefs.units.volume == prefs.units.CUFT) {
-					int cuft = 0.5 + ml_to_cuft(gas_volume(cyl, cyl->type.workingpressure));
-					ret = QString("%1cuft").arg(cuft);
+					double cuft = ml_to_cuft(gas_volume(cyl, cyl->type.workingpressure));
+					ret = QString("%1cuft").arg(cuft, 0, 'f', 1);
 				} else {
 					ret = QString("%1l").arg(cyl->type.size.mliter / 1000.0, 0, 'f', 1);
 				}
@@ -102,13 +102,13 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const
 			break;
 		case END:
 			if (cyl->end.mbar)
-				ret = get_pressure_string(cyl->end, TRUE	);
+				ret = get_pressure_string(cyl->end, TRUE);
 			break;
 		case O2:
-			ret = QString("%1%").arg((cyl->gasmix.o2.permille + 5) / 10);
+			ret = QString("%1%").arg(cyl->gasmix.o2.permille / 10.0, 0, 'f', 1);
 			break;
 		case HE:
-			ret = QString("%1%").arg((cyl->gasmix.he.permille + 5) / 10);
+			ret = QString("%1%").arg(cyl->gasmix.he.permille / 10.0, 0, 'f', 1);
 			break;
 		}
 		break;
@@ -227,14 +227,14 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
 		}
 		break;
 	case O2:
-		if (CHANGED(toInt, "%", "%")) {
-			cyl->gasmix.o2.permille = value.toInt() * 10;
+		if (CHANGED(toDouble, "%", "%")) {
+			cyl->gasmix.o2.permille = value.toDouble() * 10 + 0.5;
 			mark_divelist_changed(TRUE);
 		}
 		break;
 	case HE:
-		if (CHANGED(toInt, "%", "%")) {
-			cyl->gasmix.he.permille = value.toInt() * 10;
+		if (CHANGED(toDouble, "%", "%")) {
+			cyl->gasmix.he.permille = value.toDouble() * 10 + 0.5;
 			mark_divelist_changed(TRUE);
 		}
 		break;
@@ -408,7 +408,7 @@ bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int r
 			if (prefs.units.weight == prefs.units.LBS)
 				ws->weight.grams = lbs_to_grams(value.toDouble());
 			else
-				ws->weight.grams = value.toDouble() * 1000.0;
+				ws->weight.grams = value.toDouble() * 1000.0 + 0.5;
 			// now update the ws_info
 			WSInfoModel *wsim = WSInfoModel::instance();
 			QModelIndexList matches = wsim->match(wsim->index(0,0), Qt::DisplayRole, ws->description);
-- 
1.8.3



More information about the subsurface mailing list