Float as Infinity

T

The87Boy

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

r6144

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.
 
T

The87Boy

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

Tim Prince

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

Eric Sosman

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

user923005

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.
 
K

Keith Thompson

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?

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

CBFalconer

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?

.... 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.
 
T

The87Boy

... 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?
 
T

The87Boy

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
 
T

The87Boy

(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?
 
T

The87Boy

(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?
 
T

The87Boy

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

user923005

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.
 
R

Richard Heathfield

user923005 said:
There is no answer to your question.

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
 
T

The87Boy

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
 
U

user923005

It should only be numbers

Then declare some double variables, and do some tests:

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

The87Boy

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
 

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