Fix l/100 km to MPG conversion
ClosedPublic

Authored by madcatx on Apr 6 2019, 6:46 AM.

Details

Summary

Previous code (probably) worked only for l/100 km -> MPG but not the other way around. Fix this by using reciprocal conversion when necessary.

Output:
38.7 MPG -> 6.077 l/100 km; 16.455 km/l; 46.482 MPGI
16.455 km/l -> 6.077 l/100 km; 38.7 MPG
46.48 MPGI -> 6.077 l/100 km

Seems good to me.

BUG: 378967

Diff Detail

Repository
R292 KUnitConversion
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
madcatx created this revision.Apr 6 2019, 6:46 AM
Restricted Application added a project: Frameworks. · View Herald TranscriptApr 6 2019, 6:46 AM
Restricted Application added a subscriber: kde-frameworks-devel. · View Herald Transcript
madcatx requested review of this revision.Apr 6 2019, 6:46 AM
meven added a subscriber: meven.Apr 6 2019, 6:59 AM
meven added inline comments.
src/fuel_efficiency.cpp
48

Indentation seems off here

madcatx updated this revision to Diff 55512.Apr 6 2019, 7:03 AM

Tabs vs spaces

aacid added a subscriber: aacid.Apr 6 2019, 3:06 PM

is having an autotest for this possible?

madcatx updated this revision to Diff 55574.Apr 6 2019, 3:38 PM
  1. Tweaked conversion factor to match those used by Google online converter
  2. Added test to convert l/100 km to other representations and back
ngraham edited the summary of this revision. (Show Details)Apr 6 2019, 10:23 PM
ngraham added reviewers: Frameworks, aacid.
apol added a subscriber: apol.Apr 8 2019, 12:11 AM
apol added inline comments.
src/fuel_efficiency.cpp
36

I'm not sure I understand what isReciprocal means here.

madcatx added inline comments.Apr 8 2019, 2:56 AM
src/fuel_efficiency.cpp
36

l/100 km (amount of fuel needed to cross a given distance) is reciprocal to MPG (distance crossed with a given amount of fuel). KUnitConverter can only scale units by a given factor, not invert them which is what is needed to make this work. isReciprocal flag denotes that the conversion between the base unit (l/100 km) and the target unit shall be reciprocal.

madcatx marked an inline comment as done.Apr 11 2019, 10:06 AM
aacid added inline comments.Apr 13 2019, 9:34 PM
autotests/convertertest.cpp
71

Could you please also add a test where the initial value is in say MPG and then we have the conversion to liters/100km, mpgi and l/100 km?

src/fuel_efficiency.cpp
36

I understand what you mean here, but i don't think that reciprocal is the word that describes this (are you a native speaker? if so maybe it's juts that my english is bad :D)

Oh, it's actually called reciprocal number too, i think here we use the inverse naming for it https://en.wikipedia.org/wiki/Multiplicative_inverse

Maybe naming it "m_isReciprocaltoDefaultUnit" would make it understand? i.e. makes it clear that that value is in relation to the default unit?

madcatx updated this revision to Diff 56201.Apr 14 2019, 2:04 PM

Added an inverse test with initial value in MPG

madcatx updated this revision to Diff 56203.Apr 14 2019, 2:14 PM

More obvious explanation of the meaning of m_isReciprocal

madcatx marked 2 inline comments as done.Apr 14 2019, 2:18 PM
madcatx added inline comments.
src/fuel_efficiency.cpp
36

Direct translation from my native language would be "inverse" too, although somehow I prefer the word "reciprocal" as it feels less ambiguous. (People may perceive "inversion" in multiple ways but "reciprocity" has a pretty solid definition as far as math goes... IMHO :) )

Regardless, I changed the name and added an explanatory comment.

madcatx marked an inline comment as done.Apr 14 2019, 2:22 PM
aacid accepted this revision.Apr 14 2019, 3:57 PM
This revision is now accepted and ready to land.Apr 14 2019, 3:57 PM
This revision was automatically updated to reflect the committed changes.