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

Method test_float_operation

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

Source from the content-addressed store, hash-verified

3985
3986 @requires_IEEE_754
3987 def test_float_operation(self):
3988 Decimal = self.decimal.Decimal
3989 FloatOperation = self.decimal.FloatOperation
3990 localcontext = self.decimal.localcontext
3991
3992 with localcontext() as c:
3993 ##### trap is off by default
3994 self.assertFalse(c.traps[FloatOperation])
3995
3996 # implicit conversion sets the flag
3997 c.clear_flags()
3998 self.assertEqual(Decimal(7.5), 7.5)
3999 self.assertTrue(c.flags[FloatOperation])
4000
4001 c.clear_flags()
4002 self.assertEqual(c.create_decimal(7.5), 7.5)
4003 self.assertTrue(c.flags[FloatOperation])
4004
4005 # explicit conversion does not set the flag
4006 c.clear_flags()
4007 x = Decimal.from_float(7.5)
4008 self.assertFalse(c.flags[FloatOperation])
4009 # comparison sets the flag
4010 self.assertEqual(x, 7.5)
4011 self.assertTrue(c.flags[FloatOperation])
4012
4013 c.clear_flags()
4014 x = c.create_decimal_from_float(7.5)
4015 self.assertFalse(c.flags[FloatOperation])
4016 self.assertEqual(x, 7.5)
4017 self.assertTrue(c.flags[FloatOperation])
4018
4019 ##### set the trap
4020 c.traps[FloatOperation] = True
4021
4022 # implicit conversion raises
4023 c.clear_flags()
4024 self.assertRaises(FloatOperation, Decimal, 7.5)
4025 self.assertTrue(c.flags[FloatOperation])
4026
4027 c.clear_flags()
4028 self.assertRaises(FloatOperation, c.create_decimal, 7.5)
4029 self.assertTrue(c.flags[FloatOperation])
4030
4031 # explicit conversion is silent
4032 c.clear_flags()
4033 x = Decimal.from_float(7.5)
4034 self.assertFalse(c.flags[FloatOperation])
4035
4036 c.clear_flags()
4037 x = c.create_decimal_from_float(7.5)
4038 self.assertFalse(c.flags[FloatOperation])
4039
4040 def test_float_comparison(self):
4041 Decimal = self.decimal.Decimal

Callers

nothing calls this directly

Calls 10

localcontextFunction · 0.85
DecimalClass · 0.85
assertFalseMethod · 0.80
clear_flagsMethod · 0.80
assertTrueMethod · 0.80
create_decimalMethod · 0.80
assertEqualMethod · 0.45
from_floatMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected