Nico Schlömer
2015-12-17 13:43:22 UTC
Hi everyone,
I noticed a funny behavior in numpy's array_equal. The two arrays
```
a1 = numpy.array(
[3.14159265358979320],
dtype=numpy.float64
)
a2 = numpy.array(
[3.14159265358979329],
dtype=numpy.float64
)
```
(differing the in the 18th overall digit) are reported equal by array_equal:
```
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
I noticed a funny behavior in numpy's array_equal. The two arrays
```
a1 = numpy.array(
[3.14159265358979320],
dtype=numpy.float64
)
a2 = numpy.array(
[3.14159265358979329],
dtype=numpy.float64
)
```
(differing the in the 18th overall digit) are reported equal by array_equal:
```
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