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.

Monday, February 18, 2008

Trigonometry, Part 3

I was hoping to publish every Saturday, but this past Saturday I got sidetracked. So without further ado, I bring you the next installment in the trigonometry series of articles.


Each of the six basic trigonometric functions has an inverse function. The inverse functions for sine, cosine, and tangent (arcsine, arcosine, and arctangent, respectively) are already defined natively in PHP. In ASP, only the inverse function for tangent (arctangent) is defined, but we will create an adapter for it to be consistent with the abbreviation in PHP. We also need to define some constants that are already present in PHP but not in ASP.


ASP

  1. Const M_PI = 3.14159265358979323846
  2. Const M_PI_2 = M_PI / 2
  3. function asin(x)
  4.     asin = atn(x / sqr(1 - x ^ 2))
  5. end function
  6. function acos(x)
  7.     acos = M_PI_2 - asin(x)
  8. end function
  9. function atan(x)
  10.     atan = atn(x)
  11. end function

As you can probably guess, the inverse functions for cosecant, secant, and cotangent are arccosecant, arcsecant, and arccotangent.


ASP

  1. function acsc(x)
  2.     acsc = asin(1 / x)
  3. end function
  4. function asec(x)
  5.     asec = M_PI_2 - acsc(x)
  6. end function
  7. function acot(x)
  8.     acot = M_PI_2 - atn(x)
  9. end function

PHP

  1. function acsc($x)
  2. {
  3.     return asin(1 / $x);
  4. }
  5. function asec($x)
  6. {
  7.     return (M_PI_2 - acsc($x));
  8. }
  9. function acot($x)
  10. {
  11.     return (M_PI_2 - atan($x));
  12. }

Next time we'll do something different for a change. I promise.

Saturday, February 9, 2008

Trigonometry, Part 2

As promised, this week we're building some special trigonometry functions using the functions we wrote last week: versed sine (or versine), coversed sine (or coversine), haversed sine (or haversine), hacoversed sine (or hacoversine), exsecant, and excosecant. Actually, to be honest, only two of those build on the functions from last week.


ASP

  1. function versin(x)
  2.     versin = 1 - cos(x)
  3. end function
  4. function coversin(x)
  5.     coversin = 1 - sin(x)
  6. end function
  7. function haversin(x)
  8.     haversin = versin(x) / 2
  9. end function
  10. function hacoversin(x)
  11.     hacoversin = coversin(x) / 2
  12. end function
  13. function exsec(x)
  14.     exsec = sec(x) - 1
  15. end function
  16. function excsc(x)
  17.     excsc = csc(x) - 1
  18. end function

PHP

  1. function versin($x)
  2. {
  3.     return (1 - cos($x));
  4. }
  5. function coversin($x)
  6. {
  7.     return (1 - sin($x));
  8. }
  9. function haversin($x)
  10. {
  11.     return (versin($x) / 2);
  12. }
  13. function hacoversin($x)
  14. {
  15.     return (coversin($x) / 2);
  16. }
  17. function exsec($x)
  18. {
  19.     return (sec($x) - 1);
  20. }
  21. function excsc($x)
  22. {
  23.     return (csc($x) - 1);
  24. }

Next week, the fun continues with inverse trigonometric functions.

Saturday, February 2, 2008

Trigonometry, Part 1

Both ASP and PHP contain three basic trigonometry functions: sin(), cos(), and tan(). You may remember from school that there are three more basic trigonometry functions: cosecant, secant, and cotangent. It might be handy to have some functions for these too.


ASP

  1. function csc(x)
  2.     csc = 1 / sin(x)
  3. end function
  4. function sec(x)
  5.     sec = 1 / cos(x)
  6. end function
  7. function cot(x)
  8.     cot = 1 / tan(x)
  9. end function

PHP

  1. function csc($x)
  2. {
  3.     return (1 / sin($x));
  4. }
  5. function sec($x)
  6. {
  7.     return (1 / cos($x));
  8. }
  9. function cot($x)
  10. {
  11.     return (1 / tan($x));
  12. }

Next week we'll build off some of these basic functions to make some special functions.