MATHTRAP(3M) — NEWS-OS Programmer’s Manual
NAME
mathtrap − enable/disable floating-point exception trap
SYNOPSIS
#include <math.h>
int mathtrap(flags)
int flags;
DESCRIPTION
This function sets a flag which enable or disable Floating-point exception traps. Some constants defined in <math.h> or bitwide logical OR of them are used as Flags. mathtrap returns previous value of flags.
The names of constants and exceptions are:
Exception <math.h>
Branch/Set on Unordered FPET_BSUNO
Signaling Not a Number FPET_SGNAN
Operand Error FPET_OPERR
Overflow FPET_OVFLW
Underflow FPET_UNDFL
Divide by Zero FPET_DIVZR
Inexact FPET_INEXA
For example, to enable “Operand Error” and “Divide by Zero” exception traps, call
mathtrap(FPET_OPERR | FPET_DIVZR);
To disable all Floating−point exception traps (this is default status), call
mathtrap(FPET_CLEAR);
To specify IEEE 754 ’s “Invalid Operation”, call
mathtrap(FPET_INVOP);
where FPET_INVOP equals FPET_BSUNO | FPET_SGNAN | FPET_OPERR.
Trap handler is specified with signal(3) function. The signal number of all Floating-point exception traps is SIGFPE which is defined in “<signal.h>”. And signal codes are:
Exception signal code (<signal.h>)
Branch/Set on Unordered FPE_UNORDERED_TRAP
Signaling Not a Number FPE_SNAN_TRAP
Operand Error FPE_OPERAND_TRAP
Overflow FPE_OVERFLOW_TRAP
Underflow FPE_UNDERFLOW_TRAP
Divide by Zero FPE_DIVZERO_TRAP
Inexact FPE_INEXACT_TRAP
A sample trap handler which recognize the kind of exception is shown.
#include <math.h>
#include <signal.h>
main()
{
int handler();
/∗
∗Set up exception handler
∗/
mathtrap( FPET_INVOP | FPET_OVFLW |
FPET_DIVZR | FPET_UNDFL );
signal( SIGFPE , handler);
.
.
.
}
/∗
∗Exception handler
∗/
handler(sig, code)
int sig, code;
{
switch (code) {
case FPE_UNORDERED_TRAP :
/∗
∗Branch/Set on Unordered TRAP
∗/
break;
case FPE_SNAN_TRAP :
/∗
∗Signaling NaN (Not a Number)
∗/
break;
case FPE_OPERAND_TRAP :
/∗
∗Operand error TRAP
∗/
break;
case FPE_OVERFLOW_TRAP :
/∗
∗Over Flow
∗/
break;
case FPE_UNDERFLOW_TRAP :
/∗
∗Under Flow
∗/
break;
case FPE_DIVZERO_TRAP :
/∗
∗Divide by 0
∗/
break;
case FPE_INEXACT_TRAP :
/∗
∗Inexact TRAP
∗/
break;
}
.
.
.
}
SEE ALSO
MOTOROLA "MC68881/MC68882 Floating−Point Coprocessor User’s Manual"
intro(3), math(3m)
FILES
/usr/include/math.h
/usr/include/signal.h
NOTE
This library function is only supported on CISC NEWS and in the RISC NEWS 68k cross compiler environment.
NEWS-OSRelease 4.1C