Temperature conversion: Difference between revisions
Content added Content deleted
(Added XPL0) |
m (whitespace/tidy up) |
||
Line 1: | Line 1: | ||
{{task}} |
{{task}} |
||
There are quite a number of temperature scales. For this task we will concentrate on 4 of the perhaps best-known ones: [ |
There are quite a number of temperature scales. For this task we will concentrate on 4 of the perhaps best-known ones: [[wp:Kelvin|Kelvin]], [[wp:Degree Celsius|Celcius]], [[wp:Fahrenheit|Fahrenheit]] and [[wp:Degree Rankine|Rankine]]. |
||
The Celcius and Kelvin scales have the same magnitude, but different null points. |
The Celcius and Kelvin scales have the same magnitude, but different null points. |
||
0 degrees Celcius corresponds to '''273.15''' kelvin. |
:0 degrees Celcius corresponds to '''273.15''' kelvin. |
||
⚫ | |||
⚫ | |||
The Fahrenheit and Rankine scales also have the same magnitude, but different null points. |
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 Fahrenheit corresponds to '''459.67''' degrees Rankine. |
||
⚫ | |||
⚫ | |||
The Celcius/Kelvin and Fahrenheit/Rankine scales have a ratio of '''5 : 9'''. |
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: |
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: | Line 114: | ||
NB. directly. |
NB. directly. |
||
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</lang> |
k2KCFR =: (K2K , K2C , K2F ,: K2R) p./ ]</lang> |
||
{{out|Example}} |
|||
<lang j> NB. Format matrix for printing |
|||
fmt =: '0.2' 8!:0 k2KCFR |
fmt =: '0.2' 8!:0 k2KCFR |
||
Line 154: | Line 148: | ||
C 27.00 |
C 27.00 |
||
F 80.33 |
F 80.33 |
||
R 540.00 |
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). |
'''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: | Line 183: | ||
} |
} |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
<pre> |
<pre> |
||
K 21.00 |
K 21.00 |
||
Line 202: | Line 195: | ||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>П7 0 , 8 * П8 ИП7 9 * 5 |
<lang mk61>П7 0 , 8 * П8 ИП7 9 * 5 |
||
/ 3 2 + П9 ИП7 2 7 3 , |
/ 3 2 + П9 ИП7 2 7 3 , |
||
1 5 + П4 С/П П8 1 , 8 / |
1 5 + П4 С/П П8 1 , 8 / |
||
Line 228: | Line 221: | ||
Р9 = tºF. |
Р9 = tºF. |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
<lang objc>#import <Foundation/Foundation.h> |
<lang objc>#import <Foundation/Foundation.h> |
||
Line 284: | Line 278: | ||
-40℉ |
-40℉ |
||
419.67R</pre> |
419.67R</pre> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<lang php>error_reporting(E_ALL & ~ ( E_NOTICE | E_WARNING )); |
<lang php>error_reporting(E_ALL & ~ ( E_NOTICE | E_WARNING )); |
||
Line 305: | Line 300: | ||
} |
} |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
<pre>Enter a value in kelvin (q to quit): 21 |
<pre>Enter a value in kelvin (q to quit): 21 |
||
K 21.00 |
K 21.00 |
||
Line 420: | Line 416: | ||
serr: say; say '***error!***'; say; say arg(1); say; exit 13</lang> |
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> |
'''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 |
───────────────────────────────────────────────── -40c |
||
-40 Celcius |
-40 Celcius |
||
Line 477: | Line 473: | ||
</pre> |
</pre> |
||
[Actually, water freezes at 0.000089º C, and boils at 99.974º C.] |
[Actually, water freezes at 0.000089º C, and boils at 99.974º C.] |
||
<br><br> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
Line 490: | Line 485: | ||
ChOut(0, ^R); RlOut(0, R); CrLf(0); |
ChOut(0, ^R); RlOut(0, R); CrLf(0); |
||
]</lang> |
]</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |