Heat_Index calculation in my C program; question about formula

U

user923005

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!  :-D

     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!  :)

A more accurate heat index formula:
http://www.weatherimages.org/data/heatindex.html
 
G

George

Turns out you are right about the brains of modern compilers:

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

#ifdef UNIT_TEST

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static const double clock_inv = 1.0 / CLOCKS_PER_SEC;
int main(void)
{
double temperature;
double humidity;
double sum;
clock_t start,
end;

sum = 0;
start = clock();
for (temperature = 32.0; temperature <= 120.0; temperature +=
0.01)
for (humidity = 0; humidity <= 100.0; humidity += 0.01)
sum += Heat_Index(temperature, humidity);
end = clock();
printf("Sum for expanded evaluation is %g which took %g seconds
\n", sum, (end - start) * clock_inv);

sum = 0;
start = clock();
for (temperature = 32.0; temperature <= 120.0; temperature +=
0.01)
for (humidity = 0; humidity <= 100.0; humidity += 0.01)
sum += Heat_Index_Horner(temperature, humidity);
end = clock();
printf("Sum for Horner's evaluation is %g which took %g seconds
\n", sum, (end - start) * clock_inv);
return 0;
}
/*
Sum for expanded evaluation is 9.4831e+009 which took 0.327 seconds
Sum for Horner's evaluation is 9.4831e+009 which took 0.437 seconds
*/
#endif /* UNIT_TEST */

Oh my goodness, we have one result that is far less than a second and a
second result that is more or less the first.

Speaking only for my purposes, the tenth of a second between the solutions
might be the difference between whether Keith reaches for the mayo, the
ham, or the radish. One might be less zealous than the others.

--
George

Iraq is no diversion. It is a place where civilization is taking a decisive
stand against chaos and terror, we must not waver.
George W. Bush
 
D

David Thompson

Barry L. Bond said:
Presumably the -6, and 2 and 2, were or should be superscripts.
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:
Not just finite but small, and in particular, in science, usually only
the digits needed to represent the significant digits of a
measurement, or value computed based on measurement(s) --
rather than using 'placeholder' zeroes whose only function is to put
the significant digits far enough left or right of the decimal point.

It appears to me that http://en.wikipedia.org/wiki/Scientific_notation
has this one just about right (and nicely organized and illustrated).
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
insert 10 and 'power-of' operator or equivalently superscripting
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".

Right. Actually nearly all programming languages have adopted this
notation, originally created for FORTRAN at a time when computers
(really their peripherals) couldn't do superscripts on a dare.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
U

user923005

(e-mail address removed) said:




David Thompson said:
On Sat, 25 Oct 2008 05:42:52 +0000, Richard Heathfield
[scientific notation]
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:
Not just finite but small,
I had no idea there was an upper limit. What is that limit?
LDBL_MANT_DIG.

Dave's claim was not that long double's range was limited, but that the
range expressible by scientific notation was limited.

Both statements are true.

For instance, most integers, rationals, algebraic numbers,
transcendental numbers, p-adics and infinite values are not
expressible in scientific notation[1].
Scientific notation is useful for numerical approximations used in
science or engineering where a finite number of digits are to be
computed and/or displayed.

[1] Under the assumption that the medium (whether paper, electronic,
etc.) is finite. We can certainly create a gedankenexperiment where
our paper, time and other resources are infinite.
 
D

David Thompson

David Thompson said:

[scientific notation]
Not just finite but small,

I had no idea there was an upper limit. What is that limit?

<snip>

Sorry, I misthought that. The number of placeholder zeroes can indeed
be large (but finite); the number of _signficant_ digits is small.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
K

Keith Thompson

David Thompson said:
David Thompson said:

[scientific notation]
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:

Not just finite but small,

I had no idea there was an upper limit. What is that limit?

<snip>

Sorry, I misthought that. The number of placeholder zeroes can indeed
be large (but finite); the number of _signficant_ digits is small.

I think Richard's point is that though there are limits for C
implementations, there is no upper limit *in mathematics* for
scientific notation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top