473,830 Members | 2,156 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 16259
The87Boy wrote On 09/25/07 03:25,:
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?
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."

--
Er*********@sun .com
Sep 25 '07 #21
On Sep 25, 3:27 am, The87Boy <the87...@gmail .comwrote:
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
With an assignment.

Sep 25 '07 #22
user923005 said:
On Sep 25, 3:27 am, The87Boy <the87...@gmail .comwrote:
<snipped 50-line quote for 1-line reply>
>>
How can I initialize valueMin and valueMax

With an assignment.
Context is always good, but in moderation.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 25 '07 #23
On Tue, 25 Sep 2007 03:27:07 -0700, in comp.lang.c , The87Boy
<th******@gmail .comwrote:
>
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
Sep 25 '07 #24
The87Boy wrote:
>
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.

--
pete
Sep 25 '07 #25
On Sep 25, 3:31 pm, pete <pfil...@mindsp ring.comwrote:
The87Boy wrote:
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.
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.hfor 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;
}


Sep 26 '07 #26
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?
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
Sep 26 '07 #27
On Tue, 25 Sep 2007 00:19:19 -0700, The87Boy <th******@gmail .com>
wrote:
>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?
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
Sep 26 '07 #28
Barry Schwarz <sc******@doezl .netwrites:
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?

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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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"
Sep 26 '07 #29
On 25 Sep., 20:58, Mark McIntyre <markmcint...@s pamcop.netwrote :
On Tue, 25 Sep 2007 03:27:07 -0700, in comp.lang.c , The87Boy

<the87...@gmail .comwrote:
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.
It isn't the question, but should I set it to "float
valueMin=-999999999999999 99999999" or?

Sep 26 '07 #30

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
12315
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?
1
10523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10199
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
9312
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...
1
7741
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6948
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
5616
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
5778
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3956
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.