Velocity Reviews > Heat_Index calculation in my C program; question about formula

Heat_Index calculation in my C program; question about formula

Barry L. Bond
Guest
Posts: n/a

 10-25-2008

Greetings!

I just got a new Peet Brothers Ultimeter 2100 Weather Station. This
new one has a way to display the heat index, if you press the "dew point"
key twice.

Being aware of all the controversy surrounding the exact calculation
of the heat index, I would like my own software (which I programmed in the
1990's, when I got their Ultimeter 2000 weather station, but that one
didn't show heat index) to display it. Living in Florida, when it's hot,
in particular, I wouldn't mind knowing what it is, even if I'm not close
to the actual weather station mounted in my office to press the "d.p." key
twice. (My software shows all the current and daily record weather
conditions, updated twice a minute.)

Here is the heat index formula:

<<>>
-42.379 + 2.04901523T + 10.14333127R - 0.22475541TR - 6.83783x10-3T2 - 5.481717x10-2R2 + 1.22874x10-3T2R + 8.5282x10-4TR2 - 1.99x10-6T2R2
<<>>

And, here is the line in my C program:

<<>>
Heat_Index = (-42.379) + (2.04901523 * Temperature) +
(10.14333127 * Humidity) -
(0.22475541 * Temperature * Humidity) -
(6.83783 * powf (10.0, -3) * powf (Temperature, 2)) -
(5.481717 * powf (10.0, -2) * powf (Humidity, 2)) +
(1.22874 * powf (10.0, -3) * powf (Temperature, 2) *
Humidity) + (8.5282 * powf (10.0, -4) * Temperature *
powf (Humidity, 2)) - (1.99 * powf (10.0, -6) *
powf (Temperature, 2) * powf (Humidity, 2));
<<>>

Don't laugh!

Temperature and Humidity are correct, as I've viewed it in gdb. It's
probably not surprising that I'm NOT getting what the weather station is
getting for the heat index.

I'm not mathematically educated, so I'll bet there is something that
I'm not properly capturing from the above formula in my C code.

Especially where it says things like "1.99x10-6T2R2".

I am thinking the "times Temperature squared and times humidity
squared" is probably okay, but that "x10-6" part, I'm not sure that's
right. Since there is no variable there, why wouldn't they just modify
the number before the "x"?

Anyway, if anyone doesn't mind talking with extreme patience to
someone who truly doesn't know whether what I have is right (but I know
it's not getting the correct amount), what should I do?

You don't have to rewrite the whole statement. If you could just tell
me how I handle things like the "1.99x10-6T2R2", I expect I would be able
to do the other "x" portions appropriate!

Thank you!

Barry
--
Barry L. Bond | http://home.cfl.rr.com/os9barry/
Software Engineer, ITT Corporation | (My personal home web page, last
bbondATcfl.rr.com | updated February 17, 2005)

Paul
Guest
Posts: n/a

 10-25-2008
Barry L. Bond wrote:
> Greetings!
>
> I just got a new Peet Brothers Ultimeter 2100 Weather Station. This
> new one has a way to display the heat index, if you press the "dew point"
> key twice.
>
> Being aware of all the controversy surrounding the exact calculation
> of the heat index, I would like my own software (which I programmed in the
> 1990's, when I got their Ultimeter 2000 weather station, but that one
> didn't show heat index) to display it. Living in Florida, when it's hot,
> in particular, I wouldn't mind knowing what it is, even if I'm not close
> to the actual weather station mounted in my office to press the "d.p." key
> twice. (My software shows all the current and daily record weather
> conditions, updated twice a minute.)
>
> Here is the heat index formula:
>
> <<>>
> -42.379 + 2.04901523T + 10.14333127R - 0.22475541TR - 6.83783x10-3T2 - 5.481717x10-2R2 + 1.22874x10-3T2R + 8.5282x10-4TR2 - 1.99x10-6T2R2
> <<>>
>
> And, here is the line in my C program:
>
> <<>>
> Heat_Index = (-42.379) + (2.04901523 * Temperature) +
> (10.14333127 * Humidity) -
> (0.22475541 * Temperature * Humidity) -
> (6.83783 * powf (10.0, -3) * powf (Temperature, 2)) -
> (5.481717 * powf (10.0, -2) * powf (Humidity, 2)) +
> (1.22874 * powf (10.0, -3) * powf (Temperature, 2) *
> Humidity) + (8.5282 * powf (10.0, -4) * Temperature *
> powf (Humidity, 2)) - (1.99 * powf (10.0, -6) *
> powf (Temperature, 2) * powf (Humidity, 2));
> <<>>
>
> Don't laugh!
>
> Temperature and Humidity are correct, as I've viewed it in gdb. It's
> probably not surprising that I'm NOT getting what the weather station is
> getting for the heat index.
>
> I'm not mathematically educated, so I'll bet there is something that
> I'm not properly capturing from the above formula in my C code.
>
> Especially where it says things like "1.99x10-6T2R2".
>
> I am thinking the "times Temperature squared and times humidity
> squared" is probably okay, but that "x10-6" part, I'm not sure that's
> right. Since there is no variable there, why wouldn't they just modify
> the number before the "x"?
>
> Anyway, if anyone doesn't mind talking with extreme patience to
> someone who truly doesn't know whether what I have is right (but I know
> it's not getting the correct amount), what should I do?
>
> You don't have to rewrite the whole statement. If you could just tell
> me how I handle things like the "1.99x10-6T2R2", I expect I would be able
> to do the other "x" portions appropriate!
>
> Thank you!
>
> Barry

Page 41 has the equation. Checked second doc for units for TEMP and HUMIDITY.

http://gis.esri.com/library/userconf..._gis-based.pdf
http://www.srh.noaa.gov/ffc/html/studies/ta_htindx.PDF

Assuming no transcription errors, I read the equation this way.

-42.40 +
(2.04901523 * TEMP) +
(10.14333127 * HUMIDITY) -
(0.22475541 * TEMP * HUMIDITY) -
(0.00683783 * TEMP * TEMP) -
(0.05481717 * HUMIDITY * HUMIDITY) +
(0.00122874 * TEMP * TEMP * HUMIDITY) +
(0.00085282 * TEMP * HUMIDITY * HUMIDITY) -
(0.00000199 * TEMP * TEMP * HUMIDITY * HUMIDITY)

TEMP = ambient dry bulb temperature in degrees Farenheit
HUMIDITY = relative humidity as integer percentage 0..100

If it still doesn't work for you, include a sample (TEMP, HUMIDITY, HI)
result so there is something to debug with.

HTH,
Paul

Barry L. Bond
Guest
Posts: n/a

 10-25-2008

Hi Richard!

>Although my system doesn't have a powf function, I presume it's a float
>equivalent of pow. So I wrote my own float equivalent of pow, and compared
>float calcs against double calcs, for temperatures in the range 0 to 30
>(varying by 0.1) and humidities in the range 0-100 (varying by 1.0). Most
>of the time, there was remarkably little difference between the two -
>quite a few zeros after the point - but occasionally there were what I
>would term minor discrepancies - e.g. T=5.2 H=83 gives a difference of
>0.000030517578125 between a float calc and a double calc of the above
>equation.

Yes. I found a man page for powf, that's what it is.

>You might want to give some example data, and tell us what results the
>weather station gets for those data and what results your program gets for
>the same data.

Ah! Okay.

Running it just now, with 74.5999985 F as the temp and 88.5% as the
humidity, the Heat_Index is 73.6352844. The Weather Station currently shows
79 F.

(Actually, I just noticed I'm making a one decimal point placement
error in the displaying of it, so it actually isn't as far off as I
thought earlier! But, it is still different from what the weather
station reports for Heat Index.)

>Is the equation generally accepted? Is your version an approximation? Is
>theirs? Are the constants considered canonical, or are they merely
>suggestions?

Yes. In fact, I was given three URLs from Peet Brothers, and one of
the URLs is the exact same page as the second link in Paul's reply to my
article. I printed that one side/both sides of paper, and I'm looking at
exactly that same text.

They said they used exactly the same calculation to calculate the
heat index in their Ultimeter 2100 as that very URL.

Merely suggestions, I'd say. But, I was rather wanting to see it,
again, especially when it was 95+ and high humidity, as we have a lot in
our summer months in Florida.

>No, that's fine - it's just a way of expressing numbers in a consistent
>format. The idea is that any (real) number that can be expressed in a
>finite number of decimals can also be expressed in this way:

>optional sign
>value in the range [1.0, 10.0)... the notation here means that 10.0 itself
>isn't part of the range
>multiplication sign
>optional sign
>exponent

>This is so well-established that the C language has a notation for it.
>Instead of writing (6.83783 * powf (10.0, -3)), for example, you can just
>write (6.83783E-3), where - in this context - E stands for "times ten to
>the power of".

Ah! That makes sense! Thank you for your kind and patient help!

Barry
--
Barry L. Bond | http://home.cfl.rr.com/os9barry/
Software Engineer, ITT Corporation | (My personal home web page, last
bbondATcfl.rr.com | updated February 17, 2005)

Barry L. Bond
Guest
Posts: n/a

 10-25-2008

Hi Paul!

>Page 41 has the equation. Checked second doc for units for TEMP and HUMIDITY.

>http://gis.esri.com/library/userconf..._gis-based.pdf
>http://www.srh.noaa.gov/ffc/html/studies/ta_htindx.PDF

The second URL is exactly the same PDF file that Peet Brothers
emailed me, and they said they used the exact same calculation to get
their heat index.

>Assuming no transcription errors, I read the equation this way.

>-42.40 +
>(2.04901523 * TEMP) +
>(10.14333127 * HUMIDITY) -
>(0.22475541 * TEMP * HUMIDITY) -
>(0.00683783 * TEMP * TEMP) -
>(0.05481717 * HUMIDITY * HUMIDITY) +
>(0.00122874 * TEMP * TEMP * HUMIDITY) +
>(0.00085282 * TEMP * HUMIDITY * HUMIDITY) -
>(0.00000199 * TEMP * TEMP * HUMIDITY * HUMIDITY)

>TEMP = ambient dry bulb temperature in degrees Farenheit
>HUMIDITY = relative humidity as integer percentage 0..100

Yes, that's how I'm reading it, thanks to what Richard posted!

>If it still doesn't work for you, include a sample (TEMP, HUMIDITY, HI)
>result so there is something to debug with.

Something I just gave Richard, Temperature 74.5999985 and Humidity
88.5% gives 73.6352844. The weather station gives 79. (It rounds to the
nearest degree in what it displays. But, for things that it gives to my
software, most things are one decimal point.)

Barry
--
Barry L. Bond | http://home.cfl.rr.com/os9barry/
Software Engineer, ITT Corporation | (My personal home web page, last
bbondATcfl.rr.com | updated February 17, 2005)

George
Guest
Posts: n/a

 10-25-2008
On Sat, 25 Oct 2008 08:31:41 +0000, Richard Heathfield wrote:

> Barry L. Bond said:
>
> <snip>
>
>> Running it just now, with 74.5999985 F as the temp and 88.5% as the
>> humidity, the Heat_Index is 73.6352844. The Weather Station currently
>> shows 79 F.

>
> Using double, I get 73.63526917-. Using float, I get 73.63528442+, which
> matches your result. The difference is 0.000015258789062.
>
> Without knowing what the proper result should be, I don't see what other
> help I can give.

I'd be curious to see your source. I have a similar project.
--
George

The action we take and the decisions we make in this decade will have
consequences far into this century. If America shows weakness and
uncertainty, the world will drift toward tragedy. That will not happen on
my watch.
George W. Bush

Keith Thompson
Guest
Posts: n/a

 10-25-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) (Barry L. Bond) writes:
[...]
> Here is the heat index formula:
>
> <<>>
> -42.379 + 2.04901523T + 10.14333127R - 0.22475541TR - 6.83783x10-3T2 - 5.481717x10-2R2 + 1.22874x10-3T2R + 8.5282x10-4TR2 - 1.99x10-6T2R2
> <<>>
>
> And, here is the line in my C program:
>
> <<>>
> Heat_Index = (-42.379) + (2.04901523 * Temperature) +
> (10.14333127 * Humidity) -
> (0.22475541 * Temperature * Humidity) -
> (6.83783 * powf (10.0, -3) * powf (Temperature, 2)) -
> (5.481717 * powf (10.0, -2) * powf (Humidity, 2)) +
> (1.22874 * powf (10.0, -3) * powf (Temperature, 2) *
> Humidity) + (8.5282 * powf (10.0, -4) * Temperature *
> powf (Humidity, 2)) - (1.99 * powf (10.0, -6) *
> powf (Temperature, 2) * powf (Humidity, 2));
> <<>>

[...]

None of your calls to powf() are necessary. In 5 places, you use it
where a floating-point constant would be clearer:

6.83783 * powf (10.0, -3) should be 6.83783e-3
5.481717 * powf (10.0, -2) should be 5.481717e-2
and so forth

The other calls all use an exponent of 2, i.e., you're just computing
the square of a number. IMHO it would be clearer to write
"Humidity*Humidity" rather than powf (Humidity, 2). And since both
Humidity*Humidity and Temperature*Temperature are used several times,
I'd probably compute them separately:

const double Humidity_Squared = Humidity * Humidity;
const double Temperature_Squared = Temperature * Temperature;

Make sure Heat_Index, Humidity, and Temperature are all declared as
type double.

Here's my version of the formula. (Note that it matches the formula

Heat_Index = -42.379
+ 2.04901523 * Temperature
+ 10.14333127 * Humidity
- 0.22475541 * Temperature * Humidity
- 6.83783e-3 * Temperature_Squared
- 5.481717e-2 * Humidity_Squared
+ 1.22874e-3 * Temperature_Squared * Humidity
+ 8.5282e-4 * Temperature * Humidity_Squared
- 1.99e-6 * Temperature_Squared * Humidity_Squared;

Note that arranged the expression so that each term is on a line by
itself. I've also dropped all the parentheses, taking advantage of
the fact that "*" binds more tightly than "+" and "-". Adding
parentheses to complicated expressions in C often adds clarity, but in
this case the precedence is sufficiently well known that parentheses

Here's another version of the same formula, which more closely follows
the mathematical form as given on the Wikipedia page; I find it
clearer than the above:

/* T is temperature in degrees Fahrenheit,
* R is relative humidity in percent
*/
double Heat_Index(double T, double R)
{
const double C1 = -42.379;
const double C2 = 2.04901523;
const double C3 = 10.14333127;
const double C4 = -0.22475541;
const double C5 = -6.83783e-3;
const double C6 = -5.481717e-2;
const double C7 = 1.22874e-3;
const double C8 = 8.5282e-4;
const double C9 = -1.99e-6;
return C1 + C2*T + C3*R + C4*T*R + C5*T*T + C6*R*R +
C7*T*T*R + C8*T*R*R + C9*T*T*R*R;
}

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Tim Prince
Guest
Posts: n/a

 10-25-2008
Keith Thompson wrote:

> return C1 + C2*T + C3*R + C4*T*R + C5*T*T + C6*R*R +
> C7*T*T*R + C8*T*R*R + C9*T*T*R*R;

more efficient would be a grouping of terms such as
C1 + T*(C2 + T*(C5 + R*(C7 + C9*R)) + R*(C4 + C8*R)) + R*(C3 + C6*R)

The lack of an exponentiation operator in C, the existence of Horner's
rule, and the explicit allowance for such regrouping in the standards
for certain other programming languages, all should point in this direction.

Keith Thompson
Guest
Posts: n/a

 10-25-2008
Tim Prince <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>
>> return C1 + C2*T + C3*R + C4*T*R + C5*T*T + C6*R*R +
>> C7*T*T*R + C8*T*R*R + C9*T*T*R*R;

> more efficient would be a grouping of terms such as
> C1 + T*(C2 + T*(C5 + R*(C7 + C9*R)) + R*(C4 + C8*R)) + R*(C3 + C6*R)
>
> The lack of an exponentiation operator in C, the existence of Horner's
> rule, and the explicit allowance for such regrouping in the standards
> for certain other programming languages, all should point in this
> direction.

The greater complexity and obscurity of your version, the fact that
compilers are likely to perform common subexpression optimizations
(e.g., computing T*T and R*R only once) should point away from your
version and in the direction of greater clarity. Unless the
computation is time-critical, and this change improves performance
measurably.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Barry L. Bond
Guest
Posts: n/a

 10-26-2008

Hi again, Richard!

>> Running it just now, with 74.5999985 F as the temp and 88.5% as the
>> humidity, the Heat_Index is 73.6352844. The Weather Station currently
>> shows 79 F.

>Using double, I get 73.63526917-. Using float, I get 73.63528442+, which
>matches your result. The difference is 0.000015258789062.

Thank you. I will be changing it, to use the E- notation,
etc. However it is good to know that you're getting the same thing, or
close enough!

>Without knowing what the proper result should be, I don't see what other
>help I can give.

Oh, I'm sorry.

Peet Brothers emailed me 3 URLs, and the second URL is the 2-page PDF
file (that perfectly matches Paul's second URL he posted). They said that
they used that exact calculation for the Ultimeter 2100. So, they (said
that they) use the exact same calculation.

I'm not sure what to think now... but at least my Heat_Index appears
to be "working", though I now know a couple of improvements!

Barry
--
Barry L. Bond | http://home.cfl.rr.com/os9barry/
Software Engineer, ITT Corporation | (My personal home web page, last
bbondATcfl.rr.com | updated February 17, 2005)

Barry L. Bond
Guest
Posts: n/a

 10-26-2008

Hi George!

>I'd be curious to see your source. I have a similar project.

Oh boy. While I have many things I wouldn't mind other people
seeing the source code, this one isn't one of them!

Still, though, it DOES work. (I was going to add some things and
improve it from writing it in the 1990's, but I just haven't gotten to
it.) :-O It DOES work, and I actually run it frequently, and especially
when there is any sort of "weather emergency" or situation that is like to
have some weather item different.

If you can give me an email address that would work for you, I'll
email it to you. I have no reason not to share it, since you explicitly

You can do whatever you want to "mess up" the email (so it can't be
grabbed by software scanning USENET posts) but still be usable to me, such
as the way I do my email address at the end.

Barry
--
Barry L. Bond | http://home.cfl.rr.com/os9barry/
Software Engineer, ITT Corporation | (My personal home web page, last
bbondATcfl.rr.com | updated February 17, 2005)