Float as Infinity

E

Eric Sosman

The87Boy wrote On 09/25/07 03:25,:
What is the longest number, you can have in C?
And which data-type do I have to use then?

What do you mean by "longest?" Greatest value,
greatest precision, greatest amount of storage used,
something else?

And what do you mean by "number?" Natural numbers,
integers, floating-point numbers, complex numbers,
quaternions, ...?

And what do you mean by "in C?" Built into the
language and library, or implementable with them?

Rather than trying to answer these rhetorical
questions, try turning the problem around: What do
you want to *do* with this "longest number?" Choose
a hammer or choose a saw according to the job you want
to do, not according to which is "longer."
 
M

Mark McIntyre

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.


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
P

pete

The87Boy said:
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.
 
U

user923005

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;
}
 
B

Barry Schwarz

The87Boy said:
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.


Remove del for email
 
B

Barry Schwarz

How can I initialize the values then?

You don't initialize the values or alternately it doesn't matter what
value you initialize them to. As soon as you read the first number,
you assign that value to both min and max and then process the rest of
the numbers through two if statements.


Remove del for email
 
K

Keith Thompson

Barry Schwarz said:
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.

One problem with that approach is that it indicates that a sequence
with no elements has a minimum and maximum values of, say INT_MAX and
INT_MIN, respectively, which really doesn't make much sense. It's
better, IMHO, to report that there is no minumum or maximum value --
and if you're handling an empty sequence specially anyway, there's
less advantage in using the extreme values.

It makes the code depend on the element type, making it too easy to
forget to change the bounds when you change the type. And consider
trying to use the same algorithm for a sequence of "bignum" values
(arbitrary-precision numbers) where there are no minimum or maximum
values.

But it depends on the requirements. If the requirements say that
you're to return INT_MAX and INT_MIN for an empty list, then that's
what you do. And if the requirements are silent on the question,
either ask for clarification or do what you think makes the most sense
while leaving room for changes.
 
T

The87Boy

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?
 
U

user923005

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;
 
E

Eric Sosman

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'.
 
C

Charlie Gordon

user923005 said:
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 -2147483648).
DBL_MIN is the smallest non-zero positive value that can be represented in a
double (typically 2.2250738585072014E-308).
The equivalent of INT_MIN for a double is -DBL_MAX, not DBL_MIN.
 
E

Eric Sosman

Barry Schwarz wrote On 09/25/07 21:37,:
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?
 
C

Charlie Gordon

Eric Sosman said:
Barry Schwarz wrote On 09/25/07 21:37,:

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 !
 
E

Eric Sosman

Charlie Gordon wrote On 09/27/07 05:36,:
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.
 
U

user923005

Charlie Gordon wrote On 09/27/07 05:36,:



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 > a[i + 1]) {
max_et = max_et > a ? max_et : a;
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 ? min_et : a;
}
}
*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
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top