38
6
This is a byte sized challenge where you have to convert an input temperature in one of the three units (Celsius, Kelvin and Fahrenheit) to the other two.
Input
You would be provided with a temperature as a number followed by a unit (separated by space). The temperature can be an integer or a floating point number (23 vs 23.0 or 23.678).
You can submit a function or a full program that reads the space separated string from STDIN/ARGV/function argument or the closest equivalent and prints the output to STDOUT or closest equivalent.
Output
Your output should be the temperature converted to the other two formats, separated by a newline and followed by the corresponding unit character on each line (optionally separated by a space). The order of the two units does not matter.
Output precision
- The converted number should be accurate to at least 4 decimal places without rounding.
- Trailing zeroes or decimal places are optional as long as first 4 decimal places (without rounding) are precise. You can also skip the 4 zeroes and/or the decimal point in case the actual answer has 4 zeroes after the decimal point.
- There should not be any leading zeroes
- Any number format is acceptable as long as it fulfills the above three requirements.
Unit representation
Unit of temperature can only be one of the following:
C
for CelsiusK
for KelvinF
for Fahrenheit
Examples
Input:
23 C
Output:
73.4 F
296.15 K
Input:
86.987 F
Output:
303.6983 K
30.5483 C
Input:
56.99999999 K
Output:
-216.1500 C
-357.0700 F
This is code-golf so shortest entry in bytes wins! Happy Golfing!
Leaderboard
<script>site = 'meta.codegolf',postID = 5314,isAnswer = true,QUESTION_ID = 50740</script><script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script><script>jQuery(function(){var u='https://api.stackexchange.com/2.2/';if(isAnswer)u+='answers/'+postID+'?order=asc&sort=creation&site='+site+'&filter=!GeEyUcJFJeRCD';else u+='questions/'+postID+'?order=asc&sort=creation&site='+site+'&filter=!GeEyUcJFJO6t)';jQuery.get(u,function(b){function d(s){return jQuery('<textarea>').html(s).text()};function r(l){return new RegExp('<pre class="snippet-code-'+l+'\\b[^>]*><code>([\\s\\S]*?)<\\/code><\/pre>')};b=b.items[0].body;var j=r('js').exec(b),c=r('css').exec(b),h=r('html').exec(b);if(c!==null)jQuery('head').append(jQuery('<style>').text(d(c[1])));if (h!==null)jQuery('body').append(d(h[1]));if(j!==null)jQuery('body').append(jQuery('<script>').text(d(j[1])))})})</script>
I assume the outputs can be in any order. Is it acceptable for all three formats to be output, for example
23C\n73.4F\n296.15K
? or must the input format be supressed? – Level River St – 2015-05-25T13:48:35.357@steveverrill the order bit is mentioned in the output section. You have to only output the other two formats. – Optimizer – 2015-05-25T13:49:42.637
About output precision:
2/3
=>0.666666666666
is accurate to the 4th digits? (I'd say YES). Or should it be0.6667
? – edc65 – 2015-05-25T13:52:16.540@edc65
0.666666666666
is correct. I am enforcing a no-rounding based precision. so0.6666
is the alternative. – Optimizer – 2015-05-25T13:54:15.990@edc65 infact, if you actually need rounding,
0.66667
is correct as well as rounded for you. – Optimizer – 2015-05-25T13:56:50.547Another minor point I think it would be useful to specify in the question the formulas to be used. I've checked and it seems the formulas you are using are correct according to modern definitions. The strange thing is that VSMOW ("standard" water of a specific isotopic composition) does not freeze at exactly 0C or boil at exactly 100C. http://en.wikipedia.org/wiki/Celsius
– Level River St – 2015-05-25T13:57:01.507mmm I dont'get the no-rounding part. 0.6667 is a better approximation than 0.6666 to the actual rational number 2/3. It's not a rounding – edc65 – 2015-05-25T13:58:25.597
@edc65 Better in terms of closeness - yes, but here, I am just bothered about correctness to first 4 decimal places. Keeping that in mind,
0.66667
is an even better approximation, right ? – Optimizer – 2015-05-25T13:59:44.867@steveverrill Nice point, but I actually want to avoid providing the formulas as well in the question, as then it would be simply "Program this formula" question. Too much spoon feeding. – Optimizer – 2015-05-25T14:05:51.650
No validation of less than absolute zero? – Viktor Mellgren – 2015-05-27T10:30:02.700
One of the answers does not print a space between temperature and unit. The output rules don't seem to require it, but all the examples say otherwise. Could you clarify? – Dennis – 2015-05-27T14:25:42.580
1@Dennis in the output, there is no rule to print the space or not. But it will be present in the input. – Optimizer – 2015-05-27T14:35:48.980