Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Float as Infinity

Reply
Thread Tools

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;

 
Reply With Quote
 
 
 
 
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@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
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)
 
Reply With Quote
 
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;
> /*
> read loop goes here...
> 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.


 
Reply With Quote
 
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)
 
Reply With Quote
 
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.


 
Reply With Quote
 
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)

 
Reply With Quote
 
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Infinity + Infinity (or NegInfinity - NegInfinity) Skybuck Flying C Programming 40 01-25-2012 12:25 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Inconsistency producing constant for float "infinity" Peter Hansen Python 6 08-12-2006 11:16 PM
Infinity and -Infinity Jon A. Lambert Ruby 3 11-06-2005 04:46 AM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments