How to convert from Celsius to Kelvin, Fahrenheit, etc. with multiplication only
12 views (last 30 days)
Show older comments
Hello,
I recently found a handy units/dimensions script that generates a structure of conversion values. Effectively, you can multiply by one of the elements of the structure to convert into SI units, and divide to convert back to whatever units you wish.
For example, if I want to convert 30 oz. to lb., I could write:
myMass = 30*u.oz/u.lb
where u.oz = 0.028 and u.lb = 0.454. This works great for any conversions where I multiply by a constant. Where it breaks down is any conversion that requires a scalar offset, such as temperature. Here, I would like to be able to convert between temperature units using the same method. Is this possible? For example, say I want to convert 30 °C to °F. The code I want would look like this.
myTemp = 30*u.C/u.F
However, converting Celsius to Kelvin requires adding 273.15, not multiplying by anything. And converting between Kelvin and Fahrenheit requires both an addition and a multiplication. So how would I define u.C and u.F here? I would like to keep the conversion similar to the other units, so you always multiply and divide and don't need to add and subtract. Is it possible to do this? I've thought about it for a while, and can't seem to come up with a method.
Thanks for the help!
0 Comments
Answers (2)
John D'Errico
on 23 May 2017
No. There is no conversion between degrees F, degrees C, or degrees K that will not involve an additive (or subtractive) offset.
You can want for something to exist, but the laws of mathematics won't listen to your prayers.
3 Comments
John D'Errico
on 23 May 2017
Edited: John D'Errico
on 23 May 2017
But why? It hardly seems worth the bother, to hope to find some programming trick just to avoid an explicit add or subtract. Note the danger here - if you did find some programming quirk that would work here, it would be a great way to create impossible to read code, code that is just asking for bugs.
The best code is easy to read, understand, and debug. To rely on a trick (even if that trick exists) to avoid an explicit addition is silly, since you will still need to do that addition somewhere.
Stephen23
on 23 May 2017
Edited: Stephen23
on 23 May 2017
This would obfuscate the actual conversion behind some hacks: it is not a multiplication or division, so hiding the actual conversion is just going to be misleading. Considering the mathematical simplicity of the conversion, there does not seem to be any advantage to this.
Simpler and clearer would be to simply write a function and call that:
otp = celsius_2_fahrenheit(inp)
Walter Roberson
on 23 May 2017
Edited: Walter Roberson
on 23 May 2017
u = symunit;
rewrite(30*u.Celsius,u.Fahrenheit,'Temperature','absolute')
Caution: u.C is coulomb and u.F is farad !'
Notice that there is also
rewrite(30*u.Celsius,u.Fahrenheit,'Temperature','difference')
which is for calculating how many degrees Fahrenheit change a change of 30 Celsius would be. That corresponds to the calculation you have been doing, and is a useful calculation in its own right. You need to be concerned not only with absolute temperatures but with relative temperatures.
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!