title: How derive a formula for (measurement) unit conversion of arbitrary compound units?
body: <p>I'm developing a <a href="https://gitlab.com/RobertZenz/jMathPaper/" rel="nofollow noreferrer">calculator application which also does allow to convert between units</a>. For this I want to provide the feature to not only convert between arbitrary (measurement) units, but also between arbitrary compound units, like <code>km/h</code> to <code>m/s</code>. First, let me start with the structure I have so that you understand setup.</p>
  
  <p>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:</p>
  
  <pre><code># 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
  </code></pre>
  
  <p>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.</p>
  
  <p>My problem starts with unit conversions which are not consisting of a simple factor, for example temperature conversions:</p>
  
  <pre><code>celsius     (x * (9/5) + 32)°F
  fahrenheit  ((x - 32) * (5/9))°C
  </code></pre>
  
  <p>As you can see, I already have a system in place which allows to place arbitrary formulas as conversion with <code>x</code> as the number that is being converted. Now, the main problem are compound units, for example when I want to convert <code>km/h</code> to <code>ft/s</code>. My <a href="https://gitlab.com/RobertZenz/jMathPaper/-/blob/69c0596ed5fd2927a7e4525c64c601ed51a2e571/src/org/bonsaimind/jmathpaper/core/units/UnitConverter.java#L564" rel="nofollow noreferrer">current approach is to as follows (warning, not for the faint of heart)</a>:</p>
  
  <ol>
  <li>Split the compound unit into its components: <code>km</code> and <code>h</code>, <code>ft</code> and <code>s</code></li>
  <li>Match the units against each other: <code>km</code> and <code>ft,</code>h<code>and</code>s`</li>
  <li>Derive a formula from this point:
  
  <ol>
  <li>Convert all prefixed units to their bases: <code>km</code> to <code>1000m</code></li>
  <li>Get the conversion factor between units and place that in the formula.</li>
  </ol></li>
  </ol>
  
  <p>That yields <code>((1 * 1000) * 3.28) / (3600)</code> as a derivation formula, and a factor of <code>0.911...</code> roughly, which I then use to convert the given value to <code>ft/s</code>. So far, so well.</p>
  
  <p>Now, when we think back on the temperature conversions, this whole approach falls apart because I'm using <code>1</code> as value in the formula. Assuming I want to convert <code>C/s</code> to <code>F/h</code> I'm currently deriving the formula <code>(1 * (9/5) + 32) / (0.0002777)</code> and using the resulting factor for the conversion, which is plain out wrong.</p>
  
  <p>My second approach was to insert the value (assume <code>9</code>) at each unit conversion, which gives me <code>(9 * (9/5) + 32) / (9 * 0.0002777)</code>, 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.</p>
  
  <p>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?</p>