www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/03/01:49:18

From: "John Luebs" <sales AT luebsphoto DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: division by 0
Date: 3 Feb 1998 02:39:44 GMT
Organization: Luebs & Son Photo
Lines: 53
Message-ID: <01bd304c$61945240$a3b972ce@comp>
References: <34CF2295 DOT 7457 AT quantum DOT de> <#$L$GXFL9GA DOT 231 AT upnetnews02 DOT moswest DOT msn DOT net> <34D077C7 DOT 55500E21 AT cornell DOT edu>
NNTP-Posting-Host: sa1-b2.dreamscape.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019