MCPcopy
hub / github.com/numpy/numpy / test_true_divide

Method test_true_divide

numpy/_core/tests/test_ufunc.py:641–726  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

639 np.equal(np.array([3]), 2**300, casting="equiv")
640
641 def test_true_divide(self):
642 a = np.array(10)
643 b = np.array(20)
644 tgt = np.array(0.5)
645
646 for tc in 'bhilqBHILQefdgFDG':
647 dt = np.dtype(tc)
648 aa = a.astype(dt)
649 bb = b.astype(dt)
650
651 # Check result value and dtype.
652 for x, y in itertools.product([aa, -aa], [bb, -bb]):
653
654 # Check with no output type specified
655 if tc in 'FDG':
656 tgt = complex(x) / complex(y)
657 else:
658 tgt = float(x) / float(y)
659
660 res = np.true_divide(x, y)
661 rtol = max(np.finfo(res).resolution, 1e-15)
662 assert_allclose(res, tgt, rtol=rtol)
663
664 if tc in 'bhilqBHILQ':
665 assert_(res.dtype.name == 'float64')
666 else:
667 assert_(res.dtype.name == dt.name)
668
669 # Check with output type specified. This also checks for the
670 # incorrect casts in issue gh-3484 because the unary '-' does
671 # not change types, even for unsigned types, Hence casts in the
672 # ufunc from signed to unsigned and vice versa will lead to
673 # errors in the values.
674 for tcout in 'bhilqBHILQ':
675 dtout = np.dtype(tcout)
676 assert_raises(TypeError, np.true_divide, x, y, dtype=dtout)
677
678 for tcout in 'efdg':
679 dtout = np.dtype(tcout)
680 if tc in 'FDG':
681 # Casting complex to float is not allowed
682 assert_raises(TypeError, np.true_divide, x, y, dtype=dtout)
683 else:
684 tgt = float(x) / float(y)
685 rtol = max(np.finfo(dtout).resolution, 1e-15)
686 # The value of tiny for double double is NaN
687 with warnings.catch_warnings():
688 warnings.simplefilter('ignore', UserWarning)
689 if not np.isnan(np.finfo(dtout).tiny):
690 atol = max(np.finfo(dtout).tiny, 3e-308)
691 else:
692 atol = 3e-308
693 # Some test values result in invalid for float16
694 # and the cast to it may overflow to inf.
695 with np.errstate(invalid='ignore', over='ignore'):
696 res = np.true_divide(x, y, dtype=dtout)
697 if not np.isfinite(res) and tcout == 'e':
698 continue

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected