Bug 378967

Summary: Fuel consumption conversion incorrect
Product: [Frameworks and Libraries] frameworks-kunitconversion Reporter: Radek Novacek <rad.n>
Component: generalAssignee: John Layt <jlayt>
Status: RESOLVED FIXED    
Severity: normal CC: kde, kdelibs-bugs, madcatx, mark_p._sanders, nate, simonandric5
Priority: NOR    
Version: 5.32.0   
Target Milestone: ---   
Platform: Fedora RPMs   
OS: Linux   
Latest Commit: Version Fixed In: 5.58
Sentry Crash Report:
Attachments: Screenshot from KRunner

Description Radek Novacek 2017-04-19 14:50:57 UTC
Fuel consumption unit conversion doesn't work for some unit combination. Discovered using KRunner Unit Converter plugin.

Expected values:
100 mpg (US) = 2.35215 l/100km
10 l/100 km = 23.5215 mpg (US)
20 kmpl = 5 l/100km

Actual values:
100 mpg (US) = 0.4251700680272109 l/100km
10 l/100 km = 2351.9999999999995 mpg (US)
20 kmpl = 0.2 l/100km

Given that kmpl to mpg conversion is correct (20kmpl = 47.04mpg) I guess that just l/100km is incorrect.

I've used Google Unit Conversion for checking the values (like "https://www.google.com/search?q=100 mpg to l/100km")
Comment 1 Radek Novacek 2017-04-19 14:54:30 UTC
Created attachment 105098 [details]
Screenshot from KRunner
Comment 2 Christoph Feck 2017-04-27 11:54:02 UTC
I doubt KUnitConversion handles reciprocals, so either the units should get removed, or some major rework is needed.
Comment 3 Christoph Feck 2018-01-02 18:13:29 UTC
*** Bug 387959 has been marked as a duplicate of this bug. ***
Comment 4 Kai Uwe Broulik 2018-01-02 18:30:35 UTC
It seems KUnitConversion cannot handle an inverse fraction.
n mpg = 253 / n l/100km
The code tries to do "something" in that direction by overriding toDefault()/fromDefault() but that clearly breaks in one or other form (either mpg→l/100km or, if you fix that, mpg→mpgi and mpg→km/l)
Comment 5 madcatx 2019-04-11 12:41:40 UTC
For those interested, please review the following patch

https://phabricator.kde.org/D20284
Comment 6 Albert Astals Cid 2019-04-14 15:59:46 UTC
Git commit bc7495095baa7a70e756c4d2044de2197d1a4e3b by Albert Astals Cid, on behalf of Michal Malý.
Committed on 14/04/2019 at 15:59.
Pushed by aacid into branch 'master'.

Fix l/100 km to MPG conversion

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.

Reviewers: broulik, #frameworks, aacid

Subscribers: apol, aacid, meven, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D20284

M  +20   -0    autotests/convertertest.cpp
M  +23   -9    src/fuel_efficiency.cpp

https://commits.kde.org/kunitconversion/bc7495095baa7a70e756c4d2044de2197d1a4e3b