--Calculates the heat index based on temperature in degrees --fahrenheit and percent of relative humidity. --Heat index formula taken from --Http://Www.Usatoday.Com/Weather/Whicalc.htm --Written By: Ralph W. Reid --Last Modified: February 26, 2005 --This is the second version of this program. The first version --produced a warning during compilation due to the use of unchecked --conversion of a character into an integer (different sizes). This --version of this program produces no such warning during compilation --while using gnat 3.3.4 on a Slackware Linux 10.1 system with --kernel 2.4.29. --This source code is freeware. Give credit/blame as appropriate. --No warranties apply. --Simple compile: gnatmake hi_ada with Ada.Command_Line; with Ada.Exceptions; with Ada.Io_Exceptions; with Ada.Text_Io; use Ada.Text_Io; with Ada.Float_Text_Io; use Ada.Float_Text_Io; function Hi_Ada return Natural is function Atof (workstr : in String) return Float is --Returns a floating point value of the workstr parameter. The --returned value is undefined if workstr contains invalid characters. --define a float array to define floating point digit values type Floatarray is array (0 .. 9) of Float; --variable definitions I : Natural range 1 .. (workstr'length + 1) := 1; Keep_Working : Boolean := TRUE; Multiplier : Float := 0.1; Negative : Boolean := FALSE; Result : Float := 0.0; Values : Floatarray := (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); Workchar : Character := ' '; Zerochar : Integer := Character'Pos ('0'); begin --deal with an empty string if (Workstr'Length = 0) then return 0.0; end if; --get past any initial white space Workchar := Workstr (I); while (workchar <= ' ' and I <= Workstr'length) loop I := I + 1; if (I <= Workstr'Length) then Workchar := Workstr (I); end if; end loop; --handle any numeric negation while (workchar = '-' and I <= Workstr'Length) loop if Negative = FALSE then Negative := TRUE; else Negative := FALSE; end if; I := I + 1; if (I <= Workstr'Length) then Workchar := Workstr (I); end if; end loop; --perform the whole number digit conversions while (I <= Workstr'Length and workchar >= '0' and workchar <= '9') loop Result := Result * 10.0; Result := Result + Values (Character'Pos (workchar) - zerochar); I:= I + 1; if (I <= Workstr'Length) then Workchar := Workstr (I); end if; end loop; --deal with fractional part of number if needed if (workchar = '.') then I := I + 1; if (I <= Workstr'Length) then Workchar := Workstr (I); end if; while (I <= Workstr'Length and Workchar >= '0' and Workchar <= '9') loop result := result + multiplier * Values (Character'Pos (workchar) - zerochar); Multiplier := Multiplier * 0.1; I := I + 1; if (I <= Workstr'Length) then Workchar := Workstr (I); end if; end loop; end if; --negate the result if necessary if Negative then Result := -1.0 * Result; end if; --return the final result return Result; end Atof; ---------------------------------------------------------------------- function Calc_Heat_Index (Temp : in float; Humidity : in float) return float is --This function returns the heat index based on temperature in degrees --fahrenheit and percentage of relative humidity. The temperature and --relative humidity values are expected to be within valid ranges. begin return (-42.379 + (2.04901523 * Temp) + (10.14333127 * Humidity) - (0.22475541 * Temp * Humidity) - (6.83783 * 0.001 * (Temp * temp)) - (5.481717 * 0.01 * (Humidity * Humidity)) + (1.22874 * 0.001 * (Temp * temp) * Humidity) + (8.5282 * 0.0001 * temp * (Humidity * humidity)) - (1.99 * 0.000001 * (Temp * temp) * (Humidity * humidity))); end Calc_Heat_Index; ---------------------------------------------------------------------- --mainline code --temperature range is from about absolute 0 to near plasma? Lowtemp : Float := -279.4; Hightemp : Float := 3000.0; --relative humidity is from 0.0 to 100.0 percent Lowhumidity : Float := 0.0; Highhumidity : Float := 100.0; temp : Float range lowtemp .. hightemp := 0.0; humidity : Float range lowhumidity .. highhumidity := 0.0; result : Float range lowtemp .. Float'last := 0.0; begin if Ada.Command_Line.Argument_Count = 0 then Put_Line ("Program for calculating heat index"); Put_Line ("Written in Ada95 by: Ralph W. Reid"); Put_Line ("Last Modified: February 26, 2005"); New_Line; Put_Line ("No command line arguments found."); Put_Line ("In the future, fahrenheit degrees and relative"); Put_Line ("humidity can be passed on the command line if"); Put_Line ("desired."); New_Line (2); Put ("Temp in degrees fahrenheit ("); Put (lowtemp, 0, 0, 0); Put (" through "); Put (hightemp, 0, 0, 0); Put_Line ("):"); Get (Temp); Put ("Relative humidity percentage ("); Put (Lowhumidity, 0, 0, 0); Put (" through "); Put (Highhumidity, 0, 0, 0); Put_Line ("):"); Get (Humidity); New_Line; else if Ada.Command_Line.Argument_Count = 1 then Temp := atof (Ada.Command_Line.Argument (1)); Put ("Relative humidity percentage ("); Put (Lowhumidity, 0, 0, 0); Put (" through "); Put (Highhumidity, 0, 0, 0); Put_Line ("):"); Get (Humidity); New_Line; else Temp := Atof (Ada.Command_Line.Argument (1)); Humidity := Atof (Ada.Command_Line.Argument (2)); end if; end if; --calculate the heat index result := Calc_Heat_Index (temp, humidity); Put ("The heat index is "); Put (Result, 0, 0, 0); Put_Line (" degrees fahrenheit."); --return a no-error condition return 0; end Hi_Ada;