Last active
August 1, 2025 07:29
-
-
Save LightningStalker/406ffa4f9d25e6a8b46661fccce9c086 to your computer and use it in GitHub Desktop.
Find the heat index
This file contains hidden or 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
| /* compile with $ g++ -Wall -Os -o heat_index heat_index.cpp | |
| * | |
| * ported to C++ from meteocalc by Project Crew™ 7/28/2025 | |
| * tested against "Heat index calculator" of Calculator.net | |
| * see https://en.wikipedia.org/wiki/Heat_index | |
| * http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml | |
| */ | |
| #include <iostream> | |
| #include <iomanip> | |
| #include <numeric> | |
| #include <cmath> | |
| #include <string> | |
| #include <filesystem> | |
| using namespace std; | |
| namespace fs = std::filesystem; | |
| void | |
| usage(string argv0) | |
| { | |
| cerr | |
| << endl | |
| << " " << argv0 << " needs temp°F and relative%humidity." << endl | |
| << " output is heat index in °F" << endl | |
| << endl | |
| << " Usage: " << argv0 << " [°F] [%]" << endl | |
| << " Example: " << argv0 << " 89.16 69.68" << endl | |
| << " Output should be: 103.26" << endl | |
| << endl; | |
| } | |
| int | |
| main(int argc, char * argv[]) | |
| { | |
| float HI, T, RH; | |
| if (argc == 3) | |
| { | |
| try | |
| { | |
| /* shell args passed in */ | |
| T = stod(argv[1]); | |
| RH = stod(argv[2]); | |
| } catch (invalid_argument& e) | |
| { | |
| cerr << "error: arguments must be numeric." << endl; | |
| return EXIT_FAILURE; | |
| } | |
| /* coefficients from human body model */ | |
| const float | |
| c1 = -42.379, | |
| c2 = 2.04901523, | |
| c3 = 10.14333127, | |
| c4 = -0.22475541, | |
| c5 = -6.83783e-3, | |
| c6 = -5.481717e-2, | |
| c7 = 1.22874e-3, | |
| c8 = 8.5282e-4, | |
| c9 = -1.99e-6; | |
| /* array of intermediate polynomial */ | |
| float poly[] = { | |
| c1, | |
| c2 * T, | |
| c3 * RH, | |
| c4 * T * RH, | |
| c5 * powf(T, 2), | |
| c6 * powf(RH, 2), | |
| c7 * powf(T, 2) * RH, | |
| c8 * T * powf(RH, 2), | |
| c9 * powf(T, 2) * powf(RH, 2), | |
| }; | |
| if (T >= 80 && RH >= 40) | |
| { | |
| /* sum up our array */ | |
| HI = accumulate(begin(poly), end(poly), 0.0, plus<float>()); | |
| }else | |
| { | |
| /* using simplified formula */ | |
| HI = 0.5 * (T + 61. + (T - 68.) * 1.2 + RH * 0.094); | |
| } | |
| /* output */ | |
| cout << fixed << setprecision(2) | |
| << HI << endl; | |
| }else | |
| { | |
| /* declare path and initialize */ | |
| fs::path argv0(argv[0]); | |
| /* basename */ | |
| usage(string(argv0.stem())); | |
| return EXIT_FAILURE; | |
| } | |
| /* the end */ | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment