#-*-Coding:utf-8-*-
__author__ = ' Administrator '
#数学计算
Import decimal# for fixed-point and floating-point arithmetic
#文档: Https://docs.python.org/2.7/library/decimal.html?highlight=decimal#module-decimal
#使用from_float () can accurately convert decimals
Fmt= ' {0:<25} {1:<25} '
Print Fmt.format (' INPUT ', ' output ')
Print Fmt.format ('-' *25, '-' *25)
Print Fmt.format (5,decimal. Decimal (5))
Print Fmt.format (' 3.14 ', decimal. Decimal (' 3.14 '))
Print Fmt.format (repr (. 1), Decimal. Decimal (str (. 1)))
f=0.1
Print Fmt.format ('%.23g '%.1,str (decimal. Decimal.from_float (f)) [: 25])
#decimal. Decimal can also be tuples, 0 for positive, 1 for negative, numeric tuple, and an integer exponent
T= (1, (+),-2)
print ' INPT: ', t
Print Decimal. Decimal (t)
#说明: is a portable way to export small values without damaging precision, tuples are transferred over the network, or stored in an exact fractional database is not supported
#计算 (Give a few simple examples)
Print
A=decimal. Decimal (' 2 ')
B=decimal. Decimal (' 1.2 ')
Print Str (a), str (b)
Print A+b,a-b,b*a,a/b,a%b
#特殊值
Print
For TSZ in [' Infinity ', ' NaN ', ' 0 ']:
Print Decimal. Decimal (TSZ), Decimal. Decimal ('-' +tsz)
Print Decimal. Decimal (' NaN ') ==decimal. Decimal (' Infinity ')
#上下文
Print
#获取当前全局上下文, GetContext ()
Import Pprint
Con=decimal.getcontext ()
Print con. Emax
Print con. Emin
Print con.capitals#1
Print con.prec#28
Print Con.rounding#round_half_even
Pprint.pprint (Con.traps)
#返回以下内容:
"""
{<class ' decimal. Clamped ';: 0,
<class ' Decimal. InvalidOperation ';: 1,
<class ' Decimal. Divisionbyzero ';: 1,
<class ' Decimal. Inexact ';: 0,
<class ' Decimal. Rounded ';: 0,
<class ' Decimal. Subnormal ';: 0,
<class ' Decimal. Overflow ';: 1,
<class ' Decimal. Underflow ';: 0}
"""
Python module: decimal