23
\$\begingroup\$

There was a discussion going on in TNB once about the best temperature scale, and we agreed on something: Take the average of all four main temperature scales! That is, Celsius, Kelvin, Fahrenheit, and Rankine (Sorry Réaumur).

So, now the issue is, most people don't use this system. So, I need a program to convert back from this average!

Challenge

Given the average of the Celsius, Fahrenheit, Kelvin, and Rankine representations of a certain temperature, output the individual standard representations, in any prespecified and consistent order. It turns out that this is possible, based on my whiteboard calculations. Input will be a single floating-point value in whatever range your language can handle, and output will be four floating-point values in any reasonable format. You can restrict the input to force the output to be in the range of your language, but you must be able to support down to Absolute Zero (thus, you need to be able to handle negative numbers).

Test Cases

input -> (Celsius, Fahrenheit, Kelvin, Rankine)
100 -> (-70.86071428571424, -95.54928571428565, 202.28928571428574, 364.12071428571437)
20 -> (-128.0035714285714, -198.4064285714286, 145.14642857142857, 261.2635714285714)
-10 -> (-149.43214285714282, -236.97785714285715, 123.71785714285716, 222.69214285714287)
10000 -> (7000.567857142858, 12633.022142857144, 7273.717857142858, 13092.692142857144)

These values were generated with Uriel's Python program, and I verified that they were correct.

\$\endgroup\$
12
  • 2
    \$\begingroup\$ related \$\endgroup\$
    – Leaky Nun
    Commented Apr 24, 2017 at 14:52
  • \$\begingroup\$ I think the first three test cases are messed up somehow, the Fahrenheit and Kelvin numbers have the same decimal part, and vice versa. \$\endgroup\$ Commented Apr 24, 2017 at 14:58
  • \$\begingroup\$ Also for the last test case I get 13092.... Rankines instead of 13091... \$\endgroup\$ Commented Apr 24, 2017 at 15:08
  • 8
    \$\begingroup\$ -11.99 Kelvin?! \$\endgroup\$ Commented Apr 25, 2017 at 10:57
  • 3
    \$\begingroup\$ There's nothing inherently wrong with negative Kelvin temperatures in the hypothetical. They just are infinitely hotter than regular temperatures. (Note: I'm not a physicist, I just watch Youtube videos all day and pretend to be one on the internet). \$\endgroup\$ Commented Apr 25, 2017 at 19:23

9 Answers 9

10
\$\begingroup\$

JavaScript (ES6), 49 bytes

f=
a=>[a=(a-199.205)/1.4,a+=273.15,a*=1.8,a-=459.67]
<input oninput=f(this.value).map(function(x,i){o.rows[i].cells[1].textContent=x})>
<table id=o>
<tr><th>Celsius:</th><td></td></tr>
<tr><th>Kelvin:</th><td></td></tr>
<tr><th>Rankine:</th><td></td></tr>
<tr><th>Fahrenheit:</th><td></td></tr>
</table>

\$\endgroup\$
2
  • \$\begingroup\$ You spelt Celsius wrong in your snippet :) \$\endgroup\$ Commented Apr 25, 2017 at 1:25
  • 2
    \$\begingroup\$ @numbermaniac Bah, and after all the trouble I went to to double-check the spelling of Fahrenheit too... \$\endgroup\$
    – Neil
    Commented Apr 25, 2017 at 7:41
10
\$\begingroup\$

Python, 63 bytes

def f(a):x=(a+183.205)*5/7;m=x*9/5;return x-273.15,m-459.67,x,m

a is the average, returns a tuple of the results as (celsius, fahrenheit, kelvin, rankine)

Math involved:

kelvin = x
celsius = x - 273.15
fahrenheit = x * 9/5 - 459.67
rankine = x * 9/5

a = (x + x - 273.15 + x * 9/5 - 459.67 + x * 9/5) / 4 = x * 7/5 - 183.205
x = (a + 183.205) * 5/7
\$\endgroup\$
4
\$\begingroup\$

Pyth, 40 37 36 bytes

-BJ+c36641 280c*5Q7 273.15-B*J1.8 459.67
-BJc+916.025*5Q7 273.15-B*J1.8 459.67
-BJc+916.025*5Q7 273.15-B*1.8J459.67

Try it online!

Specs

  • Input: 100
  • Output: [Kelvin, Celcius]\n[Rankine, Fahrenheit]

Math

kelvin = (average*5+916.025)/7
\$\endgroup\$
0
3
\$\begingroup\$

Dyalog APL, 46 40 bytes

6 bytes saved thanks to @Adám

273.15 459.67 0 0-⍨4⍴63 35÷⍨45×183.205+⊢

Try it online!

Anonymous monad, uses the Dyalog Classic character set.

\$\endgroup\$
5
  • \$\begingroup\$ No, it uses the Dyalog Classic character set, since this will only work on Dyalog APL. \$\endgroup\$
    – Adám
    Commented Apr 25, 2017 at 4:28
  • \$\begingroup\$ Save six bytes: 273.15 459.67 0 0-⍨4⍴63 35÷⍨45×183.205+⊢ \$\endgroup\$
    – Adám
    Commented Apr 25, 2017 at 4:37
  • \$\begingroup\$ @Adám thanks! if I may only ask, why would this not work on other APLs? \$\endgroup\$
    – Uriel
    Commented Apr 25, 2017 at 10:28
  • \$\begingroup\$ Because only Dyalog has both {dfns}, , and single-byte encoding. GNU APL has dfns, but uses UTF-8 and has no , while APL2 (which was the codepage you linked to) has neither dfns nor . \$\endgroup\$
    – Adám
    Commented Apr 25, 2017 at 10:33
  • \$\begingroup\$ Now that you are using a tacit train, it is definitely Dyalog APL, as this is unique to Dyalog APL (and J). \$\endgroup\$
    – Adám
    Commented Apr 25, 2017 at 10:37
2
\$\begingroup\$

PHP, 62 Bytes

Order Kelvin , Celsius , Rankine , Fahrenheit

print_r([$t=($argn+183.205)/1.4,$t-273.15,$t*=1.8,$t-459.67]);

Online Version

PHP, 64 Bytes

Order Kelvin , Rankine , Fahrenheit , Celsius

print_r([$k=($argn+183.205)/1.4,$r=1.8*$k,$r-459.67,$k-273.15]);

Online Version

\$\endgroup\$
2
\$\begingroup\$

dc, 37 38 bytes

[Edit 1: Added third form, per Neil's comment]

These [first two] are both the same length :( The first one produces Fahrenheit, Celsius, Kelvin, Rankine (top to bottom on the stack), and the second one produces Fahrenheit, Rankine, Celsius, Kelvin.

9k?183.205+1.4/d1.8*rd273.15-d1.8*32+f
9k?183.205+1.4/d273.15-rd1.8*d459.67-f
9k?199.205-1.4/d273.15+d1.8*d459.67-f

Example outputs (dc uses _ to signal negative numbers on input): [from first two forms; see edit below for third form.]

20
-198.406428572
-128.003571429
145.146428571
261.263571427

20
-198.406428573
261.263571427
145.146428571
-128.003571429

_10
-236.977857144
-149.432142858
123.717857142
222.692142855

_10
-236.977857145
222.692142855
123.717857142
-149.432142858

How it works

9k?183.205+1.4/d1.8*rd273.15-d1.8*32+f

9k sets 9-place arithmetic.
? reads input from stdin, leaving it at top of stack (TOS).
183.205+ adds 183.205 to TOS
1.4/ divides TOS by 1.4 or 7/5, giving degrees Kelvin.
d duplicates TOS. (Ie, duplicates degrees Kelvin)
1.8*r computes Rankine from Kelvin, then reverses top two of stack.
d273.15- duplicates TOS and subtracts 273.15 to get degrees Celsius.
d1.8*32+ duplicates TOS, multiplies by 9/5, and adds 32, for Fahrenheit.
f prints contents of stack.


Edit 1, continued:

9k?199.205-1.4/d273.15+d1.8*d459.67-f

This form, suggested by Neil, begins by computing Celsius instead of Kelvin. This saves a rotate (an r) when computing Rankin from Kelvin.

It computes Celsius = (Average - 199.205)*5/7 via 199.205-1.4/, adds 273.15 to get Kelvin, multiplies by 1.8 to get Rankin, and subtracts 459.67 to get Fahrenheit. For example:

20
-198.406428571
261.263571429
145.146428572
-128.003571428
\$\endgroup\$
2
  • \$\begingroup\$ 9k?199.205-1.4/d273.15+d1.8*d459.67-f saves 1 byte by changing the output order. \$\endgroup\$
    – Neil
    Commented Apr 25, 2017 at 10:53
  • \$\begingroup\$ @Neil, thanks! Edited into answer. \$\endgroup\$ Commented Apr 25, 2017 at 18:09
1
\$\begingroup\$

CJam, 38 bytes

rd5*916.025+7/_p_273.15-p1.8*_p459.67-

Kelvins = (5*input+916.025)/7

Outputs as

Kelvin
Celsius
Rankine
Fahrenheit

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Whispers v2, 168 bytes

> Input
> 5
> 7
> 183.205
>> 2÷3
>> 1+4
>> 6⋅5
>> 7-11
>> 7⋅12
>> 9-13
> 273.15
> 1.8
> 459.67
>> Output 7
>> Output 8
>> Output 9
>> Output 10
>> Then 14 15 16 17

Try it online!

Outputs as Kelvin\nCelsius\nRankine\nFahrenheit

\$\endgroup\$
0
\$\begingroup\$

Python 3, 67 bytes

c=(5*float(input())-996.025)/7;t=[c,c*1.8+32,c+273.15,c*1.8+491.67]

This code does some algebra to get the temperature in Celcius, then I convert it to the other temperature units. The temperatures are stored in the list t.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.