Showing posts with label Temperature. Show all posts
Showing posts with label Temperature. Show all posts

Saturday, May 26, 2018

Temperature Conversion Revisited

I recently completed another ASP.NET project and learned about the app_code folder, which lets you share code between pages. It's slightly similar to the #include directive in Classic ASP, but a lot more structured. I couldn't find much good documentation on it, so most of what I've learned has been through experimentation.
Any files placed into the app_code folder are automatically available from any page in your web application, so you can write: myNamespace.myClass.myFunction(someVariable) and the magic will happen. Alternatively, you can add Imports myNamespace.myClass in the top of your page and call myFunction without all the prefixing.
Though I'm still not a big fan of ASP.NET, this app_code thing is pretty slick and it's inspired me to write more reusable code. I had previously written a collection of functions for temperature conversion, which I've since grown unhappy with after reading Code Complete 2.0 by Steve McConnell because I named my functions like this: CelsiusToFahrenheit, instead of like this: FahrenheitFromCelsius.
I had been planning to write more conversion functions for various units of measurement, but was never satisfied enough with how it was turning out to make it publically available. Besides learning to appreciate ASP.NET, I'm also learning to appreciate object-oriented programming. This whole namespace/class combination with app_code seems to be appropriately suited for doing unit of measurement conversion.
So here's my plan. I've defined a namespace called UOM, which is the manufacturing industry standard abbreviation for unit of measure. Each type of measurement will be a class: temperature, distance, volume, mass, etc. Each class will accept string input so that you can specify both the numeric quantity and the unit of measure simultaneously. The class will use the specified unit of measure to convert values to a common unit of measure, and be able to return values in any equivalent unit of measure.

VB.NET

  1. Dim inside as Temperature
  2. inside = New Temperature("10 C")
  3. Response.Write(inside.getFahrenheit) ' Displays "50"
And so I proudly announce the release of the first installment in the UOM namespace: UOM.Temperature, which handles Celsius, Delisle, Fahrenheit, Kelvin, Newton, Rankine, Réaumur, and Rømer temperature scales.
Download the VB.NET source code for UOM.Temperature from Snipplr.com

Tuesday, May 10, 2011

Temperature Conversion Revisited

I recently completed another ASP.NET project and learned about the app_code folder, which lets you share code between pages. It's slightly similar to the #include directive in Classic ASP, but a lot more structured. I couldn't find much good documentation on it, so most of what I've learned has been through experimentation.

Any files placed into the app_code folder are automatically available from any page in your web application, so you can write: myNamespace.myClass.myFunction(someVariable) and the magic will happen. Alternatively, you can add Imports myNamespace.myClass in the top of your page and call myFunction without all the prefixing.

Though I'm still not a big fan of ASP.NET, this app_code thing is pretty slick and it's inspired me to write more reusable code. I had previously written a collection of functions for temperature conversion, which I've since grown unhappy with after reading Code Complete 2.0 by Steve McConnell because I named my functions like this: CelsiusToFahrenheit, instead of like this: FahrenheitFromCelsius.

I had been planning to write more conversion functions for various units of measurement, but was never satisfied enough with how it was turning out to make it publically available. Besides learning to appreciate ASP.NET, I'm also learning to appreciate object-oriented programming. This whole namespace/class combination with app_code seems to be appropriately suited for doing unit of measurement conversion.

So here's my plan. I've defined a namespace called UOM, which is the manufacturing industry standard abbreviation for unit of measure. Each type of measurement will be a class: temperature, distance, volume, mass, etc. Each class will accept string input so that you can specify both the numeric quantity and the unit of measure simultaneously. The class will use the specified unit of measure to convert values to a common unit of measure, and be able to return values in any equivalent unit of measure.

VB.NET

  1. Imports UOM
  2. Dim outside as Temperature
  3. outside = New Temperature("10 C")
  4. Response.Write(outside.getFahrenheit) 'Displays "50"

And so I proudly announce the release of the first installment in the UOM namespace: UOM.Temperature, which handles Celsius, Delisle, Fahrenheit, Kelvin, Newton, Rankine, Réaumur, and Rømer temperature scales.

Download the VB.NET source code for UOM.Temperature from Snipplr.com

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.