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
function CelsiusToFahrenheit(temperature)
CelsiusToFahrenheit = (temperature * 9 / 5) + 32
end function
function CelsiusToKelvin(temperature)
CelsiusToKelvin = temperature + 273.15
end function
function FahrenheitToCelsius(temperature)
FahrenheitToCelsius = (temperature - 32) * 5 / 9
end function
function FahrenheitToKelvin(temperature)
FahrenheitToKelvin = (temperature - 32) * 5 / 9 + 273.15
end function
function KelvinToCelsius(temperature)
KelvinToCelsius = temperature - 273.15
end function
function KelvinToFahrenheit(temperature)
KelvinToFahrenheit = (temperature - 273.15) * 9 / 5 + 32
end function
PHP
function CelsiusToFahrenheit($temperature)
{
return ($temperature * 9 / 5) + 32;
}
function CelsiusToKelvin($temperature)
{
return $temperature + 273.15;
}
function FahrenheitToCelsius($temperature)
{
return ($temperature - 32) * 5 / 9;
}
function FahrenheitToKelvin($temperature)
{
return ($temperature - 32) * 5 / 9 + 273.15;
}
function KelvinToCelsius($temperature)
{
return $temperature - 273.15;
}
function KelvinToFahrenheit($temperature)
{
return ($temperature - 273.15) * 9 / 5 + 32;
}
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
function convertTemperature(temperature, originScale, targetScale)
' Return the input value for temperature if both scales are identical.
if UCase(originScale) = UCase(targetScale) then
convertTemperature = temperature
exit function
end if
Select Case UCase(originScale)
Case "CELSIUS"
Select Case UCase(targetScale)
Case "FAHRENHEIT"
convertTemperature = CelsiusToFahrenheit(temperature)
Case "KELVIN"
convertTemperature = CelsiusToKelvin(temperature)
End Select
Case "FAHRENHEIT"
Select Case UCase(targetScale)
Case "CELSIUS"
convertTemperature = FahrenheitToCelsius(temperature)
Case "KELVIN"
convertTemperature = FahrenheitToKelvin(temperature)
End Select
Case "KELVIN"
Select Case UCase(targetScale)
Case "CELSIUS"
convertTemperature = KelvinToCelsius(temperature)
Case "FAHRENHEIT"
convertTemperature = KelvinToFahrenheit(temperature)
End Select
End Select
end function
PHP
function convertTemperature($temperature, $originScale, $targetScale)
{
// Return the input value for temperature if both scales are identical.
if (strtoupper($originScale) == strtoupper($targetScale))
{
return $temperature;
}
else
{
switch (strtoupper($originScale))
{
case "CELSIUS":
switch (strtoupper($targetScale))
{
case "FAHRENHEIT":
return CelsiusToFahrenheit($temperature);
break;
case "KELVIN":
return CelsiusToKelvin($temperature);
break;
}
break;
case "FAHRENHEIT":
switch (strtoupper($targetScale))
{
case "CELSIUS":
return FahrenheitToCelsius($temperature);
break;
case "KELVIN":
return FahrenheitToKelvin($temperature);
break;
}
break;
case "KELVIN":
switch (strtoupper($targetScale))
{
case "CELSIUS":
return KelvinToCelsius($temperature);
break;
case "FAHRENHEIT":
return KelvinToFahrenheit($temperature);
break;
}
break;
}
}
}
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.