Operating System - OpenVMS
1821801 Members
3060 Online
109637 Solutions
New Discussion юеВ

How to Turn Off Floating-Point Exception Interruption

 
SOLVED
Go to solution
Ray Lu
Occasional Contributor

How to Turn Off Floating-Point Exception Interruption

If I have overflowed floating-point values in my C program, the program is interrupted and floating-point value exception message is returned by default. Is there any way to turn off this exception handling, like a compiler option or system function?

I'm running on a OpenVMS Alpha system. I mainly care about VAX floating-point types. I've tried cc option /PDSC_MASK=SILENT but saw no difference. I believe there's no such systerm functions as feholdexcept() on Unix to turn off exception complaint. My short test program is attached below.

Thanks for helping me.

Ray

------------------
#include
#include

int main(void)
{
int i, j;
double d;
unsigned char *dp = (unsigned char*)&d;
float f;

for (i=0; i<16; i++) {
for (j=0; j dp[j] = rand();
f = (float)d;
printf("f=%g\n", (float)f);
}

return 0;
}
3 REPLIES 3
Craig A Berry
Honored Contributor
Solution

Re: How to Turn Off Floating-Point Exception Interruption

I don't think there's a way to do that with VAX doubles (D_FLOAT). Is there any reason you can't use IEEE floats? Try compiling with

$ cc/float=ieee/ieee=denorm
John Gillings
Honored Contributor

Re: How to Turn Off Floating-Point Exception Interruption

Ray,

It's very dangerous to ignore fatal exceptions. However, if you really want to you can catch any exception using an exception handler and then do what you like with it.

Here's an example based on your sample program using the standard handler LIB$SIG_TO_RET which converts any signalled exception into a return status and unwinds from the declaring routine.

#include
#include
#include

int sub(float *f){
int i, j;
double d;
unsigned char *dp = (unsigned char*)&d;

VAXC$ESTABLISH(LIB$SIG_TO_RET);
for (i=0; i<16; i++) {
for (j=0; j dp[j] = rand();
*f = (float)d;
}
return 1;
}

int main(void)
{
float f;

printf("stat=%d f=%g \n", sub(&f),(float)f);

return 0;
}

The result is

stat=1284 f=0

1284 is HPARITH, and f is 0 because the store operation that resulted in the exception did not complete. By the time you're back in the main program, all the useful information from the signal array and mechanism args has been lost.

If you want different behaviour you can write your own exception handler, but I'm not sure if you'll be able to continue from a fatal exception, so unwinding may be your only option.
A crucible of informative mistakes
Jim_McKinney
Honored Contributor

Re: How to Turn Off Floating-Point Exception Interruption

Perhaps sigaction()...


#include
#include
#include

int main(void)
{
int i, j;
double d;
unsigned char *dp = (unsigned char*)&d;
float f;

struct sigaction ignore;

ignore.sa_handler = SIG_IGN;
ignore.sa_flags = 0;
sigemptyset(&ignore.sa_mask);
sigaction(SIGFPE,&ignore,(struct sigaction *)NULL);

for (i=0; i<16; i++) {
for (j=0; jdp[j] = rand();
f = (float)d;
printf("f=%g\n", (float)f);
}

return 0;
}