Bobby
Bobby

Reputation: 11576

How derive a formula for (measurement) unit conversion of arbitrary compound units?

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):

  1. Split the compound unit into its components: km and h, ft and s
  2. Match the units against each other: km and ft,hands`
  3. Derive a formula from this point:
    1. Convert all prefixed units to their bases: km to 1000m
    2. Get the conversion factor between units and place that in the formula.

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?

Upvotes: 1

Views: 358

Answers (1)

Amongalen
Amongalen

Reputation: 3131

I'm not sure how exactly to solve your problem but here is my idea what is wrong.

In general your approach seems to work fine. The problem is that temperature is probably a special case. What actually would F/h or C/s represent? I don't think there is such concept in physics. What you have probably thought about is change of temperature in time, which would be (delta C) / h. Your formula for converting delta C to delta F would look like this I believe: (x1 * (9/5) + 32) - (x2 * (9/5) + 32) = (x1-x2)*(9/5). Then you can use that in the formula for converting complex units.

I can't say on top of my head if there are more cases like this one, you will have to check other units with weird convertion ratios as well.

I hope this is helpful enough and will guide you into solving your problem.

Upvotes: 1

Related Questions