Skip to content

Commit c876e50

Browse files
Panquesito7github-actions[bot]vil02
authored
feat: add Kelvin to Celsius conversion algorithm (#2475)
* feat: add Kelvin to Celsius conversion algorithm * updating DIRECTORY.md * chore: apply suggestions from code review Co-authored-by: Piotr Idzik <[email protected]> * chore: apply suggestions from code review Co-authored-by: Piotr Idzik <[email protected]> --------- Co-authored-by: github-actions[bot] <[email protected]> Co-authored-by: Piotr Idzik <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent ea4100e commit c876e50

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
* [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/happy_number.cpp)
290290
* [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/iterative_tree_traversals.cpp)
291291
* [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kadanes3.cpp)
292+
* [Kelvin To Celsius](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kelvin_to_celsius.cpp)
292293
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache.cpp)
293294
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/matrix_exponentiation.cpp)
294295
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/palindrome_of_number.cpp)

others/kelvin_to_celsius.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)