Re: Divide by 0 - exception thrown?
On Tue, 17 Oct 2006 10:27:23 -0300, "Jason Doucette"
<www.jasondoucette.com> wrote:
Doug, this is precisely what I have proposed in the first post of this
thread. I was mainly concerned with reliability and portability, however.
I found that it should be reliable, as long as 3rd party code (i.e. drivers)
do not change the FPU control word and forget to change it back (I am
uncertain as when such code could be invoked, and thus when the best place
to confirm the control word is as it should be).
If the system ever borrows your thread out from under you and fails to save
and restore its context, it has a very bad bug, and I would assume that
doesn't happen. As for third party code such as the printer driver
mentioned earlier, I wouldn't worry about it unless I was calling it in
between some FP calculations.
As far as portability, so
far, due to lack of information, I can only *assume* that if other platforms
use FPUs that follow the IEEE standard, which supports NaNs, that the
hardware can be set to mask exceptions (i.e. that is, to follow the IEEE
standard). I should be able to set the compilation to fail on such
platforms, so I can find / implement their OS specific functions to ensure
the FPU control word at that time. Also, I was planning on merely using
_isnan() to check the final result to see if anything "bad" happened, but
the functions you mention could work, as well, but they'd require a little
more work (i.e. I don't considered a denormal an error; it's just a loss of
precision -- but some people could).
Note that _isnan won't detect overflow or (almost all) division by zero;
for these things, you'd have to use _finite. Checking the status word can
give you some details on the error.
--
Doug Harrison
Visual C++ MVP