Repository: numpy/numpy
File: numpy/_core/tests/test_arrayprint.py (Python)

Code before:
        np.set_printoptions(floatmode='fixed', precision=8)
        assert_equal(repr(z),
            "array([0.00000000, 0.09997559, 0.19995117, 0.30004883, 0.39990234,\n"
            "       0.50000000], dtype=float16)")

        # maxprec_equal  mode, precision=8
        np.set_printoptions(floatmode='maxprec_equal', precision=8)
        assert_equal(repr(x),
            "array([0.610352, 0.921875, 0.457031, 0.090576, 0.373291, 0.007244,\n"
            "       0.593262, 0.946777, 0.238281, 0.422607], dtype=float16)")
        assert_equal(repr(y),
            "array([0.29188210, 0.50641726, 0.28487506, 0.43429653, 0.73265384,\n"
            "       0.34595033, 0.08620728, 0.39112753])")
        assert_equal(repr(z),
            "array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5], dtype=float16)")
        assert_equal(repr(w[::5]),
            "array([1.e+00, 1.e+05, 1.e+10, 1.e+15, 1.e+20])")
        assert_equal(repr(wp), "array([1.234e+001, 1.000e+002, 1.000e+123])")
        assert_equal(repr(c),
            "array([1.00000000+1.00000000j, 1.12345679+1.12345679j])")

        # test unique special case (gh-18609)
        a = np.float64.fromhex('-1p-97')
        assert_equal(np.float64(np.array2string(a, floatmode='unique')), a)

    def test_gh_28679(self):
        # test cutoff to exponent notation for half, single, and complex64
        a = np.half([999, 999])
        b = np.half([999, 1000])
        c = np.single([9999999, 9999999])
        d = np.single([9999999, -10000000])
        e = np.complex64([9999999 + 9999999j, 9999999 + 9999999j])
        f = np.complex64([9999999 + 9999999j, 9999999 + -10000000j])
        assert_equal(str(a), "[999. 999.]")
        assert_equal(str(b), "[9.99e+02 1.00e+03]")
        assert_equal(str(c), "[9999999. 9999999.]")
        assert_equal(str(d), "[ 9.999999e+06 -1.000000e+07]")
        assert_equal(str(e), "[9999999.+9999999.j 9999999.+9999999.j]")
        assert_equal(str(f), "[9999999.+9.999999e+06j 9999999.-1.000000e+07j]")

    def test_legacy_mode_scalars(self):
        # in legacy mode, str of floats get truncated, and complex scalars
        # use * for non-finite imaginary part
        np.set_printoptions(legacy='1.13')
        assert_equal(str(np.float64(1.123456789123456789)), '1.12345678912')
        assert_equal(str(np.complex128(complex(1, np.nan))), '(1+nan*j)')

        np.set_printoptions(legacy=False)
        assert_equal(str(np.float64(1.123456789123456789)),
                     '1.1234567891234568')
        assert_equal(str(np.complex128(complex(1, np.nan))), '(1+nanj)')

Review comment (suggestion):
Here and below, I'd tend to `pytest.mark.parametrize("value, expected", [np.half(...), "[999. 999.]"), ...`, but no big deal.

Code after:
        np.set_printoptions(floatmode='fixed', precision=8)
        assert_equal(repr(z),
            "array([0.00000000, 0.09997559, 0.19995117, 0.30004883, 0.39990234,\n"
            "       0.50000000], dtype=float16)")

        # maxprec_equal  mode, precision=8
        np.set_printoptions(floatmode='maxprec_equal', precision=8)
        assert_equal(repr(x),
            "array([0.610352, 0.921875, 0.457031, 0.090576, 0.373291, 0.007244,\n"
            "       0.593262, 0.946777, 0.238281, 0.422607], dtype=float16)")
        assert_equal(repr(y),
            "array([0.29188210, 0.50641726, 0.28487506, 0.43429653, 0.73265384,\n"
            "       0.34595033, 0.08620728, 0.39112753])")
        assert_equal(repr(z),
            "array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5], dtype=float16)")
        assert_equal(repr(w[::5]),
            "array([1.e+00, 1.e+05, 1.e+10, 1.e+15, 1.e+20])")
        assert_equal(repr(wp), "array([1.234e+001, 1.000e+002, 1.000e+123])")
        assert_equal(repr(c),
            "array([1.00000000+1.00000000j, 1.12345679+1.12345679j])")

        # test unique special case (gh-18609)
        a = np.float64.fromhex('-1p-97')
        assert_equal(np.float64(np.array2string(a, floatmode='unique')), a)

    test_cases_gh_28679 = [
        (np.half([999, 999]), "[999. 999.]"),
        (np.half([999, 1000]), "[9.99e+02 1.00e+03]"),
        (np.single([999999, 999999]), "[999999. 999999.]"),
        (np.single([999999, -1000000]), "[ 9.99999e+05 -1.00000e+06]"),
        (
            np.complex64([999999 + 999999j, 999999 + 999999j]),
            "[999999.+999999.j 999999.+999999.j]"
        ),
        (
            np.complex64([999999 + 999999j, 999999 + -1000000j]),
            "[999999.+9.99999e+05j 999999.-1.00000e+06j]"
        ),
    ]

    @pytest.mark.parametrize("input_array, expected_str", test_cases_gh_28679)
    def test_gh_28679(self, input_array, expected_str):
        # test cutoff to exponent notation for half, single, and complex64
        assert_equal(str(input_array), expected_str)

    test_cases_legacy_2_2 = [
        (np.half([1.e3, 1.e4, 65504]), "[ 1000. 10000. 65504.]"),
        (np.single([1.e6, 1.e7]), "[ 1000000. 10000000.]"),
        (np.single([1.e7, 1.e8]), "[1.e+07 1.e+08]"),
    ]