C++ std::isfinite
Check whether a temperature is a finite number before including it in a daily average.
A simple example
std::isfinite
- Start with 2.0
- Do this Test for a finite value
- Result Yes
#include <iostream>
#include <cmath>
int main() {
std::cout << ((std::isfinite(2.0)) ? "yes" : "no") << "\n";
return 0;
}
yes
Syntax
std::isfinite(21.5)
Run these examples in C++
Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.
c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lessonReject unusable sensor readings
A sensor feed can contain values that cannot be used in an ordinary calculation. Check the incoming readings before allowing them to affect a stored average or a chart.
#include <iostream>
#include <cmath>
int main() {
std::cout << ((std::isfinite(21.5)) ? "yes" : "no") << "\n";
return 0;
}
yes
A measurement of 21.5 is finite. A missing calculation represented by NaN or an infinite value fails this test.
Plot a freezer sensor reading
A freezer probe reports a numeric temperature below zero. Test whether the reading is a finite number before putting it on the chart.
Skip an unavailable light reading
A disconnected light meter uses NaN to mark an unavailable measurement. Test whether the reading is a finite number before putting it on the chart.
What to watch for
A finite reading can still be implausible. Apply physical range checks separately.