I'm developing a calculator application which also does allow to convert between units. For this I want to provide the feature to not only convert between arbitrary (measurement) units, but also between arbitrary compound units, like km/h
to m/s
. First, let me start with the structure I have so that you understand setup.
I have all units and conversions specified in configuration files to give the user the possibility to extend the system with more units. The overall layout of the files (wrapped into one here) looks like this:
# This is a comment.
# Unit declaration
# Name Dimension Aliases
meter 1 Metre, metre, m
foot 1 ft, feet
second 1 s,sec
hour 1 h
# Prefix
# Name Alias Base Exponent
centi c 10 -2
kilo k 10 3
# Conversion
foot 30.48cm
hour 3600sec
These are all then wrapped into types/objects for processing. Finding "hops" between arbitrary units is fully implemented, that means that the system can already find a path to convert between units which do not have a direct conversion assigned. For example, I only could specify an hour to be 60 minutes, and a minute as 60 seconds and the system could convert 1 hour into 3600 seconds. That is all working. Also fully working is the prefix support.
My problem starts with unit conversions which are not consisting of a simple factor, for example temperature conversions:
celsius (x * (9/5) + 32)°F
fahrenheit ((x - 32) * (5/9))°C
As you can see, I already have a system in place which allows to place arbitrary formulas as conversion with x
as the number that is being converted. Now, the main problem are compound units, for example when I want to convert km/h
to ft/s
. My current approach is to as follows (warning, not for the faint of heart):
- Split the compound unit into its components:
km
andh
,ft
ands
- Match the units against each other:
km
andft,
hand
s` - Derive a formula from this point:
- Convert all prefixed units to their bases:
km
to1000m
- Get the conversion factor between units and place that in the formula.
- Convert all prefixed units to their bases:
That yields ((1 * 1000) * 3.28) / (3600)
as a derivation formula, and a factor of 0.911...
roughly, which I then use to convert the given value to ft/s
. So far, so well.
Now, when we think back on the temperature conversions, this whole approach falls apart because I'm using 1
as value in the formula. Assuming I want to convert C/s
to F/h
I'm currently deriving the formula (1 * (9/5) + 32) / (0.0002777)
and using the resulting factor for the conversion, which is plain out wrong.
My second approach was to insert the value (assume 9
) at each unit conversion, which gives me (9 * (9/5) + 32) / (9 * 0.0002777)
, which also is not the right thing to do. I mean, the case of such temperature conversions are much more complicated, too. So there is still a certain knowledge gap on my end.
I'm quite at a loss here and have a hard time locating resources or solutions to this problem. Can somebody shed some light on how to do this properly?