Velocity Reviews > Float as Infinity

Float as Infinity

user923005
Guest
Posts: n/a

 09-26-2007
On Sep 26, 12:13 am, The87Boy <(E-Mail Removed)> wrote:
> On 25 Sep., 20:58, Mark McIntyre <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Tue, 25 Sep 2007 03:27:07 -0700, in comp.lang.c , The87Boy

>
> > <(E-Mail Removed)> wrote:

>
> > >How can I initialize valueMin and valueMax

>
> > This is NOT a C question. This is an algorithm question. If you don't
> > understand the algo, you cannot safely programme it, so take time to
> > understand the algo first. Here's a suggestion:

>
> > Pick ONE number.

>
> > Work out which of it is the min, and which is the max (easy....).
> > What are valueMin and valueMax set to now?

>
> > Once you've solved this step, pick a second number, and compare it.

>
> It isn't the question, but should I set it to "float
> valueMin=-99999999999999999999999" or?- Hide quoted text -
>
> - Show quoted text -

Set valueMin to the largest possible value:
DBL_MAX
Set valueMax to the most negative possible value:
DBL_MIN

Then the first time you compare something, it will always get
replaced.

The start of your program will look like this:

#include <float.h>
#include <stdio.h>
#include <stdlib.h>

static char string[32767];

int main(void)
{
double valMin = DBL_MAX;
double valMax = DBL_MIN;

Richard Heathfield
Guest
Posts: n/a

 09-26-2007
user923005 said:

<snip>

> Set valueMax to the most negative possible value:
> DBL_MIN

DBL_MIN is required by the Standard to be positive.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Eric Sosman
Guest
Posts: n/a

 09-26-2007
user923005 wrote On 09/26/07 05:10,:
> [...]
> Set valueMin to the largest possible value:
> DBL_MAX
> Set valueMax to the most negative possible value:
> DBL_MIN

Bzzzt! DBL_MIN is a *positive* value. Use
`-DBL_MAX' or `-HUGE_VAL' or (on systems that have
it) `-INFINITY'.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

Charlie Gordon
Guest
Posts: n/a

 09-26-2007
"user923005" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed) m...
> On Sep 25, 3:31 pm, pete <(E-Mail Removed)> wrote:
>> The87Boy wrote:
>>
>> > Hey all

>>
>> > I have a problem with float
>> > I should write a program, where you are getting some numbers from the
>> > command-line and try to find the maximum and minimum-values of these
>> > numbers.

>>
>> The first number entered is both the min and the max.
>>
>> Any subsequent number entered
>> which is greater than the max, is the new max.
>>
>> Any subsequent number entered
>> which is less than the min, is the new min.

>
> Another common way of doing it (especially when you don't have an
> array) is to set the minimum initially to be DBL_MAX and the maximum
> to be DBL_MIN as defined in <float.h> for step 1. I think that this
> is what the O.P. was getting at.
>
> #include <float.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> char string[32767];
> int main(void)
> {
> double valMin = DBL_MAX;
> double valMax = DBL_MIN;
> /*
> comparisons reset valMin & valMax
> */
> return 0;
> }

There seems to be a confusion between the semantics of DBL_MIN and INT_MIN.
INT_MIN is the negative int with the largest absolute value
(typically -32768 or -214748364.
DBL_MIN is the smallest non-zero positive value that can be represented in a
double (typically 2.2250738585072014E-30.
The equivalent of INT_MIN for a double is -DBL_MAX, not DBL_MIN.

--
Chqrlie.

Eric Sosman
Guest
Posts: n/a

 09-26-2007
Barry Schwarz wrote On 09/25/07 21:37,:
> The87Boy wrote:
>
>>I have a problem with float. I should write a program, where you
>>are getting some numbers from the command-line and try to find the
>>maximum and minimum-values of these numbers.
>>I need to initialize 2 floats (one with minus infinity and one with
>>plus infinity), or does anybody else have an idea?

>
>
> If the numbers are integers, use INT_MAX/MIN or LONG_MAX/MIN or
> LLONG_MAX/MIN as appropriate. If they are floating type, use
> FLT_MAX/MIN, DBL_MAX/MIN, or LDBL_MAX/MIN as appropriate.

For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_MIN
are all *positive* values.

The mistake is made so frequently that it must be
easy to make, so let's not make it any easier, okay?

--
(E-Mail Removed)

Charlie Gordon
Guest
Posts: n/a

 09-27-2007
"Eric Sosman" <(E-Mail Removed)> a écrit dans le message de news:
1190818696.415940@news1nwk...
> Barry Schwarz wrote On 09/25/07 21:37,:
>> The87Boy wrote:
>>
>>>I have a problem with float. I should write a program, where you
>>>are getting some numbers from the command-line and try to find the
>>>maximum and minimum-values of these numbers.
>>>I need to initialize 2 floats (one with minus infinity and one with
>>>plus infinity), or does anybody else have an idea?

>>
>>
>> If the numbers are integers, use INT_MAX/MIN or LONG_MAX/MIN or
>> LLONG_MAX/MIN as appropriate. If they are floating type, use
>> FLT_MAX/MIN, DBL_MAX/MIN, or LDBL_MAX/MIN as appropriate.

>
> For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_MIN
> are all *positive* values.
>
> The mistake is made so frequently that it must be
> easy to make, so let's not make it any easier, okay?

The mistake is made so easily and so frequently that it almost qualifies as
a defect
The names are just *so* confusing !

--
chqrlie.

Eric Sosman
Guest
Posts: n/a

 09-27-2007
Charlie Gordon wrote On 09/27/07 05:36,:
> "Eric Sosman" <(E-Mail Removed)> a écrit dans le message de news:
> 1190818696.415940@news1nwk...
>>
>> For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_MIN
>>are all *positive* values.
>>
>> The mistake is made so frequently that it must be
>>easy to make, so let's not make it any easier, okay?

>
>
> The mistake is made so easily and so frequently that it almost qualifies as
> a defect
> The names are just *so* confusing !

They are; something like DBL_SMALLEST might have
avoided confusion. (I'm not heaping scorn on the ANSI
committee for the unfortunate names; I think in this
case the committee just codified "prior art.") But
for now and probably for the life of C we're stuck with
the _MIN names, and must learn to live with them.

--
(E-Mail Removed)

user923005
Guest
Posts: n/a

 09-27-2007
On Sep 27, 7:16 am, Eric Sosman <(E-Mail Removed)> wrote:
> Charlie Gordon wrote On 09/27/07 05:36,:
>
> > "Eric Sosman" <(E-Mail Removed)> a écrit dans le message de news:
> > 1190818696.415940@news1nwk...

>
> >> For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_MIN
> >>are all *positive* values.

>
> >> The mistake is made so frequently that it must be
> >>easy to make, so let's not make it any easier, okay?

>
> > The mistake is made so easily and so frequently that it almost qualifies as
> > a defect
> > The names are just *so* confusing !

>
> They are; something like DBL_SMALLEST might have
> avoided confusion. (I'm not heaping scorn on the ANSI
> committee for the unfortunate names; I think in this
> case the committee just codified "prior art.") But
> for now and probably for the life of C we're stuck with
> the _MIN names, and must learn to live with them.

Believe it or not, I know better.

Anyway, here is my eventual solution (I sent a copy in to Snippets):

#include <stdlib.h>
/*
"Introduction to Algorithms"
Cormen, Leiserson, Rivest
pp. 186,187
ISBN: 0-07-013143-0

Simultaneous min & max
using only 3*N/2 comparisons

Written by Dann Corbit
9/25/2007
Donated to the public domain
*/
#ifdef e_type_LONG_DOUBLE
typedef long double e_type;
#elif defined(e_type_DOUBLE)
typedef double e_type;
#elif defined(e_type_FLOAT)
typedef float e_type;
#elif defined(e_type_UNSIGNED_LONG_LONG)
typedef unsigned long long e_type;
#elif defined(e_type_LONG_LONG)
typedef long long e_type;
#elif defined(e_type_UNSIGNED_LONG)
typedef unsigned long e_type;
#elif defined(e_type_LONG)
typedef long e_type;
#elif defined(e_type_UNSIGNED)
typedef unsigned e_type;
#elif defined(e_type_INT)
typedef int e_type;
#elif defined(e_type_SHORT)
typedef short e_type;
#elif defined(e_type_UNSIGNED_SHORT)
typedef unsigned e_type;
#elif defined(e_type_CHAR)
typedef char e_type;
#elif defined(e_type_UNSIGNED_CHAR)
typedef unsigned char e_type;
#elif defined (__cplusplus)
template < class e_type > // works with stl string class etc...
#endif
void minmax(
e_type * a, // input array
size_t arr_size, // array length
e_type * min_e, // smallest thing found
e_type * max_e // biggest thing found
)
{
e_type min_et;
e_type max_et;
size_t i,
n;
if (arr_size % 2) {
min_et = a[0];
max_et = a[0];
n = 1;
} else {
if (a[0] > a[1]) {
max_et = a[0];
min_et = a[1];
} else {
min_et = a[0];
max_et = a[1];
}
n = 2;
}
for (i = n; i < arr_size; i += 2) {

if (a[i] > a[i + 1]) {
max_et = max_et > a[i] ? max_et : a[i];
min_et = min_et < a[i + 1] ? min_et : a[i + 1];
} else {
max_et = max_et > a[i + 1] ? max_et : a[i + 1];
min_et = min_et < a[i] ? min_et : a[i];
}
}
*min_e = min_et;
*max_e = max_et;
}

#if defined( UNIT_TEST ) && (defined (e_type_DOUBLE) ||
defined( __cplusplus))
#include <stdio.h>

char string[32767];
double foo[32767];
int main(void)
{
size_t i = 0;
double dmin,
dmax;
while (fgets(string, sizeof string, stdin)) {
foo[i++] = atof(string);
if (i > 32766)
break;
}
minmax(foo, i, &dmin, &dmax);
printf("min=%f, max=%f\n", dmin, dmax);
return 0;
}
#endif