Velocity Reviews > Float as Infinity

# Float as Infinity

The87Boy
Guest
Posts: n/a

 09-24-2007
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.
I need to initialize 2 floats (one with minus infinity and one with
plus infinity), or does anybody else have an idea?

I have made this code until now:
#include <stdio.h>

int main(void) {
int sat = 0;
float max = 0.0, x;
float min = plus-infinity;
printf ("Write some numbers:\n");

while( scanf("%f", &x) == 1) {

if (max<x) {
max=x;
}

if (sat == 0) {
min = x;
sat = 1;
} else {
if (min>x) {
min = x;
}
}
}

printf ("%f is max\n", max);
printf ("%f is min\n", min);
return 0;
}

r6144
Guest
Posts: n/a

 09-24-2007
The macro INFINITY defined in math.h should do (negative infinity is
simply -INFINITY). It is introduced in the newer ISO C99 standard, so
certain compilers might not have them or use some other name.

The87Boy
Guest
Posts: n/a

 09-24-2007
On 24 Sep., 13:15, r6144 <(E-Mail Removed)> wrote:
> The macro INFINITY defined in math.h should do (negative infinity is
> simply -INFINITY). It is introduced in the newer ISO C99 standard, so
> certain compilers might not have them or use some other name.

Is there another way, I can do it then?

Tim Prince
Guest
Posts: n/a

 09-24-2007
The87Boy wrote:
> On 24 Sep., 13:15, r6144 <(E-Mail Removed)> wrote:
>> The macro INFINITY defined in math.h should do (negative infinity is
>> simply -INFINITY). It is introduced in the newer ISO C99 standard, so
>> certain compilers might not have them or use some other name.

>
> Is there another way, I can do it then?
>

Do what? If you want C90 compatibility,
#include <float.h>
initialize to FLT_MIN or FLT_MAX
as there is no supported way of handling infinities.

Eric Sosman
Guest
Posts: n/a

 09-24-2007
The87Boy wrote:
> On 24 Sep., 13:15, r6144 <(E-Mail Removed)> wrote:
>> The macro INFINITY defined in math.h should do (negative infinity is
>> simply -INFINITY). It is introduced in the newer ISO C99 standard, so
>> certain compilers might not have them or use some other name.

>
> Is there another way, I can do it then?

(The "it" that The87Boy trimmed away is initializing two
floating-point numbers to very large and very small values.)

Use HUGE_VAL and -HUGE_VAL, which will exist even on systems
where there are no representations for infinities. Note 1: If
you need float or long double instead of double, use HUGE_VALF
or HUGE_VALL if they exist, or FLT_MAX or LDBL_MAX otherwise.
Note 2: Do not confuse -FLT_MAX and -LDBL_MAX with FLT_MIN and
LDBL_MIN, which are small *positive* values. HUGE_VAL et al.
are in <math.h>, the others are in <float.h>.

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

user923005
Guest
Posts: n/a

 09-24-2007
On Sep 24, 5:58 am, Tim Prince <(E-Mail Removed)> wrote:
> The87Boy wrote:
> > On 24 Sep., 13:15, r6144 <(E-Mail Removed)> wrote:
> >> The macro INFINITY defined in math.h should do (negative infinity is
> >> simply -INFINITY). It is introduced in the newer ISO C99 standard, so
> >> certain compilers might not have them or use some other name.

>
> > Is there another way, I can do it then?

>
> Do what? If you want C90 compatibility,
> #include <float.h>
> initialize to FLT_MIN or FLT_MAX
> as there is no supported way of handling infinities.

Probably FLT_MAX will be a better approximation than FLT_MIN to
infinity. One could argue mathematically that they are equally close.

FLT_MAX/FLT_MIN will often yield +INF even on many C90 compilers
because many of them support +/-INF and NANs.

Keith Thompson
Guest
Posts: n/a

 09-24-2007
The87Boy <(E-Mail Removed)> writes:
> 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?
>
> I have made this code until now:
> #include <stdio.h>
>
> int main(void) {
> int sat = 0;
> float max = 0.0, x;
> float min = plus-infinity;
> printf ("Write some numbers:\n");
>
> while( scanf("%f", &x) == 1) {
>
> if (max<x) {
> max=x;
> }
>
> if (sat == 0) {
> min = x;
> sat = 1;
> } else {
> if (min>x) {
> min = x;
> }
> }
> }
>
> printf ("%f is max\n", max);
> printf ("%f is min\n", min);
> return 0;
> }

You don't need to use infinity to determine the minimum and maximum
values of a sequence of numbers. You've already got some of the logic
for this in your use of the "sat" variable (though I don't know what
the name "sat" is supposed to mean).

You'll need to decide what the program should do if no numbers are
entered. In that case, does it make sense to say that there even is a
minimum or maximum value?

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

CBFalconer
Guest
Posts: n/a

 09-24-2007
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?

.... snip code ...

You don't need any infinities. Maintain two values, maxfound and
minfound. Get a single value from the user, and set maxfound and
minfound (both) to that value. From then on get an input value,
and if it is greater than maxfound, update maxfound. If it is less
than minfound, update minfound. Repeat until EOF on input. printf
max and minfound. Takes about 5 lines.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

The87Boy
Guest
Posts: n/a

 09-25-2007
On 25 Sep., 01:44, CBFalconer <(E-Mail Removed)> wrote:
> 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?

>
> ... snip code ...
>
> You don't need any infinities. Maintain two values, maxfound and
> minfound. Get a single value from the user, and set maxfound and
> minfound (both) to that value. From then on get an input value,
> and if it is greater than maxfound, update maxfound. If it is less
> than minfound, update minfound. Repeat until EOF on input. printf
> max and minfound. Takes about 5 lines.

How can I initialize the values then?

The87Boy
Guest
Posts: n/a

 09-25-2007
On 24 Sep., 21:21, Keith Thompson <(E-Mail Removed)> wrote:
> The87Boy <(E-Mail Removed)> writes:
> > 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?

>
> > I have made this code until now:
> > #include <stdio.h>

>
> > int main(void) {
> > int sat = 0;
> > float max = 0.0, x;
> > float min = plus-infinity;
> > printf ("Write some numbers:\n");

>
> > while( scanf("%f", &x) == 1) {

>
> > if (max<x) {
> > max=x;
> > }

>
> > if (sat == 0) {
> > min = x;
> > sat = 1;
> > } else {
> > if (min>x) {
> > min = x;
> > }
> > }
> > }

>
> > printf ("%f is max\n", max);
> > printf ("%f is min\n", min);
> > return 0;
> > }

>
> You don't need to use infinity to determine the minimum and maximum
> values of a sequence of numbers. You've already got some of the logic
> for this in your use of the "sat" variable (though I don't know what
> the name "sat" is supposed to mean).
>
> You'll need to decide what the program should do if no numbers are
> entered. In that case, does it make sense to say that there even is a
> minimum or maximum value?

Sat is just a variable to see, if it is the first time the script is
running
How can I see, if it is chars instead of numbers