Un-average temperatures!

21

3

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.

HyperNeutrino

Posted 2017-04-24T14:50:33.870

Reputation: 26 575

2related – Leaky Nun – 2017-04-24T14:52:01.510

I think the first three test cases are messed up somehow, the Fahrenheit and Kelvin numbers have the same decimal part, and vice versa. – ETHproductions – 2017-04-24T14:58:42.763

Also for the last test case I get 13092.... Rankines instead of 13091... – Business Cat – 2017-04-24T15:08:25.527

I'm getting 364.1207142857143 for the Rankine of the first one – Leaky Nun – 2017-04-24T15:08:27.073

And how accurate must the output be? – Leaky Nun – 2017-04-24T15:09:11.303

Required precision? That directly translates into the number of decimals in the conversion factors, so it's important to know howm many decimals those factors need to have – Luis Mendo – 2017-04-24T16:07:54.130

@LeakyNun The output should be as accurate as possible; that is, to the best extent of your program's floating-point precision. However, I will require it to be within 1/100 of a unit (whatever the unit applicable is) of precision. – HyperNeutrino – 2017-04-24T18:58:48.997

7-11.99 Kelvin?! – Jonathan Allan – 2017-04-25T10:57:20.273

@JonathanAllan :D That's why programs aren't required to hande it :P – HyperNeutrino – 2017-04-25T12:07:43.893

So why is there a test case for -200 as an input then? – Jonathan Allan – 2017-04-25T12:12:52.767

@JonathanAllan If your program happens to be able to calculate it correctly, you can test if you'd like, but it says that it's not necessary. – HyperNeutrino – 2017-04-25T12:13:25.950

2

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).

– Draco18s no longer trusts SE – 2017-04-25T19:23:27.113

Answers

4

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

Leaky Nun

Posted 2017-04-24T14:50:33.870

Reputation: 45 011

10

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

Uriel

Posted 2017-04-24T14:50:33.870

Reputation: 11 708

10

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>

Neil

Posted 2017-04-24T14:50:33.870

Reputation: 95 035

You spelt Celsius wrong in your snippet :) – numbermaniac – 2017-04-25T01:25:06.097

2@numbermaniac Bah, and after all the trouble I went to to double-check the spelling of Fahrenheit too... – Neil – 2017-04-25T07:41:55.030

3

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.

Uriel

Posted 2017-04-24T14:50:33.870

Reputation: 11 708

No, it uses the Dyalog Classic character set, since this will only work on Dyalog APL.

– Adám – 2017-04-25T04:28:11.503

Save six bytes: 273.15 459.67 0 0-⍨4⍴63 35÷⍨45×183.205+⊢

– Adám – 2017-04-25T04:37:22.253

@Adám thanks! if I may only ask, why would this not work on other APLs? – Uriel – 2017-04-25T10:28:22.913

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 . – Adám – 2017-04-25T10:33:54.670

Now that you are using a tacit train, it is definitely Dyalog APL, as this is unique to Dyalog APL (and J). – Adám – 2017-04-25T10:37:50.297

2

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

Jörg Hülsermann

Posted 2017-04-24T14:50:33.870

Reputation: 13 026

2

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

James Waldby - jwpat7

Posted 2017-04-24T14:50:33.870

Reputation: 131

9k?199.205-1.4/d273.15+d1.8*d459.67-f saves 1 byte by changing the output order. – Neil – 2017-04-25T10:53:29.157

@Neil, thanks! Edited into answer. – James Waldby - jwpat7 – 2017-04-25T18:09:49.843

1

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!

Business Cat

Posted 2017-04-24T14:50:33.870

Reputation: 8 927

0

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.

Dat

Posted 2017-04-24T14:50:33.870

Reputation: 879

0

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

user79856

Posted 2017-04-24T14:50:33.870

Reputation: