From: Darren Dale <dd55@co...> - 2004-10-04 00:58:44
Would somebody kindly direct me to some information on how to deal with
dividing by zero? I am getting ValueError: math domain error, is there
anything I can do to return an inf instead?
From: Darren Dale <dd55@co...> - 2004-10-04 15:03:32
On Monday 04 October 2004 10:38 am, you wrote:
> On Monday 04 October 2004 00:58, Darren Dale wrote:
> > Would somebody kindly direct me to some information on how to deal with
> > dividing by zero? I am getting ValueError: math domain error, is there
> > anything I can do to return an inf instead?
> > Thanks,
> > Darren
> If you can't avoid the division by zero, you can handle the exception:
> a = x/0
> except ValueError:
> pass # or some other outcome (a = 'inf')
> as far as I know python does not have a representation for infinity. (if I
> am wrong, somebody please correct me)
There are some special representations that can be imported, for example
from numarray.ieeespecial import inf
However, My simulations require HEAVY array mathematics, so I dont have an
opportunity to test for exceptions.
From: Darren Dale <dd55@co...> - 2004-10-04 16:26:21
> from the numpy list:
> > numarray allows one to customize how errors are handled. You can
> > choose:
> > 1) to silently ignore all errors.
> > 2) print a warning message (default)
> > 3) raise an exception.
> > One may separately set one of these three behaviors for each of
> > the 4 ieee categories of floating point errors, namely
> > 1) invalid results (i.e., NaNs)
> > 2) divide by zeros (Infs)
> > 3) overflows
> > 4) underflows
> now try this:
> from numarray import *
> Warning: Encountered divide by zero(s) in divide
> array([ inf, 1.00000000e+000, 5.00000000e-001,
> 3.33333333e-001, 2.50000000e-001, 2.00000000e-001,
> 1.66666667e-001, 1.42857143e-001, 1.25000000e-001,
> there is the inf!!
Thank you Flavio,
I had tried the test you just suggested, and only got the warning message. I
incorrectly assumed that the result had not been returned. Now I see that it
from numarray import *
a=1./arange(10) #displays error
print a # displays a with the inf