# Float as Infinity

Discussion in 'C Programming' started by The87Boy, Sep 24, 2007.

1. ### The87BoyGuest

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

The87Boy, Sep 24, 2007

2. ### r6144Guest

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.

r6144, Sep 24, 2007

3. ### The87BoyGuest

On 24 Sep., 13:15, r6144 <> 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?

The87Boy, Sep 24, 2007
4. ### Tim PrinceGuest

The87Boy wrote:
> On 24 Sep., 13:15, r6144 <> 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.

Tim Prince, Sep 24, 2007
5. ### Eric SosmanGuest

The87Boy wrote:
> On 24 Sep., 13:15, r6144 <> 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
lid

Eric Sosman, Sep 24, 2007
6. ### user923005Guest

On Sep 24, 5:58 am, Tim Prince <> wrote:
> The87Boy wrote:
> > On 24 Sep., 13:15, r6144 <> 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.

user923005, Sep 24, 2007
7. ### Keith ThompsonGuest

The87Boy <> 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) <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"

Keith Thompson, Sep 24, 2007
8. ### CBFalconerGuest

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

CBFalconer, Sep 25, 2007
9. ### The87BoyGuest

On 25 Sep., 01:44, CBFalconer <> 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, Sep 25, 2007
10. ### The87BoyGuest

On 24 Sep., 21:21, Keith Thompson <> wrote:
> The87Boy <> 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

The87Boy, Sep 25, 2007
11. ### The87BoyGuest

On 24 Sep., 15:07, Eric Sosman <> wrote:
> The87Boy wrote:
> > On 24 Sep., 13:15, r6144 <> 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>.

What is the longest number, you can have in C?
And which data-type do I have to use then?

The87Boy, Sep 25, 2007
12. ### The87BoyGuest

On 24 Sep., 15:07, Eric Sosman <> wrote:
> The87Boy wrote:
> > On 24 Sep., 13:15, r6144 <> 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>.

What is the longest number, you can have in C?
And which data-type do I have to use then?

The87Boy, Sep 25, 2007
13. ### The87BoyGuest

On 24 Sep., 15:07, Eric Sosman <> wrote:
> The87Boy wrote:
> > On 24 Sep., 13:15, r6144 <> 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>.

What is the longest number, you can have in C?
And which data-type do I have to use then?

The87Boy, Sep 25, 2007
14. ### user923005Guest

On Sep 25, 12:19 am, The87Boy <> wrote:
> On 25 Sep., 01:44, CBFalconer <> 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?

double maxVal = arr[0] > arr[1] ? arr[0] : arr[1];
double minVal = arr[0] < arr[1] ? arr[0] : arr[1];

Perhaps it's time to open the C book and read it.

user923005, Sep 25, 2007
15. ### user923005Guest

On Sep 25, 1:03 am, The87Boy <> wrote:
> On 24 Sep., 15:07, Eric Sosman <> wrote:
>
>
>
>
>
> > The87Boy wrote:
> > > On 24 Sep., 13:15, r6144 <> 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>.

>
> What is the longest number, you can have in C?

There is no answer to your question. Data type double must hold at
least 1e37.

> And which data-type do I have to use then?

What are the requirements for your data type?

user923005, Sep 25, 2007
16. ### Richard HeathfieldGuest

user923005 said:

> On Sep 25, 1:03 am, The87Boy <> wrote:

<snip>

>> What is the longest number, you can have in C?

>

Of course you are right.

> Data type double must hold at least 1e37.

Oh, I think we can do better than that, though. Using C, we can calculate
1!, which isn't terribly interesting, or 2!, which is still pretty dull,
but 3! gets a little more exciting, because it's 6.

So:

3! = 6
6! = 720
720! =
2601218943565795100204903227081043611191521875016945785727541837850835631156947
38224067857795813045708261992057589224725953664156516205201587379198458774083252
91052446903888118841237643411919510455053466586162432719401971139098455367272785
37099345629855586719369774070003700430783758997420676784016967207846280629229032
10716166986726054898844551425719398549944893959449606404513236214026598619307324
93697704776060676806701764916694030348199618814556251955925669188308255149429475
96537274845624628824234526597789737740896466553992435928786212515967483220976029
50569669992728467056374713753301924831358707612541268341586012944756601145542074
95899525635430682886346310849656506827715529962567908452357025521862223581300167
00834523443236821935793184701956510729781804354173890560727428048583995919729021
72661229129842051606757903623233769945396419147517556755769539223380305682530859
99774416757843528159134613403946049012695420288383471013637338244845066600933484
84440711931292537694657354337375724772230181534032647177531984537341478674327048
45798378661870325740593892421570969599463055752106320326349320922073832092335630
99232675044017017605720260108292880423356066430898887102973807975780130560495763
42838683057190662205291174822510536697756603029574043387983471518552602805333866
35713910104633641976909739743228599421983704697910995630338960467588986579571117
65666700391567481531159439800436253993997312030664906013253113047190288984918562
03766669164468791125249193754425845895000311561682974304641142538074897281723375
95538066171980140467793561479363526626568333950976000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000

Aha! That's more of a challenge, isn't it? Right, here we go...

2601218943565795100204903227081043611191521875016945785727541837850835631156947
38224067857795813045708261992057589224725953664156516205201587379198458774083252
91052446903888118841237643411919510455053466586162432719401971139098455367272785
37099345629855586719369774070003700430783758997420676784016967207846280629229032
10716166986726054898844551425719398549944893959449606404513236214026598619307324
93697704776060676806701764916694030348199618814556251955925669188308255149429475
96537274845624628824234526597789737740896466553992435928786212515967483220976029
50569669992728467056374713753301924831358707612541268341586012944756601145542074
95899525635430682886346310849656506827715529962567908452357025521862223581300167
00834523443236821935793184701956510729781804354173890560727428048583995919729021
72661229129842051606757903623233769945396419147517556755769539223380305682530859
99774416757843528159134613403946049012695420288383471013637338244845066600933484
84440711931292537694657354337375724772230181534032647177531984537341478674327048
45798378661870325740593892421570969599463055752106320326349320922073832092335630
99232675044017017605720260108292880423356066430898887102973807975780130560495763
42838683057190662205291174822510536697756603029574043387983471518552602805333866
35713910104633641976909739743228599421983704697910995630338960467588986579571117
65666700391567481531159439800436253993997312030664906013253113047190288984918562
03766669164468791125249193754425845895000311561682974304641142538074897281723375
95538066171980140467793561479363526626568333950976000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000! =
~~^000202~~#AT!!*\$\$8NO CARRIER

Richard Heathfield, Sep 25, 2007
17. ### The87BoyGuest

On 25 Sep., 10:20, user923005 <> wrote:
> On Sep 25, 12:19 am, The87Boy <> wrote:
>
>
>
> > On 25 Sep., 01:44, CBFalconer <> 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?

>
> double maxVal = arr[0] > arr[1] ? arr[0] : arr[1];
> double minVal = arr[0] < arr[1] ? arr[0] : arr[1];
>
> Perhaps it's time to open the C book and read it.

We haven't learned about arrays in the C yet

The87Boy, Sep 25, 2007
18. ### The87BoyGuest

On 25 Sep., 10:22, user923005 <> wrote:
> On Sep 25, 1:03 am, The87Boy <> wrote:
>
>
>
> > On 24 Sep., 15:07, Eric Sosman <> wrote:

>
> > > The87Boy wrote:
> > > > On 24 Sep., 13:15, r6144 <> 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>.

>
> > What is the longest number, you can have in C?

>
> There is no answer to your question. Data type double must hold at
> least 1e37.
>
> > And which data-type do I have to use then?

>
> What are the requirements for your data type?

It should only be numbers

The87Boy, Sep 25, 2007
19. ### user923005Guest

On Sep 25, 2:20 am, The87Boy <> wrote:
> On 25 Sep., 10:22, user923005 <> wrote:
>
>
>
>
>
> > On Sep 25, 1:03 am, The87Boy <> wrote:

>
> > > On 24 Sep., 15:07, Eric Sosman <> wrote:

>
> > > > The87Boy wrote:
> > > > > On 24 Sep., 13:15, r6144 <> 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>.

>
> > > What is the longest number, you can have in C?

>
> > There is no answer to your question. Data type double must hold at
> > least 1e37.

>
> > > And which data-type do I have to use then?

>
> > What are the requirements for your data type?

>
> It should only be numbers

Then declare some double variables, and do some tests:

if (valueMin > value) /* do something */;
if (valueMax < value) /* do something */;

user923005, Sep 25, 2007
20. ### The87BoyGuest

On 25 Sep., 12:15, user923005 <> wrote:
> On Sep 25, 2:20 am, The87Boy <> wrote:
>
>
>
> > On 25 Sep., 10:22, user923005 <> wrote:

>
> > > On Sep 25, 1:03 am, The87Boy <> wrote:

>
> > > > On 24 Sep., 15:07, Eric Sosman <> wrote:

>
> > > > > The87Boy wrote:
> > > > > > On 24 Sep., 13:15, r6144 <> 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>.

>
> > > > What is the longest number, you can have in C?

>
> > > There is no answer to your question. Data type double must hold at
> > > least 1e37.

>
> > > > And which data-type do I have to use then?

>
> > > What are the requirements for your data type?

>
> > It should only be numbers

>
> Then declare some double variables, and do some tests:
>
> if (valueMin > value) /* do something */;
> if (valueMax < value) /* do something */;

How can I initialize valueMin and valueMax

The87Boy, Sep 25, 2007