[Tutor] unitest with random inputs

Peter Otten __peter__ at web.de
Wed Jul 19 14:04:24 EDT 2017

Sydney Shall wrote:

> On 19/07/2017 18:56, Steven D'Aprano wrote:
>> On Wed, Jul 19, 2017 at 06:08:57PM +0200, Sydney Shall wrote:
>> (I haven't tested that code myself, so please try it, and if it doesn't
>> work for some reason, let us know on the mailing list and somebody can
>> fix it.)
>> But... I'm rather surprised that you need this test. Are you sure that
>> your array capitalsadvanced will *always* contain at least one Not A
>> Number value? Unless you put one in yourself, NANs generally indicate
>> that a mathematical error has occurred somewhere.
> Steven,
> Thanks again.
> I started this precisely because I also thought and still think, that
> the origin of my problem is a mathematical error somewhere.
> However, when I use predetermined input values, the errors are absent.
> They only appear when I use the random function. Although it should not
> be the case, I wondered whether the random function was feeding in a
> zero, because the errors seem to involve an invalid value after a
> true-divide according to some of the error reports earlier. So, my first
> step was to try and see if there are any zeros in the array that is used
> first. The tests say that the type and the length of the array is
> correct. But how do I test for a zero in a numpy.ndarray?

(a == 0).any()

But dividing by 0 should give you inf, not nan:

>>> import numpy
>>> a = numpy.arange(5)
>>> b = 1 / a
__main__:1: RuntimeWarning: divide by zero encountered in true_divide
>>> b
array([        inf,  1.        ,  0.5       ,  0.33333333,  0.25      ])

Can you show a bit more of the relevant code, a toy script that shows the 
problematic behaviour perhaps? That might reduce the need for speculation.

More information about the Tutor mailing list