Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Mar 1999 09:48:40 -0800 (PST)
From:      John Polstra <jdp@polstra.com>
To:        cjclark@home.com
Cc:        hackers@freebsd.org
Subject:   Re: FP Math Problem
Message-ID:  <199903111748.JAA57916@vashon.polstra.com>
In-Reply-To: <199903042248.RAA08114@cc942873-a.ewndsr1.nj.home.com>

next in thread | previous in thread | raw e-mail | index | archive | help
In article <199903042248.RAA08114@cc942873-a.ewndsr1.nj.home.com>,
Crist J. Clark <cjclark@home.com> wrote:
> I am trying to figure out why the following program produces a
> floating point exception and core dumps.
> 
> #include <stdio.h>
> #include <floatingpoint.h>
> #include <float.h>
> 
> int main()
> {
> 
>   double a = 1e200;
>   double b;
> 
>   printf("fpgetmask:    %o\n",fpgetmask());
> 
>   printf("a:            %e\n",a);
> 
>   b = a*a;
> 
>   printf("isinf:        %d\n",isinf(b));
> 
>   printf("isnan:        %d\n",isnan(b));
> 
>   printf("b > DBL_MAX:  %d\n",b>DBL_MAX);
> 
>   printf("b:         %e\n",b);
> 
>   return 0;
> }
> 
> My output is,
> 
> [112:~/tmp] ./dtest 
> fpgetmask:    15
> a:            1.000000e+200
> isinf:        0
> isnan:        0
> Floating exception (core dumped)
> 
> I started with this on -questions and (although the thread got
> sidetracked by a bad test program I presented at the beginning) other
> FreeBSDers have not reproduced the error, but get what I would expect,
> 
> % ./dtest 
> fpgetmask:    15
> a:            1.000000e+200
> isinf:        1
> isnan:        0
> b > DBL_MAX:  1
> b:            inf
> 
> However, I get the FPE and core dump on my three FreeBSD machines
> (different hardware on each).

I played around with this a little bit on a Pentium-II.  I also got
the floating exception.  There _should_ be a floating exception, but
it should happen at the "b = a*a" statement, I believe.  If you add
a second "b = a*a" after it, then you do get the FPE.

As the code is written, for some reason nothing is stored into b at
all.  That's why some people get the core dump later on and others
don't.  (It might also depend on the specific FP hardware.)  You can
confirm this by initializing b to 1.0.

Bruce Evans <bde@freebsd.org> could probably explain this.  You
might wish to ask him about it.

John
-- 
  John Polstra                                               jdp@polstra.com
  John D. Polstra & Co., Inc.                        Seattle, Washington USA
  "Nobody ever went broke underestimating the taste of the American public."
                                                            -- H. L. Mencken


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199903111748.JAA57916>