-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Kelvin to Celsius conversion algorithm #2475
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df1abae
feat: add Kelvin to Celsius conversion algorithm
Panquesito7 7643c17
updating DIRECTORY.md
45ba274
chore: apply suggestions from code review
Panquesito7 bb96512
Merge branch 'master' into kelvin_to_celsius
Panquesito7 91b9f6c
chore: apply suggestions from code review
Panquesito7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @file | ||
* @brief Conversion from [Kelvin to | ||
* Celsius](https://byjus.com/chemistry/kelvin-to-celsius/) degrees. | ||
* @details | ||
* The algorithm consists on converting a Kelvin degree value to a Celsius | ||
* value. | ||
* The formula to convert a Kelvin to a Celsius value is: | ||
* @f[ C = K - 273.15 @f] where: | ||
* - C is the Celsius temperature | ||
* - K is the Kelvin temperature | ||
* | ||
* Check out [Kelvin](https://en.wikipedia.org/wiki/Kelvin) and | ||
* [Celsius](https://en.wikipedia.org/wiki/Celsius) on Wikipedia for more | ||
* information about their story, how do they work, when and why they should be | ||
* used, etc.. | ||
* @author [David Leal](https://github.com/Panquesito7) | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <cmath> /// for std::abs | ||
#include <iostream> /// for IO operations | ||
|
||
/** | ||
* @namespace | ||
* @brief Other algorithms | ||
*/ | ||
namespace others { | ||
/** | ||
* @brief Compare two floating point numbers with a certain tolerance. | ||
* This is needed as with some values, the result (e.g.: -196.15) might be a bit | ||
* lower (in this case, -196.499999...). | ||
* @param a the first number to compare | ||
* @param b the second number to compare | ||
* @param tolerance the tolerance to use when comparing the numbers | ||
* @returns true if the numbers ARE equal within the given tolerance | ||
* @returns false if the numbers are NOT equal within the given tolerance | ||
* otherwise | ||
*/ | ||
bool are_almost_equal(double a, double b, double absolute_tolerance = 0.0001) { | ||
return std::abs(a - b) < absolute_tolerance; | ||
} | ||
|
||
/** | ||
* @brief Conversion from Kelvin to Celsius algorithm. | ||
* @param number the Celsius number that will be used to convert | ||
* @returns the Kelvin number converted to Celsius | ||
*/ | ||
double kelvin_to_celsius(double temperature_in_k) { | ||
const double absolute_zero_in_c = -273.15; | ||
if (temperature_in_k < absolute_zero_in_c) { | ||
throw std::invalid_argument("input temperature below absolute zero"); | ||
} | ||
return temperature_in_k + absolute_zero_in_c; | ||
} | ||
} // namespace others | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void tests() { | ||
assert(others::are_almost_equal(others::kelvin_to_celsius(230), -43.15)); | ||
assert(others::are_almost_equal(others::kelvin_to_celsius(512), 238.85)); | ||
assert(others::are_almost_equal(others::kelvin_to_celsius(55), -218.15)); | ||
assert(others::are_almost_equal(others::kelvin_to_celsius(77), -196.15)); | ||
assert(others::are_almost_equal(others::kelvin_to_celsius(9.78), -263.37)); | ||
assert(others::are_almost_equal(others::kelvin_to_celsius(15), -258.15)); | ||
assert(others::are_almost_equal(others::kelvin_to_celsius(273.15), 0)); | ||
|
||
std::cout << "All tests have successfully passed!\n"; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
tests(); // run self-test implementations | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IO operations is perfectly valid as we are doing input and output operations. And this is also how the documentation specifies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@realstealthninja please have a look here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I/O operations cout is an output operation it makes sense to me. You have to understand that if you want a change you don't do it through precedents because precedents are repelled and lost to time. If you want change create a discussion on it and if enough people agree it will be written into contribution.md. i am merely a man of the document it would be a much more useful debate if it was on the discussion board
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I don't think either should exist 🤷🏼. It is perfectly clear to every c++ programmer that starts out with a "Hello world" program what
iostream
is 😄 (just my 2 pennies)