Temperature conversion: Difference between revisions

m (→‎{{header|Python}}: `stop early)
Line 127:
 
'''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).
 
=={{header|Java}}==
<lang java>import java.util.*;
 
public class TemparatureConversion {
public static void main(String args[]) {
if (args.length == 1) {
try {
double kelvin = Double.parseDouble(args[0]);
if (kelvin >= 0) {
System.out.printf("K %s\n", kelvin);
System.out.printf("C %s\n", kelvinToCelcius(kelvin));
System.out.printf("F %s\n", kelvinToFahrenheit(kelvin));
System.out.printf("R %s\n", kelvinToRankine(kelvin));
} else {
System.out.printf("%s K is below absolute zero");
}
} catch (NumberFormatException e) {
System.out.println(e);
}
}
}
public static double kelvinToCelcius(double k) {
return k + 273.15;
}
public static double kelvinToFahrenheit(double k) {
return k * 1.8 - 459.67;
}
public static double kelvinToRankine(double k) {
return k * 1.8;
}
}</lang>
 
=={{header|Python}}==
Anonymous user