473,830 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Float as Infinity

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

Sep 24 '07
37 16260
On 24 Sep., 15:07, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
The87Boy wrote:
On 24 Sep., 13:15, r6144 <rainy6...@gmai l.comwrote:
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?

Sep 25 '07 #11
On 24 Sep., 15:07, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
The87Boy wrote:
On 24 Sep., 13:15, r6144 <rainy6...@gmai l.comwrote:
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?

Sep 25 '07 #12
On 24 Sep., 15:07, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
The87Boy wrote:
On 24 Sep., 13:15, r6144 <rainy6...@gmai l.comwrote:
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?

Sep 25 '07 #13
On Sep 25, 12:19 am, The87Boy <the87...@gmail .comwrote:
On 25 Sep., 01:44, CBFalconer <cbfalco...@yah oo.comwrote:


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.

Sep 25 '07 #14
On Sep 25, 1:03 am, The87Boy <the87...@gmail .comwrote:
On 24 Sep., 15:07, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:


The87Boy wrote:
On 24 Sep., 13:15, r6144 <rainy6...@gmai l.comwrote:
>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?

Sep 25 '07 #15
user923005 said:
On Sep 25, 1:03 am, The87Boy <the87...@gmail .comwrote:
<snip>
>What is the longest number, you can have in C?

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! =
260121894356579 510020490322708 104361119152187 501694578572754 183785083563115 6947
382240678577958 130457082619920 575892247259536 641565162052015 873791984587740 83252
910524469038881 188412376434119 195104550534665 861624327194019 711390984553672 72785
370993456298555 867193697740700 037004307837589 974206767840169 672078462806292 29032
107161669867260 548988445514257 193985499448939 594496064045132 362140265986193 07324
936977047760606 768067017649166 940303481996188 145562519559256 691883082551494 29475
965372748456246 288242345265977 897377408964665 539924359287862 125159674832209 76029
505696699927284 670563747137533 019248313587076 125412683415860 129447566011455 42074
958995256354306 828863463108496 565068277155299 625679084523570 255218622235813 00167
008345234432368 219357931847019 565107297818043 541738905607274 280485839959197 29021
726612291298420 516067579036232 337699453964191 475175567557695 392233803056825 30859
997744167578435 281591346134039 460490126954202 883834710136373 382448450666009 33484
844407119312925 376946573543373 757247722301815 340326471775319 845373414786743 27048
457983786618703 257405938924215 709695994630557 521063203263493 209220738320923 35630
992326750440170 176057202601082 928804233560664 308988871029738 079757801305604 95763
428386830571906 622052911748225 105366977566030 295740433879834 715185526028053 33866
357139101046336 419769097397432 285994219837046 979109956303389 604675889865795 71117
656667003915674 815311594398004 362539939973120 306649060132531 130471902889849 18562
037666691644687 911252491937544 258458950003115 616829743046411 425380748972817 23375
955380661719801 404677935614793 635266265683339 509760000000000 000000000000000 00000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 00000
000000000000000 000000000000000 000000000000000 000000000000000 00000000

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

260121894356579 510020490322708 104361119152187 501694578572754 183785083563115 6947
382240678577958 130457082619920 575892247259536 641565162052015 873791984587740 83252
910524469038881 188412376434119 195104550534665 861624327194019 711390984553672 72785
370993456298555 867193697740700 037004307837589 974206767840169 672078462806292 29032
107161669867260 548988445514257 193985499448939 594496064045132 362140265986193 07324
936977047760606 768067017649166 940303481996188 145562519559256 691883082551494 29475
965372748456246 288242345265977 897377408964665 539924359287862 125159674832209 76029
505696699927284 670563747137533 019248313587076 125412683415860 129447566011455 42074
958995256354306 828863463108496 565068277155299 625679084523570 255218622235813 00167
008345234432368 219357931847019 565107297818043 541738905607274 280485839959197 29021
726612291298420 516067579036232 337699453964191 475175567557695 392233803056825 30859
997744167578435 281591346134039 460490126954202 883834710136373 382448450666009 33484
844407119312925 376946573543373 757247722301815 340326471775319 845373414786743 27048
457983786618703 257405938924215 709695994630557 521063203263493 209220738320923 35630
992326750440170 176057202601082 928804233560664 308988871029738 079757801305604 95763
428386830571906 622052911748225 105366977566030 295740433879834 715185526028053 33866
357139101046336 419769097397432 285994219837046 979109956303389 604675889865795 71117
656667003915674 815311594398004 362539939973120 306649060132531 130471902889849 18562
037666691644687 911252491937544 258458950003115 616829743046411 425380748972817 23375
955380661719801 404677935614793 635266265683339 509760000000000 000000000000000 00000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 00000
000000000000000 000000000000000 000000000000000 000000000000000 00000000! =
~~^000202~~#AT! !*$$8NO CARRIER

Sep 25 '07 #16
On 25 Sep., 10:20, user923005 <dcor...@connx. comwrote:
On Sep 25, 12:19 am, The87Boy <the87...@gmail .comwrote:
On 25 Sep., 01:44, CBFalconer <cbfalco...@yah oo.comwrote:
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

Sep 25 '07 #17
On 25 Sep., 10:22, user923005 <dcor...@connx. comwrote:
On Sep 25, 1:03 am, The87Boy <the87...@gmail .comwrote:
On 24 Sep., 15:07, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
The87Boy wrote:
On 24 Sep., 13:15, r6144 <rainy6...@gmai l.comwrote:
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

Sep 25 '07 #18
On Sep 25, 2:20 am, The87Boy <the87...@gmail .comwrote:
On 25 Sep., 10:22, user923005 <dcor...@connx. comwrote:


On Sep 25, 1:03 am, The87Boy <the87...@gmail .comwrote:
On 24 Sep., 15:07, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
The87Boy wrote:
On 24 Sep., 13:15, r6144 <rainy6...@gmai l.comwrote:
>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 */;
Sep 25 '07 #19
On 25 Sep., 12:15, user923005 <dcor...@connx. comwrote:
On Sep 25, 2:20 am, The87Boy <the87...@gmail .comwrote:
On 25 Sep., 10:22, user923005 <dcor...@connx. comwrote:
On Sep 25, 1:03 am, The87Boy <the87...@gmail .comwrote:
On 24 Sep., 15:07, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
The87Boy wrote:
On 24 Sep., 13:15, r6144 <rainy6...@gmai l.comwrote:
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

Sep 25 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
68764
by: Andreas Neudecker | last post by:
Hi. Is there anything like +infinity and -infinity available in Python, and can it be used in comparisons together with int or float numbers? Regards Andreas
9
9810
by: franzkowiak | last post by:
Hello, I've read some bytes from a file and just now I can't interpret 4 bytes in this dates like a real value. An extract from my program def l32(c): return ord(c) + (ord(c)<<8) + (ord(c)<<16) + (ord(c)<<24)
2
32337
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
6
7619
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
8
12316
by: better_cs_now | last post by:
Hello all, I'd like to get the negative value of largest possible magnitude for a float. I've considered: -numeric_limits<float>::max() and -numeric_limits<float>::infinity()
8
9480
by: bearophileHUGS | last post by:
sys.maxint gives the largest positive integer supported by Python's regular integer type. But maybe such attribute, with few others (they can be called min and max) can be given to int type itself. D is a very nice language, that I hope to see more used. It is copying lot of things from Python. D Floating point values have some proprieties: http://www.digitalmars.com/d/property.html Properties for Floating Point Types:
5
4202
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent accident" etc...) The issue appears possibly to point to a bug in the Python compiler, with it producing inconsistent results. I'm using "Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32". This code sometimes produces a float of 1.0, sometimes...
1
7663
by: Mark P | last post by:
I have a float value which should be initialized to the minimum allowed value (analogous to -infinity). I discovered today (thank you, unit tests!) that numeric_limits<float>::min() returns a very small number greater than 0, rather than the extremely negative number which I want. Is it safe to use -numeric_limits<float>::max() for this purpose, or is something else preferred? Thanks, Mark
14
5558
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10487
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10202
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9313
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6950
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3958
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.