Re: Divide by 0 - exception thrown?
- From: "Jason Doucette" <www.jasondoucette.com>
- Date: Tue, 17 Oct 2006 10:27:23 -0300
So, I must ask...
How do all of you prevent divide by 0 and overflows?
The first thing is to see if you can perform the calculation in such a way
that subexpressions don't foul up while still yielding the same answer.
I have done this as much as possible. Usually this means storing the
inverse and multiplying, when it is a commonly used constant. This also has
the advantage of additional speed.
This may be easier for integer vs. floating point calculations. Concerning
the latter, it may be easiest not to even try to prevent errors beyond the
trivial division by zero but instead to detect them when you're done. You
can leave FP exceptions masked and detect errors by checking the FPU
status
word. From memory, you will use functions like _clearfp and _statusfp.
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). 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).
Thanks for your time,
--
Jason Doucette / Xona.com
www.jasondoucette.com / www.xona.com
.
- Follow-Ups:
- Re: Divide by 0 - exception thrown?
- From: Doug Harrison [MVP]
- Re: Divide by 0 - exception thrown?
- References:
- Divide by 0 - exception thrown?
- From: Jason Doucette
- Re: Divide by 0 - exception thrown?
- From: Jason Doucette
- Re: Divide by 0 - exception thrown?
- From: Doug Harrison [MVP]
- Divide by 0 - exception thrown?
- Prev by Date: Re: Wht is Environment variable? and it's use
- Next by Date: Problem where msvc-8.0 includes msvc-6.0 headers
- Previous by thread: Re: Divide by 0 - exception thrown?
- Next by thread: Re: Divide by 0 - exception thrown?
- Index(es):
Relevant Pages
|