You know how sometimes a code error feels like you must be losing your mind? I thought I was and then I determined that there's some sort of rounding error here.
Why is the first cell in result
not equal to the same cell in expected
? This is obviously rounding (see second to last test), but it feels like bug-level rounding to me. Second, if 1 - .9 * .9 is not .19, why isn't is .19000000000004 or whatever I usually see when there's a rounding thing happening.
def calc_negation(negations):
takens = 1. - negations
return 1. - np.prod(takens, axis=1, keepdims=True)
result = calc_negation(np.array([[.1, .1], [.5, .5]]))
expected = np.array([[.19], [.75]])
print(result)
print(result == expected) # show the failed equality.
assert result.shape == (2, 1)
assert result.shape == expected.shape
assert result.dtype == expected.dtype
assert expected[0, 0] == .19 # why is this okay!?!
assert expected[1, 0] == .75
assert np.allclose(result, expected) # why is numpy rounding 1 - (.9 * .9)?
assert np.array_equal(result, expected) # FAILS!