Temperature conversion
There are quite a number of temperature scales. For this task we will concentrate on 4 of the perhaps best-known ones: Kelvin, Celcius, Fahrenheit and Rankine.
The Celcius and Kelvin scales have the same magnitude, but different null points.
0 degrees Celcius corresponds to 273.15 kelvin.
0 kelvin is absolute zero.
The Fahrenheit and Rankine scales also have the same magnitude, but different null points.
0 degrees Fahrenheit corresponds to 459.67 degrees Rankine.
0 degrees Rankine is absolute zero.
The Celcius/Kelvin and Fahrenheit/Rankine scales have a ratio of 5 : 9.
Write code that accepts a value of kelvin, converts it to values on the three other scales and prints the result. For instance:
K 21.00 C -252.15 F -421.87 R 37.80
D
<lang d>import std.stdio, std.conv, std.string;
void main(string[] args) {
if (args.length == 2 && isNumeric(args[1])) { immutable kelvin = to!double(args[1]); if (kelvin >= 0) { writefln("K %2.2f", kelvin); writefln("C %2.2f", kelvinToCelsius(kelvin)); writefln("F %2.2f", kelvinToFahrenheit(kelvin)); writefln("R %2.2f", kelvinToRankine(kelvin)); } else writefln("%2.2f K is below absolute zero", kelvin); }
}
double kelvinToCelsius(in double k) pure nothrow {
return k - 273.15;
}
double kelvinToFahrenheit(in double k) pure nothrow {
return k * 1.8 - 459.67;
}
double kelvinToRankine(in double k) pure nothrow {
return k * 1.8;
}
unittest {
import std.math; assert(approxEqual(kelvinToCelsius(21.0), -252.15)); assert(approxEqual(kelvinToFahrenheit(21.0), -421.87)); assert(approxEqual(kelvinToRankine(21.0), 37.8));
}</lang>
- Output:
K 21.00 C -252.15 F -421.87 R 37.80
J
Solution:<lang j> NB. Temp conversions are all linear polynomials
K2K =: 0 1 NB. K = (1 *k) + 0 K2C =: _273 1 NB. C = (1 *k) - 273 K2F =: _459.67 1.8 NB. F = (1.8*k) - 459.67 K2R =: 0 1.8 NB. R = (1.8*k) + 0
NB. Do all conversions at once (eval NB. polynomials in parallel). This is the NB. numeric matrix J programs would manipulate NB. directly. k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</lang>
Example:<lang j> NB. Format matrix for printing
fmt =: '0.2' 8!:0 k2KCFR
NB. Tag each temp with scale, for human NB. legibility. kcfr =: 0 _1 |: 'KCFR' ,"0 1"_1 >@:fmt kcfr 21
K 21.00 C-252.00 F-421.87 R 37.80
kcfr 0 NB. Absolute zero
K 0.00 C-273.00 F-459.67 R 0.00
kcfr 21 100 300 NB. List of temps works fine
K 21.00 C-252.00 F-421.87 R 37.80
K 100.00 C-173.00 F-279.67 R 180.00
K 300.00 C 27.00 F 80.33 R 540.00
</lang>
Notes: The approach is founded on polynomials, one for each conversion (e.g. Fahrenheit = 1.8*x - 459.67 where x is measured in degrees Kelvin), and all polynomials are evaluated simultaneously using the built-in p.. Through some code decorations (specifically the / in p./ the "0 1"_1 and the 0 _1 |:), the function is will convert arrays of temperatures of arbitrarily high dimension (a single temp, lists of temps, tables of temps, cubes of temps, etc).
Python
<lang python>>>> while True: k = float(input('K ? ')) print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees." % (k, k - 273.15, k * 1.8 - 459.67, k * 1.8))
K ? 21.0
21 Kelvin = -252.15 Celsius = -421.87 Fahrenheit = 37.8 Rankine degrees.
K ? 222.2
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
K ?
Traceback (most recent call last):
File "<pyshell#4>", line 2, in <module> k = float(input('K ? '))
KeyboardInterrupt >>> </lang>
Python: Universal conversion
This converts from any one of the units to all the others <lang python>>>> toK = {'C': (lambda c: c + 273.15),
'F': (lambda f: (f + 459.67) / 1.8), 'R': (lambda r: r / 1.8), 'K': (lambda k: k) }
>>> while True: magnitude, unit = input('<value> <K/R/F/C> ? ').split() k = toK[unit](float(magnitude)) print("%g Kelvin = %g Celsius = %g Fahrenheit = %g Rankine degrees." % (k, k - 273.15, k * 1.8 - 459.67, k * 1.8))
<value> <K/R/F/C> ? 222.2 K
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
<value> <K/R/F/C> ? -50.95 C
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
<value> <K/R/F/C> ? -59.71 F
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
<value> <K/R/F/C> ? 399.96 R
222.2 Kelvin = -50.95 Celsius = -59.71 Fahrenheit = 399.96 Rankine degrees.
<value> <K/R/F/C> ? </lang>