TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
kelvin_to_celsius.cpp
Go to the documentation of this file.
1
20#include <cassert>
21#include <cmath>
22#include <iostream>
23
28namespace others {
40bool are_almost_equal(double a, double b, double absolute_tolerance = 0.0001) {
41 return std::abs(a - b) < absolute_tolerance;
42}
43
49double 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
62static void tests() {
70
71 std::cout << "All tests have successfully passed!\n";
72}
73
78int main() {
79 tests(); // run self-test implementations
80 return 0;
81}
static void tests()
Self-test implementations.
int main()
Main function.
for vector
double kelvin_to_celsius(double temperature_in_k)
Conversion from Kelvin to Celsius algorithm.
bool are_almost_equal(double a, double b, double absolute_tolerance=0.0001)
Compare two floating point numbers with a certain tolerance. This is needed as with some values,...