Jump to content

Temperature conversion: Difference between revisions

m
whitespace/tidy up
(Added XPL0)
m (whitespace/tidy up)
Line 1:
{{task}}
 
There are quite a number of temperature scales. For this task we will concentrate on 4 of the perhaps best-known ones: [http[wp://en.wikipedia.org/wiki/Kelvin |Kelvin]], [http[wp://en.wikipedia.org/wiki/Degree_CelsiusDegree Celsius|Celcius]], [http[wp://en.wikipedia.org/wiki/Fahrenheit |Fahrenheit]] and [http[wp://en.wikipedia.org/wiki/Degree_RankineDegree Rankine|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.
 
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.
 
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:
Line 120 ⟶ 114:
NB. directly.
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</lang>
{{out|Example}}
 
'''Example''':<lang j> NB. Format matrix for printing
fmt =: '0.2' 8!:0 k2KCFR
 
Line 154 ⟶ 148:
C 27.00
F 80.33
R 540.00</lang>
</lang>
 
'''Notes''': The approach is founded on polynomials, one for each conversion (e.g. <tt>Fahrenheit = 1.8*x - 459.67</tt> where <tt>x</tt> is measured in degrees Kelvin), and all polynomials are evaluated simultaneously using the built-in <tt>p.</tt>. Through some code decorations (specifically the <tt>/</tt> in <tt>p./</tt> the <tt>"0 1"_1</tt> and the <tt>0 _1 |:</tt>), we permit our function to convert arrays of temperatures of arbitrarily high dimension (a single temp, lists of temps, tables of temps, cubes of temps, etc).
 
Line 191 ⟶ 183:
}
}</lang>
{{out}}
<pre>
K 21.00
Line 202 ⟶ 195:
 
=={{header|МК-61/52}}==
<lang mk61>П7 0 , 8 * П8 ИП7 9 * 5
/ 3 2 + П9 ИП7 2 7 3 ,
1 5 + П4 С/П П8 1 , 8 /
Line 228 ⟶ 221:
 
Р9 = tºF.
 
=={{header|Objective-C}}==
<lang objc>#import <Foundation/Foundation.h>
Line 284 ⟶ 278:
-40℉
419.67R</pre>
 
=={{header|PHP}}==
<lang php>error_reporting(E_ALL & ~ ( E_NOTICE | E_WARNING ));
Line 305 ⟶ 300:
}
}</lang>
{{out}}
<pre>Enter a value in kelvin (q to quit): 21
K 21.00
Line 420 ⟶ 416:
serr: say; say '***error!***'; say; say arg(1); say; exit 13</lang>
'''output''' when using the input of: <tt> -40C, 0 c (water freezes), 37C (body temp), 100 C (water boils), 21 degrees Kelvin, 0K (outer space?) </tt>
<pre>
<pre style="overflow:scroll">
───────────────────────────────────────────────── -40c
-40 Celcius
Line 477 ⟶ 473:
</pre>
[Actually, water freezes at 0.000089º C, &nbsp; and boils at 99.974º C.]
<br><br>
 
=={{header|XPL0}}==
Line 490 ⟶ 485:
ChOut(0, ^R); RlOut(0, R); CrLf(0);
]</lang>
 
{{out}}
<pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.