|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Conversion from [Kelvin to |
| 4 | + * Celsius](https://byjus.com/chemistry/kelvin-to-celsius/) degrees. |
| 5 | + * @details |
| 6 | + * The algorithm consists on converting a Kelvin degree value to a Celsius |
| 7 | + * value. |
| 8 | + * The formula to convert a Kelvin to a Celsius value is: |
| 9 | + * @f[ C = K - 273.15 @f] where: |
| 10 | + * - C is the Celsius temperature |
| 11 | + * - K is the Kelvin temperature |
| 12 | + * |
| 13 | + * Check out [Kelvin](https://en.wikipedia.org/wiki/Kelvin) and |
| 14 | + * [Celsius](https://en.wikipedia.org/wiki/Celsius) on Wikipedia for more |
| 15 | + * information about their story, how do they work, when and why they should be |
| 16 | + * used, etc.. |
| 17 | + * @author [David Leal](https://github.com/Panquesito7) |
| 18 | + */ |
| 19 | + |
| 20 | +#include <cassert> /// for assert |
| 21 | +#include <cmath> /// for std::abs |
| 22 | +#include <iostream> /// for IO operations |
| 23 | + |
| 24 | +/** |
| 25 | + * @namespace |
| 26 | + * @brief Other algorithms |
| 27 | + */ |
| 28 | +namespace others { |
| 29 | +/** |
| 30 | + * @brief Compare two floating point numbers with a certain tolerance. |
| 31 | + * This is needed as with some values, the result (e.g.: -196.15) might be a bit |
| 32 | + * lower (in this case, -196.499999...). |
| 33 | + * @param a the first number to compare |
| 34 | + * @param b the second number to compare |
| 35 | + * @param tolerance the tolerance to use when comparing the numbers |
| 36 | + * @returns true if the numbers ARE equal within the given tolerance |
| 37 | + * @returns false if the numbers are NOT equal within the given tolerance |
| 38 | + * otherwise |
| 39 | + */ |
| 40 | +bool are_almost_equal(double a, double b, double absolute_tolerance = 0.0001) { |
| 41 | + return std::abs(a - b) < absolute_tolerance; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Conversion from Kelvin to Celsius algorithm. |
| 46 | + * @param number the Celsius number that will be used to convert |
| 47 | + * @returns the Kelvin number converted to Celsius |
| 48 | + */ |
| 49 | +double kelvin_to_celsius(double temperature_in_k) { |
| 50 | + const double absolute_zero_in_c = -273.15; |
| 51 | + if (temperature_in_k < absolute_zero_in_c) { |
| 52 | + throw std::invalid_argument("input temperature below absolute zero"); |
| 53 | + } |
| 54 | + return temperature_in_k + absolute_zero_in_c; |
| 55 | +} |
| 56 | +} // namespace others |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Self-test implementations |
| 60 | + * @returns void |
| 61 | + */ |
| 62 | +static void tests() { |
| 63 | + assert(others::are_almost_equal(others::kelvin_to_celsius(230), -43.15)); |
| 64 | + assert(others::are_almost_equal(others::kelvin_to_celsius(512), 238.85)); |
| 65 | + assert(others::are_almost_equal(others::kelvin_to_celsius(55), -218.15)); |
| 66 | + assert(others::are_almost_equal(others::kelvin_to_celsius(77), -196.15)); |
| 67 | + assert(others::are_almost_equal(others::kelvin_to_celsius(9.78), -263.37)); |
| 68 | + assert(others::are_almost_equal(others::kelvin_to_celsius(15), -258.15)); |
| 69 | + assert(others::are_almost_equal(others::kelvin_to_celsius(273.15), 0)); |
| 70 | + |
| 71 | + std::cout << "All tests have successfully passed!\n"; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @brief Main function |
| 76 | + * @returns 0 on exit |
| 77 | + */ |
| 78 | +int main() { |
| 79 | + tests(); // run self-test implementations |
| 80 | + return 0; |
| 81 | +} |
0 commit comments