<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 The decimal module implements fixed and floating point arithmetic using the model familiar to most people, rather than the IEEE floating point version implemented by most computer hardware and familiar to programmers. A Decimal instance can represent any number exactly, round up or down, and apply a limit to the number of significant digits.
Decimal
Step1:  Decimals can also be created from tuples containing a sign flag
Step2:  Formatting
Step3:  Arithmetic
Step4:  Special Value
Step5:  Context
Step6:  Precision
Step7:  Local Context
<ASSISTANT_TASK:>
Python Code:
import decimal
fmt = '{0:<25}{1:<25}'
print(fmt.format('Input', 'Output'))
print(fmt.format('-'*25, '-'*25))
#Integer
print(fmt.format(5, decimal.Decimal(5)))
#String
print(fmt.format('3.14', decimal.Decimal('3.14')))
#Float
f = 0.1
print(fmt.format(repr(f), decimal.Decimal(str(f))))
print('{:0.23g}{:<25}'.format(f, str(decimal.Decimal.from_float(f))[:25]))
Explanation: The decimal module implements fixed and floating point arithmetic using the model familiar to most people, rather than the IEEE floating point version implemented by most computer hardware and familiar to programmers. A Decimal instance can represent any number exactly, round up or down, and apply a limit to the number of significant digits.
Decimal
End of explanation
import decimal
# Tuple
t = (1, (1, 1), -2)
print('Input  :', t)
print('Decimal:', decimal.Decimal(t))
Explanation: Decimals can also be created from tuples containing a sign flag
End of explanation
import decimal
d = decimal.Decimal(1.1)
print('Precision:')
print('{:.1}'.format(d))
print('{:.2}'.format(d))
print('{:.3}'.format(d))
print('{:.18}'.format(d))
print('\nWidth and precision combined:')
print('{:5.1f} {:5.1g}'.format(d, d))
print('{:5.2f} {:5.2g}'.format(d, d))
print('{:5.2f} {:5.2g}'.format(d, d))
print('\nZero padding:')
print('{:05.1}'.format(d))
print('{:05.2}'.format(d))
print('{:05.3}'.format(d))
Explanation: Formatting
End of explanation
import decimal
a = decimal.Decimal('5.1')
b = decimal.Decimal('3.14')
c = 4
d = 3.14
print('a     =', repr(a))
print('b     =', repr(b))
print('c     =', repr(c))
print('d     =', repr(d))
print()
print('a + b =', a + b)
print('a - b =', a - b)
print('a * b =', a * b)
print('a / b =', a / b)
print()
print('a + c =', a + c)
print('a - c =', a - c)
print('a * c =', a * c)
print('a / c =', a / c)
print()
print('a + d =', end=' ')
try:
    print(a + d)
except TypeError as e:
    print(e)
Explanation: Arithmetic
End of explanation
import decimal
for value in ['Infinity', 'NaN', '0']:
    print(decimal.Decimal(value), decimal.Decimal('-' + value))
print()
# Math with infinity
print('Infinity + 1:', (decimal.Decimal('Infinity') + 1))
print('-Infinity + 1:', (decimal.Decimal('-Infinity') + 1))
# Print comparing NaN
print(decimal.Decimal('NaN') == decimal.Decimal('Infinity'))
print(decimal.Decimal('NaN') != decimal.Decimal(1))
Explanation: Special Value
End of explanation
import decimal
context = decimal.getcontext()
print('Emax     =', context.Emax)
print('Emin     =', context.Emin)
print('capitals =', context.capitals)
print('prec     =', context.prec)
print('rounding =', context.rounding)
print('flags    =')
for f, v in context.flags.items():
    print('  {}: {}'.format(f, v))
print('traps    =')
for t, v in context.traps.items():
    print('  {}: {}'.format(t, v))
Explanation: Context
End of explanation
import decimal
d = decimal.Decimal('0.123456')
for i in range(1, 5):
    decimal.getcontext().prec = i
    print(i, ':', d, d * 1)
Explanation: Precision
End of explanation
import decimal
with decimal.localcontext() as c:
    c.prec = 2
    print('Local precision:', c.prec)
    print('3.14 / 3 =', (decimal.Decimal('3.14') / 3))
print()
print('Default precision:', decimal.getcontext().prec)
print('3.14 / 3 =', (decimal.Decimal('3.14') / 3))
import decimal
# Set up a context with limited precision
c = decimal.getcontext().copy()
c.prec = 3
# Create our constant
pi = c.create_decimal('3.1415')
# The constant value is rounded off
print('PI    :', pi)
# The result of using the constant uses the global context
print('RESULT:', decimal.Decimal('2.01') * pi)
Explanation: Local Context
End of explanation
<END_TASK>