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
28
namespace
others
{
40
bool
are_almost_equal
(
double
a,
double
b,
double
absolute_tolerance = 0.0001) {
41
return
std::abs(a - b) < absolute_tolerance;
42
}
43
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
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
78
int
main
() {
79
tests
();
// run self-test implementations
80
return
0;
81
}
tests
static void tests()
Self-test implementations.
Definition
kelvin_to_celsius.cpp:62
main
int main()
Main function.
Definition
kelvin_to_celsius.cpp:78
others
for vector
others::kelvin_to_celsius
double kelvin_to_celsius(double temperature_in_k)
Conversion from Kelvin to Celsius algorithm.
Definition
kelvin_to_celsius.cpp:49
others::are_almost_equal
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,...
Definition
kelvin_to_celsius.cpp:40
others
kelvin_to_celsius.cpp
Generated by
1.12.0