# Is there a general algorithm for conversion of units?

I'm not exactly sure where the best place to put this, as it's more of a general question about dimensional analysis.

I decided I was tired of having to convert units all of the time, and was not satisfied with the available python libraries for conversion of units. I decided to make my own, and it was simple enough to get write a rough outline of a unit conversion program using this article. However, this only works for units that are scalar multiples of others. Relative units such as Celsius do not work.

I switched to implementing conversions as graph of units to traverse, converting at each step of the way. I can convert $$m / s$$ to $$ft / hr$$ by doing repeatedly replacements. The problem comes that sometimes there is no substation. To make the conversion, $$\frac{kg*m^2}{s}=1000\frac{kg*L}{m*s},$$ you have to first multiply both sides by another unit before there is a valid conversion to be made.

Is the only possiblity to have quotient units multiply the top and bottom by every single unit? This seems horribly inefficient. Are there any other similar problems I'm missing?

• Do you intend to write your own code, or are you willing to use a small program that already does conversions? – David White Dec 8 '18 at 2:25
• My general method is the google search: google.se/search?q=5+btu+in+joule – user137289 Dec 8 '18 at 9:12

If I were writing my own python program to handle this question then I would probably decide to treat physical dimensions using vectors (i.e. one dimensional arrays). The elements of the array or vector represent the basic physical quantities such as mass, length, time, electric charge, etc. Then you have two types of conversion. On the one hand there is the definition of things like energy $$(M L^2/T^2$$) and power (energy $$/ T$$); on the other there is the conversion of units. These two concepts are closely related.
Just convert everything to a fixed set of base units (SI units would be a good choice). So you would set $$\mathrm{L} = 10^{-3} \mathrm{m}^{3}$$, $$\mathrm{ft} = 0.304 \mathrm{m}$$ etc. Once you've expressed the left and right hand side as a scalar times powers of your base units you just have to divide the scalars.
• @TheLoneMilkMan The problem is that Celsius is not really a unit: $5 K = 10^\circ C - 5^\circ \neq 5^\circ C = 278.15 K$. You should always convert any temperature to Kelvin (and temperature Differences always have to be expressed in Kelvin anyway) before doing arithmetic. BTW you can do the same for Fahrenheit just convert $^\circ F$ to the Rankine scale. – 0x539 Dec 8 '18 at 1:49