Specifying "Infinity"
Author Message
Specifying "Infinity"

I am working on an application that requires me to set certain
elements in an array to negative infinity.  Is there an easy
way to do this, or do I have to use the maximum-magnitude
negative number allowable for a given data type?

If it helps at all, the data type in question is "double."

--
Chris Lanciani
UF '91

Fri, 24 Jan 1997 05:47:37 GMT
Specifying "Infinity"

Quote:

>I am working on an application that requires me to set certain
>elements in an array to negative infinity.  Is there an easy
>way to do this, or do I have to use the maximum-magnitude
>negative number allowable for a given data type?

>If it helps at all, the data type in question is "double."

>--
>Chris Lanciani
>UF '91

Unless you're willing to assign some special (out of range) value as
an infinity flag, then negative machine infinity is the only real choice.
Will having infinity set to a real number cause computational problems?
That is the question you have to answer before you decide whether
an acceptable infinity is a special case (flag)  or simply an approximate
value (machine infinity).

Sat, 25 Jan 1997 10:05:09 GMT
Specifying "Infinity"

Quote:
>I am working on an application that requires me to set certain
>elements in an array to negative infinity.  Is there an easy
>way to do this, or do I have to use the maximum-magnitude
>negative number allowable for a given data type?

If you want to do this, be aware that not every implementation
supports infinites. But if you don't need portability to non-IEEE 754
platforms, you can try this trick:

l3apollo6:~/tmp 93> cat test.c
#include <stdio.h>
#include <signal.h>

main(void)
{
double x;

signal(SIGFPE, SIG_IGN);
x = -1.0/0;
printf("%f\n",x);
return 0;

Quote:
}

l3apollo6:~/tmp 94> cc test.c
******** Line 9 of "test.c": [Warning #300]  Expression will fault at runtime -- divide
by zero.
l3apollo6:~/tmp 95> ./a.out
-inf

Warning: This code actually invokes undefined behavior, so use it at
your own risk and don't tell anybody that I taught you that :-)

Including <float.h> and using -DBL_MAX (for a double) is a much safer
approach, it's portable and it doesn't invoke undefined behavior, as
long as you don't try to perform any arithmetic with these values.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland

Sun, 26 Jan 1997 06:30:36 GMT
Specifying "Infinity"

Quote:

> The most portable way of doing this would be to make all your array
> elements structures:
> struct could_be_inf {
>    double val;
>    char inf_flag;
> }

Ugh!  You lose at least one byte per array element, and on most UNIX
platforms, probably 8 bytes because of padding.

It's better (IMHO) to do something like this (include relevant headers:)

double pos_inf = MAX_DBL;
double neg_inf = -MAX_DBL;

You can safely assign these:
a[x][y] = pos_inf;

And can even compare them like this:

if (a[x][y] == pos_inf) do_some_stuff();

with the caveat that:

if any array element needs to be set to pos or neg infinity, make
sure it gets that way with the appropriate assignment.  That
way, you can be confident that the comparison will always work.

This uses no extra storage, and you lose only two values in the entire
range of double (not much of a cost).

--
David F. Skoll
<a HREF="http://www.doe.carleton.ca/students/skoll/">

Sun, 26 Jan 1997 09:04:30 GMT
Specifying "Infinity"

Quote:
>> Very good.  However, I still maintain that my method is the most portable.
>Mine is equally portable.  It's strictly ANSI C, so I don't know
>what you mean by "most portable."

What was the macro name you used for your infinity values again?  What header
files are they found in?
--
Wayne Berke

Mon, 27 Jan 1997 02:16:01 GMT
Specifying "Infinity"

Quote:

>> The most portable way of doing this would be to make all your array
>> elements structures:
>> struct could_be_inf {
>>        double val;
>>        char inf_flag;
>> }
>Ugh!  You lose at least one byte per array element, and on most UNIX
>platforms, probably 8 bytes because of padding.

Shouldn't that be 4 bytes padding?

In a world of megabyte RAM and virtual memory the extra storage requirements
might not make a difference.  Moreover, the original poster did not indicate
that he had a _huge_ array.

Quote:
>It's better (IMHO) to do something like this (include relevant headers:)
>double pos_inf = MAX_DBL;
>double neg_inf = -MAX_DBL;

[ ... implementation deleted ...]

Quote:
>This uses no extra storage, and you lose only two values in the entire
>range of double (not much of a cost).

Very good.  However, I still maintain that my method is the most portable.

Quote:
>--
>David F. Skoll
><a HREF="http://www.doe.carleton.ca/students/skoll/">

--
Wayne Berke

Sun, 26 Jan 1997 23:03:07 GMT
Specifying "Infinity"

Quote:
>I am working on an application that requires me to set certain
>elements in an array to negative infinity.  Is there an easy
>way to do this, or do I have to use the maximum-magnitude
>negative number allowable for a given data type?
>If it helps at all, the data type in question is "double."

The most portable way of doing this would be to make all your array
elements structures:

struct could_be_inf {
double val;
char inf_flag;

Quote:
}

You can then have the convention:

inf_flag  == 0  ==> array value in "val"
< 0  ==> array value is negative infinity
> 0  ==> array value is positive infinity (a bonus (-:)

Of course, if you want to do some special arithmetic on these infinities,
you'll have to write your own subroutines, but you'd probably want to do
that anyway.

Wayne

Sun, 26 Jan 1997 08:16:14 GMT
Specifying "Infinity"

Quote:
>I am working on an application that requires me to set certain
>elements in an array to negative infinity.  Is there an easy
>way to do this, or do I have to use the maximum-magnitude
>negative number allowable for a given data type?

If your platform uses IEEE floating-point, it should provide functions
that create the necessary 'special values'.  On the Sun, for example,
there's a NaN(x) macro to assign a NaN to x, and the __infinity()
function to provide an infinity.  Short of that, you can get a copy
of the IEEE spec to see what the hex value of infinity is, and use
a union to create one.

--
#include        <standard.disclaimer>
_
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up

Sun, 26 Jan 1997 23:58:14 GMT
Specifying "Infinity"

Quote:

> DBL_MAX, found in the ANSI standard header file <float.h>.

Oops.  I see that in my original response, I wrote MAX_DBL.  The correct
macro is in fact DBL_MAX.

--
David F. Skoll
<a HREF="http://www.doe.carleton.ca/students/skoll/">

Mon, 27 Jan 1997 02:28:32 GMT
Specifying "Infinity"

Quote:

>> I have neither float.h nor any definition for DBL_MAX on my system
>> (Sun SS10 with bundled compiler).

>bundled header files are completely whacked.  I run on a SS10 too, but
>I use gcc and the glibc library, which has decent headers.

_it_ requires an ANSI compiler, does it?  Then I'm really screwed.  :)
--
Wayne Berke

Mon, 27 Jan 1997 04:13:07 GMT
Specifying "Infinity"

[Wayne]

Quote:
> >> The most portable way of doing this would be to make all your array
> >> elements structures:
> >> struct could_be_inf {
> >>   double val;
> >>   char inf_flag;
> >> }

[David]

Quote:
> >Ugh!  You lose at least one byte per array element, and on most UNIX
> >platforms, probably 8 bytes because of padding.

[Wayne]

Quote:
> Shouldn't that be 4 bytes padding?

Nope, 8.  Sparc 10's, for example, like to have doubles aligned on 8-byte
boundaries.

Quote:
> In a world of megabyte RAM and virtual memory the extra storage requirements
> might not make a difference.  Moreover, the original poster did not indicate
> that he had a _huge_ array.

Maybe, maybe not.  I did some array manipulation, and it's amazing how
quickly arrays eat up memory, even for average-size problems.  Example:
I was curve-fitting 256 basis functions to 15000 datapoints, with 4
independent variables.  That's a small problem in my line, but the
matrices were huge!  I had one matrix that was 50MB alone.

Also, the "world of megabyte RAM" etc. attitude is what's responsible
for a Sparc 10 having the same perceived response time as my trusty
TRS-80 Color Computer from 1980. :-)

Quote:
> Very good.  However, I still maintain that my method is the most portable.

Mine is equally portable.  It's strictly ANSI C, so I don't know
what you mean by "most portable."

--
David F. Skoll
<a HREF="http://www.doe.carleton.ca/students/skoll/">

Mon, 27 Jan 1997 01:23:42 GMT
Specifying "Infinity"

Quote:

> What was the macro name you used for your infinity values again?  What header
> files are they found in?

DBL_MAX, found in the ANSI standard header file <float.h>.

:-)

--
David F. Skoll
<a HREF="http://www.doe.carleton.ca/students/skoll/">