Re: catching floating point exception on OS X Intel
Re: catching floating point exception on OS X Intel
- Subject: Re: catching floating point exception on OS X Intel
- From: Axel Luttgens <email@hidden>
- Date: Wed, 09 May 2007 16:59:21 +0200
On 9/05/07 1:29, Kristoffer Eriksen wrote:
Hi-
I tried this question on darwin-x86 to no avail, so I thought I
would try here.
[...]
To summarize, I'm using the feraiseexcept() function from fenv.h,
which I had hoped would enable floating point exceptions for the
requested flags.
[...]
I'm a scientist, not a systems programmer, so I may just be doing
something stupid here. Any ideas?
Function feraiseexcept() doesn't raise an exception, it only raises one
or more exception flags.
From the Mac_OSX_Numerics.pdf (which still seems to apply):
Floating-point exceptions are signaled with exception flags.
When an application begins, all floating-point exception flags
are cleared and the default rounding direction (round to
nearest) is in effect. This is the default environment.
When an exception occurs, the appropriate exception flag is
set, but the application continues normal operation.
Floating-point exception flags merely indicate that a
particular event has occurred; they do not change the flow of
control for the application. An application can examine or set
individual exception flags and can save and retrieve the entire
environment (rounding direction and exception flags).
So, taking your example, but made somewhat more verbose for seeing
what's happening:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fenv.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
double x;
int retval;
pid_t pid;
pid = getpid();
/* Just to be sure ;-) */
feclearexcept(FE_ALL_EXCEPT);
/* This will set the FE_INEXACT flag */
x = 1.0/(double)pid;
retval = fetestexcept(FE_ALL_EXCEPT);
feclearexcept(FE_ALL_EXCEPT);
/* This will set the FE_INVALID flag */
x = sqrt(-x);
retval = fetestexcept(FE_ALL_EXCEPT);
if (retval != 0)
raise(SIGFPE);
printf("%d %d %f\n",retval,pid,x);
exit(0);
}
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden