Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* Another Floating Point Weirdness
@ 1998-02-12  2:17 Francesco Callari
  0 siblings, 0 replies; 2+ messages in thread
From: Francesco Callari @ 1998-02-12  2:17 UTC (permalink / raw)


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

System config is
	kernel 2.0.32
	libm-2.0.6.so
	libc-2.0.6.so
	cpu : Alpha
	cpu model : EV56
	PC164LX

Ciao
Franco

-------------------------------------------------------------------------
Franco Callari                          
Centre for Intelligent Machines       "Beati coloro che hanno fame e sete
McGill University, 3480 University     di giustizia, perche` saranno
Montreal, Quebec, Canada, H3A 2A7      giustiziati."      (P. Bellocchio)
                                    
franco@cim.mcgill.ca | phone: +(514) 398-3371 | fax: +(514) 398-7348
WWW: <a href="http://www.cim.mcgill.ca/~franco/Home.html> Franco </a>
-------------------------------------------------------------------------

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Another Floating Point Weirdness
@ 1998-02-12  5:01 Jim Paradis
  0 siblings, 0 replies; 2+ messages in thread
From: Jim Paradis @ 1998-02-12  5:01 UTC (permalink / 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1998-02-12  5:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-02-12  5:01 Another Floating Point Weirdness Jim Paradis
  -- strict thread matches above, loose matches on Subject: below --
1998-02-12  2:17 Francesco Callari

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