MCPcopy Index your code
hub / github.com/python/cpython / test_from_float

Method test_from_float

Lib/test/test_decimal.py:2711–2741  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2709 self.assertEqual(Decimal(math.trunc(d)), r)
2710
2711 def test_from_float(self):
2712
2713 Decimal = self.decimal.Decimal
2714
2715 class MyDecimal(Decimal):
2716 def __init__(self, _):
2717 self.x = 'y'
2718
2719 self.assertIsSubclass(MyDecimal, Decimal)
2720
2721 r = MyDecimal.from_float(0.1)
2722 self.assertEqual(type(r), MyDecimal)
2723 self.assertEqual(str(r),
2724 '0.1000000000000000055511151231257827021181583404541015625')
2725 self.assertEqual(r.x, 'y')
2726
2727 bigint = 12345678901234567890123456789
2728 self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
2729 self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan())
2730 self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite())
2731 self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite())
2732 self.assertEqual(str(MyDecimal.from_float(float('nan'))),
2733 str(Decimal('NaN')))
2734 self.assertEqual(str(MyDecimal.from_float(float('inf'))),
2735 str(Decimal('Infinity')))
2736 self.assertEqual(str(MyDecimal.from_float(float('-inf'))),
2737 str(Decimal('-Infinity')))
2738 self.assertRaises(TypeError, MyDecimal.from_float, 'abc')
2739 for i in range(200):
2740 x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
2741 self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip
2742
2743 def test_create_decimal_from_float(self):
2744 Decimal = self.decimal.Decimal

Callers

nothing calls this directly

Calls 12

strFunction · 0.85
DecimalClass · 0.85
assertIsSubclassMethod · 0.80
assertTrueMethod · 0.80
expovariateMethod · 0.80
MyDecimalClass · 0.70
from_floatMethod · 0.45
assertEqualMethod · 0.45
is_qnanMethod · 0.45
is_infiniteMethod · 0.45
assertRaisesMethod · 0.45
randomMethod · 0.45

Tested by

no test coverage detected