Mail Archives: djgpp/1998/02/03/01:49:18
The original poster brought up a great example. Obviously programs like
maple does this thing by faking it. When you say in your C program:
a = 1/0
and you compile this for an 80x86 it produces a DIV instruction. An Intel
80x86 along with many other compilers will always generate an exception. I
actually have zoomed to far in, because ANSI C specifies that the CRT has
to consider a division by zero as a run-time error. So even if the
processor did not generate a DIV/0 exception, the C run-time library would
probably determine this condition. (Bottom-line: the line x/0 will always
end in error) Now there are two ways of going around this:
1) This was a poor idea (I just thought about it), just brain storming (by
mind is a tangled web), sorry :-)
2) The only way (should be #1 now!!!). Make a divide function:
float divide(float a, float b)
{
if(!b) return MAX_FLOAT; /* if you never use the maximum then this may
work as "inf" */
else return a/b;
}
This is just a sketchy example, the way the function is implemented is
completely dependent on your application. You could also say if(!b)
do_something(); Do something would basically act as that "exception
handler" that would stop the equation solving process or whatever.
A. Sinan Unur <sinan DOT unur AT cornell DOT edu> wrote in article
<34D077C7 DOT 55500E21 AT cornell DOT edu>...
> DeHackEd wrote:
> >
> > Why would you want to divide by zero? It's a mathematical no-no, and
> > most computers are less forgiving than most mathematitions (ignore if
> > spelt wrong).
>
> there are valid situations (such as finding the roots of a function,
> some optimization algorithms) when you want the program to just take
> into account that the operation produced an infinity, and go on.
>
> > This code ALWAYS divides by zero,
>
> i think that wa meant as an illustration only.
>
> by the way, for nonzero x, x/0 is a well defined operation in the
> extended reals.
> --
> ----------------------------------------------------------------------
> A. Sinan Unur
> Department of Policy Analysis and Management, College of Human Ecology,
> Cornell University, Ithaca, NY 14853, USA
>
> mailto:sinan DOT unur AT cornell DOT edu
> http://www.people.cornell.edu/pages/asu1/
>
- Raw text -