Saturday, February 23, 2008

Temperature Conversion

I promised we'd do something different this week, and that something different is temperature conversion. We're going to write some code to convert between Celsius, Fahrenheit, and Kelvin - the three most popular units of measurement for temperature.


ASP

  1. function CelsiusToFahrenheit(temperature)
  2.     CelsiusToFahrenheit = (temperature * 9 / 5) + 32
  3. end function
  4. function CelsiusToKelvin(temperature)
  5.     CelsiusToKelvin = temperature + 273.15
  6. end function
  7. function FahrenheitToCelsius(temperature)
  8.     FahrenheitToCelsius = (temperature - 32) * 5 / 9
  9. end function
  10. function FahrenheitToKelvin(temperature)
  11.     FahrenheitToKelvin = (temperature - 32) * 5 / 9 + 273.15
  12. end function
  13. function KelvinToCelsius(temperature)
  14.     KelvinToCelsius = temperature - 273.15
  15. end function
  16. function KelvinToFahrenheit(temperature)
  17.     KelvinToFahrenheit = (temperature - 273.15) * 9 / 5 + 32
  18. end function

PHP

  1. function CelsiusToFahrenheit($temperature)
  2. {
  3.     return ($temperature * 9 / 5) + 32;
  4. }
  5. function CelsiusToKelvin($temperature)
  6. {
  7.     return $temperature + 273.15;
  8. }
  9. function FahrenheitToCelsius($temperature)
  10. {
  11.     return ($temperature - 32) * 5 / 9;
  12. }
  13. function FahrenheitToKelvin($temperature)
  14. {
  15.     return ($temperature - 32) * 5 / 9 + 273.15;
  16. }
  17. function KelvinToCelsius($temperature)
  18. {
  19.     return $temperature - 273.15;
  20. }
  21. function KelvinToFahrenheit($temperature)
  22. {
  23.     return ($temperature - 273.15) * 9 / 5 + 32;
  24. }

You may have noticed that the conversion between Celsius and Kelvin is a simple addition or subtraction. I could have written the code such that some functions make use of other functions, but I decided to write each function to be self-sufficient in performing it's calculations. The reason for this is speed; calling an extra function would give a small performance hit. You could consider this contrary to how I wrote the trigonometry functions, but in those cases, the calculations were more complicated.


Ok, so now what? Let's write a facade function to make it easier to call the individual conversion functions.


ASP

  1. function convertTemperature(temperature, originScale, targetScale)
  2.     ' Return the input value for temperature if both scales are identical.
  3.     if UCase(originScale) = UCase(targetScale) then
  4.         convertTemperature = temperature
  5.         exit function
  6.     end if
  7.     Select Case UCase(originScale)
  8.     Case "CELSIUS"
  9.         Select Case UCase(targetScale)
  10.         Case "FAHRENHEIT"
  11.             convertTemperature = CelsiusToFahrenheit(temperature)
  12.         Case "KELVIN"
  13.             convertTemperature = CelsiusToKelvin(temperature)
  14.         End Select
  15.     Case "FAHRENHEIT"
  16.         Select Case UCase(targetScale)
  17.         Case "CELSIUS"
  18.             convertTemperature = FahrenheitToCelsius(temperature)
  19.         Case "KELVIN"
  20.             convertTemperature = FahrenheitToKelvin(temperature)
  21.         End Select
  22.     Case "KELVIN"
  23.         Select Case UCase(targetScale)
  24.         Case "CELSIUS"
  25.             convertTemperature = KelvinToCelsius(temperature)
  26.         Case "FAHRENHEIT"
  27.             convertTemperature = KelvinToFahrenheit(temperature)
  28.         End Select
  29.     End Select
  30. end function

PHP

  1. function convertTemperature($temperature, $originScale, $targetScale)
  2. {
  3.     // Return the input value for temperature if both scales are identical.
  4.     if (strtoupper($originScale) == strtoupper($targetScale))
  5.     {
  6.         return $temperature;
  7.     }
  8.     else
  9.     {
  10.         switch (strtoupper($originScale))
  11.         {
  12.         case "CELSIUS":
  13.             switch (strtoupper($targetScale))
  14.             {
  15.                 case "FAHRENHEIT":
  16.                     return CelsiusToFahrenheit($temperature);
  17.                     break;
  18.                 case "KELVIN":
  19.                     return CelsiusToKelvin($temperature);
  20.                     break;
  21.             }
  22.             break;
  23.         case "FAHRENHEIT":
  24.             switch (strtoupper($targetScale))
  25.             {
  26.                 case "CELSIUS":
  27.                     return FahrenheitToCelsius($temperature);
  28.                     break;
  29.                 case "KELVIN":
  30.                     return FahrenheitToKelvin($temperature);
  31.                     break;
  32.             }
  33.             break;
  34.         case "KELVIN":
  35.             switch (strtoupper($targetScale))
  36.             {
  37.                 case "CELSIUS":
  38.                     return KelvinToCelsius($temperature);
  39.                     break;
  40.                 case "FAHRENHEIT":
  41.                     return KelvinToFahrenheit($temperature);
  42.                     break;
  43.             }
  44.             break;
  45.         }
  46.     }
  47. }

Whew! The line wrapping makes it look uglier than it is, but you can copy-paste it into your code editor of choice and it will be formatted correctly there.


That's it for this week and for this topic. If you want to expand on this, you can add conversion functions for other temperature scales: Delisle, Newton, Rankine, Reaumur, and Romer. The formulas and information about each scale are available on Wikipedia.

No comments: