Esempio n. 1
0
    def test_AsSecondsDouble(self):
        from _testcapi import PyTime_AsSecondsDouble

        def float_converter(ns):
            if abs(ns) % SEC_TO_NS == 0:
                return float(ns // SEC_TO_NS)
            else:
                return float(ns) / SEC_TO_NS

        self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns),
                                float_converter, NS_TO_SEC)

        # test nan
        for time_rnd, _ in ROUNDING_MODES:
            with self.assertRaises(TypeError):
                PyTime_AsSecondsDouble(float('nan'))
Esempio n. 2
0
    def test_AsSecondsDouble(self):
        from _testcapi import PyTime_AsSecondsDouble

        def float_converter(ns):
            if abs(ns) % SEC_TO_NS == 0:
                return float(ns // SEC_TO_NS)
            else:
                return float(ns) / SEC_TO_NS

        self.check_int_rounding(lambda ns, rnd: PyTime_AsSecondsDouble(ns),
                                float_converter, NS_TO_SEC)
    def test_AsSecondsDouble(self):
        from _testcapi import PyTime_AsSecondsDouble

        for nanoseconds, seconds in (
            # near 1 nanosecond
            ( 0,  0.0),
            ( 1,  1e-9),
            (-1, -1e-9),

            # near 1 second
            (SEC_TO_NS + 1, 1.0 + 1e-9),
            (SEC_TO_NS,     1.0),
            (SEC_TO_NS - 1, 1.0 - 1e-9),

            # a few seconds
            (123 * SEC_TO_NS, 123.0),
            (-567 * SEC_TO_NS, -567.0),

            # nanosecond are kept for value <= 2^23 seconds
            (4194303999999999, 2**22 - 1e-9),
            (4194304000000000, 2**22),
            (4194304000000001, 2**22 + 1e-9),

            # start losing precision for value > 2^23 seconds
            (8388608000000002, 2**23 + 1e-9),

            # nanoseconds are lost for value > 2^23 seconds
            (16777215999999998, 2**24 - 1e-9),
            (16777215999999999, 2**24 - 1e-9),
            (16777216000000000, 2**24       ),
            (16777216000000001, 2**24       ),
            (16777216000000002, 2**24 + 2e-9),

            (33554432000000000, 2**25       ),
            (33554432000000002, 2**25       ),
            (33554432000000004, 2**25 + 4e-9),

            # close to 2^63 nanoseconds (_PyTime_t limit)
            (9223372036 * SEC_TO_NS, 9223372036.0),
            (-9223372036 * SEC_TO_NS, -9223372036.0),
        ):
            with self.subTest(nanoseconds=nanoseconds, seconds=seconds):
                self.assertEqual(PyTime_AsSecondsDouble(nanoseconds),
                                 seconds)