473,830 Members | 2,018 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 16258
On Sep 26, 12:13 am, The87Boy <the87...@gmail .comwrote:
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?- Hide quoted text -

- Show quoted text -
Set valueMin to the largest possible value:
DBL_MAX
Set valueMax to the most negative possible value:
DBL_MIN

Then the first time you compare something, it will always get
replaced.

The start of your program will look like this:

#include <float.h>
#include <stdio.h>
#include <stdlib.h>

static char string[32767];

int main(void)
{
double valMin = DBL_MAX;
double valMax = DBL_MIN;

Sep 26 '07 #31
user923005 said:

<snip>
Set valueMax to the most negative possible value:
DBL_MIN
DBL_MIN is required by the Standard to be positive.

--
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 26 '07 #32
user923005 wrote On 09/26/07 05:10,:
[...]
Set valueMin to the largest possible value:
DBL_MAX
Set valueMax to the most negative possible value:
DBL_MIN
Bzzzt! DBL_MIN is a *positive* value. Use
`-DBL_MAX' or `-HUGE_VAL' or (on systems that have
it) `-INFINITY'.

--
Er*********@sun .com
Sep 26 '07 #33
"user923005 " <dc*****@connx. coma écrit dans le message de news:
11************* ********@22g200 0h...legrou ps.com...
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;
}
There seems to be a confusion between the semantics of DBL_MIN and INT_MIN.
INT_MIN is the negative int with the largest absolute value
(typically -32768 or -2147483648).
DBL_MIN is the smallest non-zero positive value that can be represented in a
double (typically 2.2250738585072 014E-308).
The equivalent of INT_MIN for a double is -DBL_MAX, not DBL_MIN.

--
Chqrlie.
Sep 26 '07 #34
Barry Schwarz wrote On 09/25/07 21:37,:
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.
For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
are all *positive* values.

The mistake is made so frequently that it must be
easy to make, so let's not make it any easier, okay?

--
Er*********@sun .com
Sep 26 '07 #35
"Eric Sosman" <Er*********@su n.coma écrit dans le message de news:
1190818696.4159 40@news1nwk...
Barry Schwarz wrote On 09/25/07 21:37,:
>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.

For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
are all *positive* values.

The mistake is made so frequently that it must be
easy to make, so let's not make it any easier, okay?
The mistake is made so easily and so frequently that it almost qualifies as
a defect ;-)
The names are just *so* confusing !

--
chqrlie.
Sep 27 '07 #36
Charlie Gordon wrote On 09/27/07 05:36,:
"Eric Sosman" <Er*********@su n.coma écrit dans le message de news:
1190818696.4159 40@news1nwk...
>>
For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
are all *positive* values.

The mistake is made so frequently that it must be
easy to make, so let's not make it any easier, okay?

The mistake is made so easily and so frequently that it almost qualifies as
a defect ;-)
The names are just *so* confusing !
They are; something like DBL_SMALLEST might have
avoided confusion. (I'm not heaping scorn on the ANSI
committee for the unfortunate names; I think in this
case the committee just codified "prior art.") But
for now and probably for the life of C we're stuck with
the _MIN names, and must learn to live with them.

--
Er*********@sun .com

Sep 27 '07 #37
On Sep 27, 7:16 am, Eric Sosman <Eric.Sos...@su n.comwrote:
Charlie Gordon wrote On 09/27/07 05:36,:
"Eric Sosman" <Eric.Sos...@su n.coma écrit dans le message de news:
1190818696.4159 40@news1nwk...
For the UINTMAX_MAX'th time, {FLT,DBL,LDBL}_ MIN
are all *positive* values.
The mistake is made so frequently that it must be
easy to make, so let's not make it any easier, okay?
The mistake is made so easily and so frequently that it almost qualifies as
a defect ;-)
The names are just *so* confusing !

They are; something like DBL_SMALLEST might have
avoided confusion. (I'm not heaping scorn on the ANSI
committee for the unfortunate names; I think in this
case the committee just codified "prior art.") But
for now and probably for the life of C we're stuck with
the _MIN names, and must learn to live with them.
Believe it or not, I know better.

Anyway, here is my eventual solution (I sent a copy in to Snippets):

#include <stdlib.h>
/*
"Introducti on to Algorithms"
Cormen, Leiserson, Rivest
pp. 186,187
ISBN: 0-07-013143-0

Simultaneous min & max
using only 3*N/2 comparisons

Written by Dann Corbit
9/25/2007
Donated to the public domain
*/
#ifdef e_type_LONG_DOU BLE
typedef long double e_type;
#elif defined(e_type_ DOUBLE)
typedef double e_type;
#elif defined(e_type_ FLOAT)
typedef float e_type;
#elif defined(e_type_ UNSIGNED_LONG_L ONG)
typedef unsigned long long e_type;
#elif defined(e_type_ LONG_LONG)
typedef long long e_type;
#elif defined(e_type_ UNSIGNED_LONG)
typedef unsigned long e_type;
#elif defined(e_type_ LONG)
typedef long e_type;
#elif defined(e_type_ UNSIGNED)
typedef unsigned e_type;
#elif defined(e_type_ INT)
typedef int e_type;
#elif defined(e_type_ SHORT)
typedef short e_type;
#elif defined(e_type_ UNSIGNED_SHORT)
typedef unsigned e_type;
#elif defined(e_type_ CHAR)
typedef char e_type;
#elif defined(e_type_ UNSIGNED_CHAR)
typedef unsigned char e_type;
#elif defined (__cplusplus)
template < class e_type // works with stl string class etc...
#endif
void minmax(
e_type * a, // input array
size_t arr_size, // array length
e_type * min_e, // smallest thing found
e_type * max_e // biggest thing found
)
{
e_type min_et;
e_type max_et;
size_t i,
n;
if (arr_size % 2) {
min_et = a[0];
max_et = a[0];
n = 1;
} else {
if (a[0] a[1]) {
max_et = a[0];
min_et = a[1];
} else {
min_et = a[0];
max_et = a[1];
}
n = 2;
}
for (i = n; i < arr_size; i += 2) {

if (a[i] a[i + 1]) {
max_et = max_et a[i] ? max_et : a[i];
min_et = min_et < a[i + 1] ? min_et : a[i + 1];
} else {
max_et = max_et a[i + 1] ? max_et : a[i + 1];
min_et = min_et < a[i] ? min_et : a[i];
}
}
*min_e = min_et;
*max_e = max_et;
}

#if defined( UNIT_TEST ) && (defined (e_type_DOUBLE) ||
defined( __cplusplus))
#include <stdio.h>

char string[32767];
double foo[32767];
int main(void)
{
size_t i = 0;
double dmin,
dmax;
while (fgets(string, sizeof string, stdin)) {
foo[i++] = atof(string);
if (i 32766)
break;
}
minmax(foo, i, &dmin, &dmax);
printf("min=%f, max=%f\n", dmin, dmax);
return 0;
}
#endif
Sep 27 '07 #38

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
32335
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
7618
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?
0
9781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9641
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
10477
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
10197
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
9310
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
5615
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
5777
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3956
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3072
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.