How to convert from Celsius to Kelvin, Fahrenheit, etc. with multiplication only

조회 수: 12 (최근 30일)
Jacob Miller
Jacob Miller 2017년 5월 23일
편집: Stephen23 2017년 5월 23일
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!

답변 (2개)

John D'Errico
John D'Errico 2017년 5월 23일
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
John D'Errico
John D'Errico 2017년 5월 23일
편집: John D'Errico 2017년 5월 23일
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
Stephen23 2017년 5월 23일
편집: Stephen23 2017년 5월 23일
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
Walter Roberson 2017년 5월 23일
편집: Walter Roberson 2017년 5월 23일
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.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by