[Numpy-discussion] array_equal too strict?

Chris Barker - NOAA Federal chris.barker at noaa.gov
Thu Dec 17 21:08:03 EST 2015


> If you have some spare cycles, maybe you can open a pull request to add
> np.isclose to the "See Also" section?

That would be great.

Remember that equality for flits is bit-for but equality ( baring NaN
and inf...).

But you hardly ever actually want to do that with floats.

But probably np.allclose is most appropriate here.

CHB

>
> - Sebastian
>
>
>> ```
>> print(numpy.array_equal(a1, a2))
>>
>> # output: true
>> ```
>> That's expected because the difference is only in the 18th overall
>> digit, and the mantissa length of float64 is 52 bits [1], i.e., approx
>> 15.6 decimal digits. Moving the difference to the 17th overall digit
>> should also be fine, however:
>> ```
>> a1 = numpy.array(
>>    [3.1415926535897930],
>>    dtype=numpy.float64
>>    )
>> a2 = numpy.array(
>>    [3.1415926535897939],
>>    dtype=numpy.float64
>>    )
>>
>>
>> print(numpy.array_equal(a1, a2))
>> # output: false
>> ```
>> It gets even more visible with float32 and its 23 mantissa bits (i.e.,
>> 6.9 decimal digits):
>> ```
>> a1 = numpy.array(
>>    [3.14159260],
>>    dtype=numpy.float32
>>    )
>> a2 = numpy.array(
>>    [3.14159269],
>>    dtype=numpy.float32
>>    )
>>
>>
>> print(numpy.array_equal(a1, a2))
>> # output: false
>> ```
>> The difference is only in the 9th decimal digit, still `array_equal_
>> detects the difference.
>>
>>
>> I'm not sure where I'm going wrong here. Any hints?
>>
>>
>> Cheers,
>> Nico
>>
>>
>>
>>
>> [1] https://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> https://mail.scipy.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list