MCPcopy Create free account
hub / github.com/numpy/numpy / test_true_divide

Method test_true_divide

numpy/core/tests/test_ufunc.py:611–696  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

609 assert_array_equal(expected, res)
610
611 def test_true_divide(self):
612 a = np.array(10)
613 b = np.array(20)
614 tgt = np.array(0.5)
615
616 for tc in 'bhilqBHILQefdgFDG':
617 dt = np.dtype(tc)
618 aa = a.astype(dt)
619 bb = b.astype(dt)
620
621 # Check result value and dtype.
622 for x, y in itertools.product([aa, -aa], [bb, -bb]):
623
624 # Check with no output type specified
625 if tc in 'FDG':
626 tgt = complex(x)/complex(y)
627 else:
628 tgt = float(x)/float(y)
629
630 res = np.true_divide(x, y)
631 rtol = max(np.finfo(res).resolution, 1e-15)
632 assert_allclose(res, tgt, rtol=rtol)
633
634 if tc in 'bhilqBHILQ':
635 assert_(res.dtype.name == 'float64')
636 else:
637 assert_(res.dtype.name == dt.name )
638
639 # Check with output type specified. This also checks for the
640 # incorrect casts in issue gh-3484 because the unary '-' does
641 # not change types, even for unsigned types, Hence casts in the
642 # ufunc from signed to unsigned and vice versa will lead to
643 # errors in the values.
644 for tcout in 'bhilqBHILQ':
645 dtout = np.dtype(tcout)
646 assert_raises(TypeError, np.true_divide, x, y, dtype=dtout)
647
648 for tcout in 'efdg':
649 dtout = np.dtype(tcout)
650 if tc in 'FDG':
651 # Casting complex to float is not allowed
652 assert_raises(TypeError, np.true_divide, x, y, dtype=dtout)
653 else:
654 tgt = float(x)/float(y)
655 rtol = max(np.finfo(dtout).resolution, 1e-15)
656 # The value of tiny for double double is NaN
657 with suppress_warnings() as sup:
658 sup.filter(UserWarning)
659 if not np.isnan(np.finfo(dtout).tiny):
660 atol = max(np.finfo(dtout).tiny, 3e-308)
661 else:
662 atol = 3e-308
663 # Some test values result in invalid for float16
664 # and the cast to it may overflow to inf.
665 with np.errstate(invalid='ignore', over='ignore'):
666 res = np.true_divide(x, y, dtype=dtout)
667 if not np.isfinite(res) and tcout == 'e':
668 continue

Callers

nothing calls this directly

Calls 8

assert_allcloseFunction · 0.90
assert_Function · 0.90
assert_raisesFunction · 0.90
suppress_warningsClass · 0.90
astypeMethod · 0.80
filterMethod · 0.80
maxFunction · 0.50
dtypeMethod · 0.45

Tested by

no test coverage detected