[comp.lang.c] Help in dealing with floating point exceptions in C on UNIX/VAX

kgodfrey@prophet.bbn.com (02/13/87)

I'm new to C, and I'm trying to get some C code from an outside vendor to
run on a VAX running Berkeley 4.3 UNIX.  The problem I'm having is that
I can't figure out how to trap floating point exceptions.  I've tried
the infnan routine mentioned in the 4.3 manual, but I'm obviously doing
something wrong.  It seems to me there must be some reasonable way to
deal with this problem, but I've exhausted the expertise in my immediate
vicinity.  Can someone help me rescue myself from core dumps?

What I'd like to do is have the procedure propagate REALLY_BIG_NUMBER
when it gets an arithmetic overflow, instead of hitting infinity and
core dumping.

Thanks for any help/advice/leads.

Kathy Godfrey  
kgodfrey@prophet.bbn.com

moss@BRL.ARPA (02/13/87)

See matherr(3M).

-moss

tps@sdchem.UUCP (02/15/87)

In article <4441@brl-adm.ARPA> moss@BRL.ARPA (Gary S. Moss (SLCBR-VLD-V)) writes:
>See matherr(3M).
>-moss

What system are you on?
I can't find this in either my 4.3BSD or Sys V Manual.


|| Tom Stockfisch, UCSD Chemistry	tps%chem@sdcsvax.UCSD

reschly@BRL.ARPA (02/16/87)

      Tom,

   A man page with the name matherr shows up on both a Gould 6080 running UTX
2.0 (very close to 4.3BSD), and on a VAX 11/780 running 4.3BSD when using
Doug Gwyn's SysV under 4.n emulation.  Note that on the VAX it does not show
up in the 4.3BSD man pages; just the SysV pages.  I think (but am not sure
since I am not a regular user of SysV) that Doug's stuff is currently at
SysVR2.

   Essentially this man page explains a "hook" into the math library.  The
function matherr(x) is invoked by the math library when errors occur, giving
the user the opportunity to determine what gets returned to the program, and
whether to continue execution or to abort.

				Later,
				    Bob

guy@gorodish.UUCP (02/16/87)

>>See matherr(3M).
>
>What system are you on?
>I can't find this in either my 4.3BSD or Sys V Manual.

It is in the VAX S5R2 manual, at least; "matherr" is a System V-ism.

pdg@ihdev.UUCP (02/16/87)

In article <635@sdchema.sdchem.UUCP> tps@sdchemf.UUCP (Tom Stockfisch) writes:
>In article <4441@brl-adm.ARPA> moss@BRL.ARPA (Gary S. Moss (SLCBR-VLD-V)) writes:
>>See matherr(3M).
>What system are you on?
>I can't find this in either my 4.3BSD or Sys V Manual.


Look again in your system V manual.  It is in each of my Sys5r2 and r3
manuals.  It is not in either BSD or V7.

-- 

Paul Guthrie
ihnp4!ihdev!pdg			This Brain left intentionally blank.

moss@BRL.ARPA (02/17/87)

Matherr is in section 3M of the following:

"User's Manual" - System V Release 1, 301-905, Issue 1, January 1983
"Programmer Reference Manual" - System V Release 2, 307-113, Issue 2, April 1984

It is also on-line on Gould UTX/32 2.0 (which fooled me into thinking that it was
available on Berkeley systems; it is not available on vanilla BSD UNIX).

-moss

ag0@k.cc.purdue.edu.UUCP (02/18/87)

In article <635@sdchema.sdchem.UUCP> tps@sdchemf.UUCP (Tom Stockfisch) writes:
>In article <4441@brl-adm.ARPA> moss@BRL.ARPA (Gary S. Moss (SLCBR-VLD-V)) writes:
>>See matherr(3M).
>>-moss
>
>What system are you on?
>I can't find this in either my 4.3BSD or Sys V Manual.
>
>|| Tom Stockfisch, UCSD Chemistry	tps%chem@sdcsvax.UCSD

I can't find on my 4.3 system either.  I would suggest using the signal
handler.  You can write a function to be called when a floating point
exception occurs and handle it there.

#include <signal.h>

int	trapfpe();

main {
    signal (SIGFPE, trapfpe);
    .
    .
    }  /* main */

trapfpe()
{
    printf ("trapfpe: ERROR- Floating point exeption\n");
    /****** Handle the error *******/
    }  /* trapfpe */

My usage of the signal facility is a little bit different from the manual
description (which may be confusing to new programmers), but it is simple 
and it works.

			Colin

dgh@dgh.UUCP (02/18/87)

>The problem I'm having is that
>I can't figure out how to trap floating point exceptions.

The consensus response, at least from System V users, is to use matherr(3M).
The original poster hasn't said whether that was any help, and it's 
instructive to realize why it may not be.  Matherr is only counted on
for dealing with exceptions that arise in certain libm subroutines.
On most System V implementations matherr is no help for the much more 
common cases of floating-point exceptions,
such as overflow and division by zero, that arise in atomic operations
such as +-*/.  These operations are usually implemented in hardware nowadays, 
except on PC's.

Users of 68020/68881 systems have a complementary problem:  they would
sacrifice considerable performance to implement matherr's requirements
because the 68881 performs many libm subroutines in a single instruction.

Correctly implemented IEEE arithmetic offers a uniform solution since 
all atomic operations must signal exceptions if they encounter them,
and it is usually not too difficult to make library functions look like
atomic operations if they are not so provided in the hardware.  VAX
reserved operands can be exploited to provide some of the exception-handling
capabilities of IEEE arithmetic; I have heard that 4.4 BSD may actually
have some software support for that.  And IEEE and VAX arithmetic are the
only floating-point architectures of consequence in the workstation market,
at least.  

Since the original poster referred to C code, 4.3 BSD, and a VAX, 
it seems likely that the solution to her problem can be developed by consulting
sigvec(2), signal(3),
or /usr/include/signal.h, to see how to declare a signal handler for SIGFPE 
and enable it at the start of main().


David Hough

ARPA: dhough@sun.com
UUCP: {ucbvax,decvax,allegra,decwrl,cbosgd,ihnp4,seismo}!sun!dhough

meissner@dg_rtp.UUCP (02/25/87)

In article <635@sdchema.sdchem.UUCP> tps@sdchemf.UUCP (Tom Stockfisch) writes:
> In article <4441@brl-adm.ARPA> moss@BRL.ARPA (Gary S. Moss (SLCBR-VLD-V)) writes:
> >See matherr(3M).
> 
> What system are you on?
> I can't find this in either my 4.3BSD or Sys V Manual.

Matherr was added in System V.2, and is in the libm section of the manual.

-- 
	Michael Meissner, Data General	Uucp: ...mcnc!rti-sel!dg_rtp!meissner

It is 11pm, do you know what your sendmail and uucico are doing?