Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: Jim Paradis <jrp@jrp.tiac.net>
Subject: Re: Another Floating Point Weirdness
Date: Thu, 12 Feb 1998 05:01:34 +0000	[thread overview]
Message-ID: <24e8030bb296cc275117eb3d93aff579.axp-list@archive.invalid> (raw)

Francesco Callari wrote:
> 
> Apropos of FP weirdness.
> Can someone tell me why the following program will get
> a floating point exception (SIGFPE) ?
> 
> #include <stdio.h>
> #include <math.h>
> #include <assert.h>
> 
> int main(int argc, char* argv[])
> {
>     int i, j;
>     double v,m; /* works fine with m=0.0 */
> 
>     assert(finite(m));  /* passes this test */
> 
>     for (j=-10; j<=0; j++) {
>         v=exp(-(double)j);
>         if (v > m ) { /* dies with SIGFPE here */
>             m=v;
>         }
>     }
> }

Since "m" is uninitialized, it's anybody's guess as to what its initial
value actually is.  In most cases, it will be some stack pointer
address.
This gets interpreted as an IEEE *denormal* number.  Since this is not
an "infinity", it passes the "finite" test.

Note that if you build the program with "-mieee", you avoid the SIGFPE.
But in general, uninitialized variables are a bad idea.

-- 
Jim Paradis   jrp@jrp.tiac.net            "The early bird gets the worm.
http://www.tiac.net/users/jrp/index.html   If you want something else
for
                                           breakfast, get up later."

-- 
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject.  Do not send it to axp-list@redhat.com

             reply	other threads:[~1998-02-12  5:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-02-12  5:01 Jim Paradis [this message]
  -- strict thread matches above, loose matches on Subject: below --
1998-02-12  2:17 Another Floating Point Weirdness Francesco Callari
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox