Linux server.nvwebsoft.co.in 3.10.0-1160.114.2.el7.x86_64 #1 SMP Wed Mar 20 15:54:52 UTC 2024 x86_64
Apache
: 162.240.12.249 | : 3.137.221.114
202 Domain
8.1.31
nbspublicschool
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
CREATE WP USER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
49.27
KB
-rw-r--r--
__builtin__.html
10.26
KB
-rw-r--r--
__future__.html
13.79
KB
-rw-r--r--
__main__.html
7.05
KB
-rw-r--r--
_winreg.html
59.21
KB
-rw-r--r--
abc.html
23.9
KB
-rw-r--r--
aepack.html
13.16
KB
-rw-r--r--
aetools.html
14.91
KB
-rw-r--r--
aetypes.html
18.88
KB
-rw-r--r--
aifc.html
22.4
KB
-rw-r--r--
al.html
17.34
KB
-rw-r--r--
allos.html
33.72
KB
-rw-r--r--
anydbm.html
16.33
KB
-rw-r--r--
archiving.html
9.26
KB
-rw-r--r--
argparse.html
237.62
KB
-rw-r--r--
array.html
29.29
KB
-rw-r--r--
ast.html
34.98
KB
-rw-r--r--
asynchat.html
31.43
KB
-rw-r--r--
asyncore.html
36.51
KB
-rw-r--r--
atexit.html
16.8
KB
-rw-r--r--
audioop.html
31.36
KB
-rw-r--r--
autogil.html
8.19
KB
-rw-r--r--
base64.html
19.67
KB
-rw-r--r--
basehttpserver.html
34.04
KB
-rw-r--r--
bastion.html
11.04
KB
-rw-r--r--
bdb.html
36.68
KB
-rw-r--r--
binascii.html
20.67
KB
-rw-r--r--
binhex.html
10.58
KB
-rw-r--r--
bisect.html
23.24
KB
-rw-r--r--
bsddb.html
26.43
KB
-rw-r--r--
bz2.html
26.08
KB
-rw-r--r--
calendar.html
37.79
KB
-rw-r--r--
carbon.html
48.94
KB
-rw-r--r--
cd.html
27.96
KB
-rw-r--r--
cgi.html
49.92
KB
-rw-r--r--
cgihttpserver.html
13.1
KB
-rw-r--r--
cgitb.html
11.41
KB
-rw-r--r--
chunk.html
14.66
KB
-rw-r--r--
cmath.html
25.63
KB
-rw-r--r--
cmd.html
26.09
KB
-rw-r--r--
code.html
24.58
KB
-rw-r--r--
codecs.html
100.64
KB
-rw-r--r--
codeop.html
14.84
KB
-rw-r--r--
collections.html
133.96
KB
-rw-r--r--
colorpicker.html
7.52
KB
-rw-r--r--
colorsys.html
11.04
KB
-rw-r--r--
commands.html
14.36
KB
-rw-r--r--
compileall.html
16.83
KB
-rw-r--r--
compiler.html
67.75
KB
-rw-r--r--
configparser.html
62.13
KB
-rw-r--r--
constants.html
12.83
KB
-rw-r--r--
contextlib.html
19.39
KB
-rw-r--r--
cookie.html
39.07
KB
-rw-r--r--
cookielib.html
83.82
KB
-rw-r--r--
copy.html
12.19
KB
-rw-r--r--
copy_reg.html
13.76
KB
-rw-r--r--
crypt.html
10.04
KB
-rw-r--r--
crypto.html
7.59
KB
-rw-r--r--
csv.html
67.37
KB
-rw-r--r--
ctypes.html
238.78
KB
-rw-r--r--
curses.ascii.html
22.29
KB
-rw-r--r--
curses.html
146.63
KB
-rw-r--r--
curses.panel.html
14.39
KB
-rw-r--r--
custominterp.html
7.62
KB
-rw-r--r--
datatypes.html
16.84
KB
-rw-r--r--
datetime.html
226.59
KB
-rw-r--r--
dbhash.html
15.48
KB
-rw-r--r--
dbm.html
12.07
KB
-rw-r--r--
debug.html
10.15
KB
-rw-r--r--
decimal.html
194.44
KB
-rw-r--r--
development.html
14.17
KB
-rw-r--r--
difflib.html
84.83
KB
-rw-r--r--
dircache.html
11.41
KB
-rw-r--r--
dis.html
69.95
KB
-rw-r--r--
distutils.html
8.05
KB
-rw-r--r--
dl.html
16.33
KB
-rw-r--r--
doctest.html
165.54
KB
-rw-r--r--
docxmlrpcserver.html
16.43
KB
-rw-r--r--
dumbdbm.html
14.02
KB
-rw-r--r--
dummy_thread.html
9.43
KB
-rw-r--r--
dummy_threading.html
8.37
KB
-rw-r--r--
easydialogs.html
30.55
KB
-rw-r--r--
email-examples.html
45.65
KB
-rw-r--r--
email.charset.html
26.8
KB
-rw-r--r--
email.encoders.html
11.86
KB
-rw-r--r--
email.errors.html
15.77
KB
-rw-r--r--
email.generator.html
20.77
KB
-rw-r--r--
email.header.html
26.92
KB
-rw-r--r--
email.html
44.24
KB
-rw-r--r--
email.iterators.html
11.52
KB
-rw-r--r--
email.message.html
63.16
KB
-rw-r--r--
email.mime.html
27.93
KB
-rw-r--r--
email.parser.html
30.45
KB
-rw-r--r--
email.util.html
24.46
KB
-rw-r--r--
errno.html
37.99
KB
-rw-r--r--
exceptions.html
56.13
KB
-rw-r--r--
fcntl.html
22.67
KB
-rw-r--r--
filecmp.html
22.3
KB
-rw-r--r--
fileformats.html
9.14
KB
-rw-r--r--
fileinput.html
24.28
KB
-rw-r--r--
filesys.html
10.2
KB
-rw-r--r--
fl.html
49.92
KB
-rw-r--r--
fm.html
11.91
KB
-rw-r--r--
fnmatch.html
14.58
KB
-rw-r--r--
formatter.html
34.06
KB
-rw-r--r--
fpectl.html
16.01
KB
-rw-r--r--
fpformat.html
10.59
KB
-rw-r--r--
fractions.html
22.61
KB
-rw-r--r--
framework.html
33.34
KB
-rw-r--r--
frameworks.html
7.14
KB
-rw-r--r--
ftplib.html
43.99
KB
-rw-r--r--
functions.html
183.14
KB
-rw-r--r--
functools.html
27.17
KB
-rw-r--r--
future_builtins.html
13.04
KB
-rw-r--r--
gc.html
25.75
KB
-rw-r--r--
gdbm.html
15.96
KB
-rw-r--r--
gensuitemodule.html
11.51
KB
-rw-r--r--
getopt.html
23.66
KB
-rw-r--r--
getpass.html
10.65
KB
-rw-r--r--
gettext.html
78.76
KB
-rw-r--r--
gl.html
22.09
KB
-rw-r--r--
glob.html
13.26
KB
-rw-r--r--
grp.html
10.49
KB
-rw-r--r--
gzip.html
18.99
KB
-rw-r--r--
hashlib.html
18.2
KB
-rw-r--r--
heapq.html
31.61
KB
-rw-r--r--
hmac.html
10.46
KB
-rw-r--r--
hotshot.html
18.65
KB
-rw-r--r--
htmllib.html
25.32
KB
-rw-r--r--
htmlparser.html
39.11
KB
-rw-r--r--
httplib.html
62.95
KB
-rw-r--r--
i18n.html
9.52
KB
-rw-r--r--
ic.html
17.17
KB
-rw-r--r--
idle.html
20.9
KB
-rw-r--r--
imageop.html
14.76
KB
-rw-r--r--
imaplib.html
51.99
KB
-rw-r--r--
imgfile.html
11.71
KB
-rw-r--r--
imghdr.html
11.3
KB
-rw-r--r--
imp.html
34.34
KB
-rw-r--r--
importlib.html
8.26
KB
-rw-r--r--
imputil.html
31.81
KB
-rw-r--r--
index.html
72.78
KB
-rw-r--r--
inspect.html
50.71
KB
-rw-r--r--
internet.html
24.87
KB
-rw-r--r--
intro.html
8.93
KB
-rw-r--r--
io.html
98.13
KB
-rw-r--r--
ipc.html
13.41
KB
-rw-r--r--
itertools.html
115.91
KB
-rw-r--r--
jpeg.html
12.74
KB
-rw-r--r--
json.html
67.04
KB
-rw-r--r--
keyword.html
7.68
KB
-rw-r--r--
language.html
11.03
KB
-rw-r--r--
linecache.html
10.59
KB
-rw-r--r--
locale.html
55.14
KB
-rw-r--r--
logging.config.html
63.36
KB
-rw-r--r--
logging.handlers.html
69.64
KB
-rw-r--r--
logging.html
95.64
KB
-rw-r--r--
mac.html
21.79
KB
-rw-r--r--
macos.html
14.76
KB
-rw-r--r--
macosa.html
12.96
KB
-rw-r--r--
macostools.html
15.52
KB
-rw-r--r--
macpath.html
7.76
KB
-rw-r--r--
mailbox.html
156.75
KB
-rw-r--r--
mailcap.html
13.21
KB
-rw-r--r--
markup.html
18.77
KB
-rw-r--r--
marshal.html
17.98
KB
-rw-r--r--
math.html
39.24
KB
-rw-r--r--
md5.html
13.97
KB
-rw-r--r--
mhlib.html
21.54
KB
-rw-r--r--
mimetools.html
19.25
KB
-rw-r--r--
mimetypes.html
28.39
KB
-rw-r--r--
mimewriter.html
15.02
KB
-rw-r--r--
mimify.html
13.36
KB
-rw-r--r--
miniaeframe.html
12.2
KB
-rw-r--r--
misc.html
6.87
KB
-rw-r--r--
mm.html
9.03
KB
-rw-r--r--
mmap.html
28.36
KB
-rw-r--r--
modulefinder.html
15.31
KB
-rw-r--r--
modules.html
8.46
KB
-rw-r--r--
msilib.html
52.43
KB
-rw-r--r--
msvcrt.html
19.37
KB
-rw-r--r--
multifile.html
24.3
KB
-rw-r--r--
multiprocessing.html
365.71
KB
-rw-r--r--
mutex.html
11.23
KB
-rw-r--r--
netdata.html
16.98
KB
-rw-r--r--
netrc.html
12.3
KB
-rw-r--r--
new.html
12.12
KB
-rw-r--r--
nis.html
10.64
KB
-rw-r--r--
nntplib.html
41.92
KB
-rw-r--r--
numbers.html
37.75
KB
-rw-r--r--
numeric.html
13.55
KB
-rw-r--r--
operator.html
82
KB
-rw-r--r--
optparse.html
222.56
KB
-rw-r--r--
os.html
214.25
KB
-rw-r--r--
os.path.html
38.34
KB
-rw-r--r--
ossaudiodev.html
41.5
KB
-rw-r--r--
othergui.html
9.08
KB
-rw-r--r--
parser.html
39.36
KB
-rw-r--r--
pdb.html
33.96
KB
-rw-r--r--
persistence.html
14.87
KB
-rw-r--r--
pickle.html
102.27
KB
-rw-r--r--
pickletools.html
10.63
KB
-rw-r--r--
pipes.html
18.01
KB
-rw-r--r--
pkgutil.html
25.11
KB
-rw-r--r--
platform.html
28.37
KB
-rw-r--r--
plistlib.html
17.03
KB
-rw-r--r--
popen2.html
25.43
KB
-rw-r--r--
poplib.html
22.32
KB
-rw-r--r--
posix.html
14.41
KB
-rw-r--r--
posixfile.html
19.76
KB
-rw-r--r--
pprint.html
29.92
KB
-rw-r--r--
profile.html
63.56
KB
-rw-r--r--
pty.html
9.48
KB
-rw-r--r--
pwd.html
11.43
KB
-rw-r--r--
py_compile.html
11.12
KB
-rw-r--r--
pyclbr.html
14.71
KB
-rw-r--r--
pydoc.html
11.48
KB
-rw-r--r--
pyexpat.html
71.53
KB
-rw-r--r--
python.html
12.27
KB
-rw-r--r--
queue.html
24.22
KB
-rw-r--r--
quopri.html
11.9
KB
-rw-r--r--
random.html
37.83
KB
-rw-r--r--
re.html
134.74
KB
-rw-r--r--
readline.html
28.24
KB
-rw-r--r--
repr.html
20.43
KB
-rw-r--r--
resource.html
26.48
KB
-rw-r--r--
restricted.html
11.65
KB
-rw-r--r--
rexec.html
37.41
KB
-rw-r--r--
rfc822.html
42.22
KB
-rw-r--r--
rlcompleter.html
13.51
KB
-rw-r--r--
robotparser.html
12.27
KB
-rw-r--r--
runpy.html
19.34
KB
-rw-r--r--
sched.html
18.54
KB
-rw-r--r--
scrolledtext.html
9.32
KB
-rw-r--r--
select.html
39.67
KB
-rw-r--r--
sets.html
36.92
KB
-rw-r--r--
sgi.html
9.71
KB
-rw-r--r--
sgmllib.html
30.77
KB
-rw-r--r--
sha.html
12.09
KB
-rw-r--r--
shelve.html
27.02
KB
-rw-r--r--
shlex.html
32.1
KB
-rw-r--r--
shutil.html
40.22
KB
-rw-r--r--
signal.html
31.14
KB
-rw-r--r--
simplehttpserver.html
18.41
KB
-rw-r--r--
simplexmlrpcserver.html
31.39
KB
-rw-r--r--
site.html
23.64
KB
-rw-r--r--
smtpd.html
12.46
KB
-rw-r--r--
smtplib.html
42.13
KB
-rw-r--r--
sndhdr.html
10.02
KB
-rw-r--r--
socket.html
106.34
KB
-rw-r--r--
socketserver.html
59.83
KB
-rw-r--r--
someos.html
15.11
KB
-rw-r--r--
spwd.html
10.33
KB
-rw-r--r--
sqlite3.html
139.5
KB
-rw-r--r--
ssl.html
65.62
KB
-rw-r--r--
stat.html
32.31
KB
-rw-r--r--
statvfs.html
10.6
KB
-rw-r--r--
stdtypes.html
260.4
KB
-rw-r--r--
string.html
106.65
KB
-rw-r--r--
stringio.html
18.81
KB
-rw-r--r--
stringprep.html
16.13
KB
-rw-r--r--
strings.html
14.93
KB
-rw-r--r--
struct.html
40.88
KB
-rw-r--r--
subprocess.html
84.91
KB
-rw-r--r--
sun.html
6.84
KB
-rw-r--r--
sunau.html
27.1
KB
-rw-r--r--
sunaudio.html
17.79
KB
-rw-r--r--
symbol.html
7.66
KB
-rw-r--r--
symtable.html
22.94
KB
-rw-r--r--
sys.html
98.7
KB
-rw-r--r--
sysconfig.html
23.84
KB
-rw-r--r--
syslog.html
17.92
KB
-rw-r--r--
tabnanny.html
10.63
KB
-rw-r--r--
tarfile.html
78.68
KB
-rw-r--r--
telnetlib.html
25.48
KB
-rw-r--r--
tempfile.html
29.42
KB
-rw-r--r--
termios.html
16.01
KB
-rw-r--r--
test.html
52.62
KB
-rw-r--r--
textwrap.html
27.25
KB
-rw-r--r--
thread.html
20.47
KB
-rw-r--r--
threading.html
76.69
KB
-rw-r--r--
time.html
56.93
KB
-rw-r--r--
timeit.html
36.27
KB
-rw-r--r--
tix.html
46.96
KB
-rw-r--r--
tk.html
23.64
KB
-rw-r--r--
tkinter.html
67.67
KB
-rw-r--r--
token.html
19.62
KB
-rw-r--r--
tokenize.html
18.45
KB
-rw-r--r--
trace.html
25.54
KB
-rw-r--r--
traceback.html
33.44
KB
-rw-r--r--
ttk.html
101.75
KB
-rw-r--r--
tty.html
9.06
KB
-rw-r--r--
turtle.html
211.74
KB
-rw-r--r--
types.html
27.59
KB
-rw-r--r--
undoc.html
23.16
KB
-rw-r--r--
unicodedata.html
18.55
KB
-rw-r--r--
unittest.html
202.85
KB
-rw-r--r--
unix.html
10.55
KB
-rw-r--r--
urllib.html
58.68
KB
-rw-r--r--
urllib2.html
100.58
KB
-rw-r--r--
urlparse.html
40.41
KB
-rw-r--r--
user.html
11.83
KB
-rw-r--r--
userdict.html
29.73
KB
-rw-r--r--
uu.html
11.03
KB
-rw-r--r--
uuid.html
28.19
KB
-rw-r--r--
warnings.html
46.6
KB
-rw-r--r--
wave.html
22.22
KB
-rw-r--r--
weakref.html
36.52
KB
-rw-r--r--
webbrowser.html
23.07
KB
-rw-r--r--
whichdb.html
8.85
KB
-rw-r--r--
windows.html
9.33
KB
-rw-r--r--
winsound.html
18.75
KB
-rw-r--r--
wsgiref.html
81.04
KB
-rw-r--r--
xdrlib.html
29.94
KB
-rw-r--r--
xml.dom.html
89.04
KB
-rw-r--r--
xml.dom.minidom.html
40.42
KB
-rw-r--r--
xml.dom.pulldom.html
12.71
KB
-rw-r--r--
xml.etree.elementtree.html
93.22
KB
-rw-r--r--
xml.html
16.49
KB
-rw-r--r--
xml.sax.handler.html
38.63
KB
-rw-r--r--
xml.sax.html
20.22
KB
-rw-r--r--
xml.sax.reader.html
39.09
KB
-rw-r--r--
xml.sax.utils.html
14.26
KB
-rw-r--r--
xmlrpclib.html
60.79
KB
-rw-r--r--
zipfile.html
53.14
KB
-rw-r--r--
zipimport.html
20.42
KB
-rw-r--r--
zlib.html
25.46
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : decimal.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>9.4. decimal — Decimal fixed point and floating point arithmetic — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="9. Numeric and Mathematical Modules" href="numeric.html" /> <link rel="next" title="9.5. fractions — Rational numbers" href="fractions.html" /> <link rel="prev" title="9.3. cmath — Mathematical functions for complex numbers" href="cmath.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="fractions.html" title="9.5. fractions — Rational numbers" accesskey="N">next</a> |</li> <li class="right" > <a href="cmath.html" title="9.3. cmath — Mathematical functions for complex numbers" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="numeric.html" accesskey="U">9. Numeric and Mathematical Modules</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-decimal"> <span id="decimal-decimal-fixed-point-and-floating-point-arithmetic"></span><h1>9.4. <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> — Decimal fixed point and floating point arithmetic<a class="headerlink" href="#module-decimal" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> <p>The <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module provides support for decimal floating point arithmetic. It offers several advantages over the <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> datatype:</p> <ul> <li><p class="first">Decimal “is based on a floating-point model which was designed with people in mind, and necessarily has a paramount guiding principle – computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school.” – excerpt from the decimal arithmetic specification.</p> </li> <li><p class="first">Decimal numbers can be represented exactly. In contrast, numbers like <tt class="xref py py-const docutils literal"><span class="pre">1.1</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">2.2</span></tt> do not have exact representations in binary floating point. End users typically would not expect <tt class="docutils literal"><span class="pre">1.1</span> <span class="pre">+</span> <span class="pre">2.2</span></tt> to display as <tt class="xref py py-const docutils literal"><span class="pre">3.3000000000000003</span></tt> as it does with binary floating point.</p> </li> <li><p class="first">The exactness carries over into arithmetic. In decimal floating point, <tt class="docutils literal"><span class="pre">0.1</span> <span class="pre">+</span> <span class="pre">0.1</span> <span class="pre">+</span> <span class="pre">0.1</span> <span class="pre">-</span> <span class="pre">0.3</span></tt> is exactly equal to zero. In binary floating point, the result is <tt class="xref py py-const docutils literal"><span class="pre">5.5511151231257827e-017</span></tt>. While near to zero, the differences prevent reliable equality testing and differences can accumulate. For this reason, decimal is preferred in accounting applications which have strict equality invariants.</p> </li> <li><p class="first">The decimal module incorporates a notion of significant places so that <tt class="docutils literal"><span class="pre">1.30</span> <span class="pre">+</span> <span class="pre">1.20</span></tt> is <tt class="xref py py-const docutils literal"><span class="pre">2.50</span></tt>. The trailing zero is kept to indicate significance. This is the customary presentation for monetary applications. For multiplication, the “schoolbook” approach uses all the figures in the multiplicands. For instance, <tt class="docutils literal"><span class="pre">1.3</span> <span class="pre">*</span> <span class="pre">1.2</span></tt> gives <tt class="xref py py-const docutils literal"><span class="pre">1.56</span></tt> while <tt class="docutils literal"><span class="pre">1.30</span> <span class="pre">*</span> <span class="pre">1.20</span></tt> gives <tt class="xref py py-const docutils literal"><span class="pre">1.5600</span></tt>.</p> </li> <li><p class="first">Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="o">*</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">6</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">Decimal('0.142857')</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">28</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">Decimal('0.1428571428571428571428571429')</span> </pre></div> </div> </li> <li><p class="first">Both binary and decimal floating point are implemented in terms of published standards. While the built-in float type exposes only a modest portion of its capabilities, the decimal module exposes all required parts of the standard. When needed, the programmer has full control over rounding and signal handling. This includes an option to enforce exact arithmetic by using exceptions to block any inexact operations.</p> </li> <li><p class="first">The decimal module was designed to support “without prejudice, both exact unrounded decimal arithmetic (sometimes called fixed-point arithmetic) and rounded floating-point arithmetic.” – excerpt from the decimal arithmetic specification.</p> </li> </ul> <p>The module design is centered around three concepts: the decimal number, the context for arithmetic, and signals.</p> <p>A decimal number is immutable. It has a sign, coefficient digits, and an exponent. To preserve significance, the coefficient digits do not truncate trailing zeros. Decimals also include special values such as <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>. The standard also differentiates <tt class="xref py py-const docutils literal"><span class="pre">-0</span></tt> from <tt class="xref py py-const docutils literal"><span class="pre">+0</span></tt>.</p> <p>The context for arithmetic is an environment specifying precision, rounding rules, limits on exponents, flags indicating the results of operations, and trap enablers which determine whether signals are treated as exceptions. Rounding options include <tt class="xref py py-const docutils literal"><span class="pre">ROUND_CEILING</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_DOWN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_FLOOR</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_DOWN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ROUND_UP</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></tt>.</p> <p>Signals are groups of exceptional conditions arising during the course of computation. Depending on the needs of the application, signals may be ignored, considered as informational, or treated as exceptions. The signals in the decimal module are: <a class="reference internal" href="#decimal.Clamped" title="decimal.Clamped"><tt class="xref py py-const docutils literal"><span class="pre">Clamped</span></tt></a>, <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt></a>, <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-const docutils literal"><span class="pre">DivisionByZero</span></tt></a>, <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a>, <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a>, <a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><tt class="xref py py-const docutils literal"><span class="pre">Subnormal</span></tt></a>, <a class="reference internal" href="#decimal.Overflow" title="decimal.Overflow"><tt class="xref py py-const docutils literal"><span class="pre">Overflow</span></tt></a>, and <a class="reference internal" href="#decimal.Underflow" title="decimal.Underflow"><tt class="xref py py-const docutils literal"><span class="pre">Underflow</span></tt></a>.</p> <p>For each signal there is a flag and a trap enabler. When a signal is encountered, its flag is set to one, then, if the trap enabler is set to one, an exception is raised. Flags are sticky, so the user needs to reset them before monitoring a calculation.</p> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <ul class="last simple"> <li>IBM’s General Decimal Arithmetic Specification, <a class="reference external" href="http://speleotrove.com/decimal/">The General Decimal Arithmetic Specification</a>.</li> <li>IEEE standard 854-1987, <a class="reference external" href="http://754r.ucbtest.org/standards/854.pdf">Unofficial IEEE 854 Text</a>.</li> </ul> </div> <div class="section" id="quick-start-tutorial"> <span id="decimal-tutorial"></span><h2>9.4.1. Quick-start Tutorial<a class="headerlink" href="#quick-start-tutorial" title="Permalink to this headline">¶</a></h2> <p>The usual start to using decimals is importing the module, viewing the current context with <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> and, if necessary, setting new values for precision, rounding, or enabled traps:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="o">*</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span> <span class="go">Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span> <span class="go"> capitals=1, flags=[], traps=[Overflow, DivisionByZero,</span> <span class="go"> InvalidOperation])</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">7</span> <span class="c"># Set a new precision</span> </pre></div> </div> <p>Decimal instances can be constructed from integers, strings, floats, or tuples. Construction from an integer or a float performs an exact conversion of the value of that integer or float. Decimal numbers include special values such as <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> which stands for “Not a number”, positive and negative <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">-0</span></tt>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">28</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="go">Decimal('10')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.14'</span><span class="p">)</span> <span class="go">Decimal('3.14')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mf">3.14</span><span class="p">)</span> <span class="go">Decimal('3.140000000000000124344978758017532527446746826171875')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="o">-</span><span class="mi">2</span><span class="p">))</span> <span class="go">Decimal('3.14')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="mf">2.0</span> <span class="o">**</span> <span class="mf">0.5</span><span class="p">))</span> <span class="go">Decimal('1.41421356237')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">**</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'0.5'</span><span class="p">)</span> <span class="go">Decimal('1.414213562373095048801688724')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'NaN'</span><span class="p">)</span> <span class="go">Decimal('NaN')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'-Infinity'</span><span class="p">)</span> <span class="go">Decimal('-Infinity')</span> </pre></div> </div> <p>The significance of a new Decimal is determined solely by the number of digits input. Context precision and rounding only come into play during arithmetic operations.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">6</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.0'</span><span class="p">)</span> <span class="go">Decimal('3.0')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.1415926535'</span><span class="p">)</span> <span class="go">Decimal('3.1415926535')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.1415926535'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'2.7182818285'</span><span class="p">)</span> <span class="go">Decimal('5.85987')</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">rounding</span> <span class="o">=</span> <span class="n">ROUND_UP</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.1415926535'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'2.7182818285'</span><span class="p">)</span> <span class="go">Decimal('5.85988')</span> </pre></div> </div> <p>Decimals interact well with much of the rest of Python. Here is a small decimal floating point flying circus:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">Decimal</span><span class="p">,</span> <span class="s">'1.34 1.87 3.45 2.35 1.00 0.03 9.25'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="gp">>>> </span><span class="nb">max</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="go">Decimal('9.25')</span> <span class="gp">>>> </span><span class="nb">min</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="go">Decimal('0.03')</span> <span class="gp">>>> </span><span class="nb">sorted</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="go">[Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'),</span> <span class="go"> Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]</span> <span class="gp">>>> </span><span class="nb">sum</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="go">Decimal('19.29')</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="go">'1.34'</span> <span class="gp">>>> </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="go">1.34</span> <span class="gp">>>> </span><span class="nb">round</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># round() first converts to binary floating point</span> <span class="go">1.3</span> <span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">*</span> <span class="mi">5</span> <span class="go">Decimal('6.70')</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span> <span class="go">Decimal('2.5058')</span> <span class="gp">>>> </span><span class="n">c</span> <span class="o">%</span> <span class="n">a</span> <span class="go">Decimal('0.77')</span> </pre></div> </div> <p>And some mathematical functions are also available to Decimal:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">28</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span> <span class="go">Decimal('1.414213562373095048801688724')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span> <span class="go">Decimal('2.718281828459045235360287471')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'10'</span><span class="p">)</span><span class="o">.</span><span class="n">ln</span><span class="p">()</span> <span class="go">Decimal('2.302585092994045684017991455')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'10'</span><span class="p">)</span><span class="o">.</span><span class="n">log10</span><span class="p">()</span> <span class="go">Decimal('1')</span> </pre></div> </div> <p>The <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> method rounds a number to a fixed exponent. This method is useful for monetary applications that often round results to a fixed number of places:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'7.325'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'.01'</span><span class="p">),</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_DOWN</span><span class="p">)</span> <span class="go">Decimal('7.32')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'7.325'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'1.'</span><span class="p">),</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_UP</span><span class="p">)</span> <span class="go">Decimal('8')</span> </pre></div> </div> <p>As shown above, the <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> function accesses the current context and allows the settings to be changed. This approach meets the needs of most applications.</p> <p>For more advanced work, it may be useful to create alternate contexts using the Context() constructor. To make an alternate active, use the <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a> function.</p> <p>In accordance with the standard, the <tt class="xref py py-mod docutils literal"><span class="pre">Decimal</span></tt> module provides two ready to use standard contexts, <a class="reference internal" href="#decimal.BasicContext" title="decimal.BasicContext"><tt class="xref py py-const docutils literal"><span class="pre">BasicContext</span></tt></a> and <a class="reference internal" href="#decimal.ExtendedContext" title="decimal.ExtendedContext"><tt class="xref py py-const docutils literal"><span class="pre">ExtendedContext</span></tt></a>. The former is especially useful for debugging because many of the traps are enabled:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">myothercontext</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_HALF_DOWN</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">myothercontext</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">Decimal('0.142857142857142857142857142857142857142857142857142857142857')</span> <span class="gp">>>> </span><span class="n">ExtendedContext</span> <span class="go">Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span> <span class="go"> capitals=1, flags=[], traps=[])</span> <span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="go">Decimal('0.142857143')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="go">Decimal('Infinity')</span> <span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">BasicContext</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<pyshell#143>"</span>, line <span class="m">1</span>, in <span class="n">-toplevel-</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gr">DivisionByZero</span>: <span class="n">x / 0</span> </pre></div> </div> <p>Contexts also have signal flags for monitoring exceptional conditions encountered during computations. The flags remain set until explicitly cleared, so it is best to clear the flags before each set of monitored computations by using the <tt class="xref py py-meth docutils literal"><span class="pre">clear_flags()</span></tt> method.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">clear_flags</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">355</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">113</span><span class="p">)</span> <span class="go">Decimal('3.14159292')</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span> <span class="go">Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,</span> <span class="go"> capitals=1, flags=[Rounded, Inexact], traps=[])</span> </pre></div> </div> <p>The <em>flags</em> entry shows that the rational approximation to <tt class="xref py py-const docutils literal"><span class="pre">Pi</span></tt> was rounded (digits beyond the context precision were thrown away) and that the result is inexact (some of the discarded digits were non-zero).</p> <p>Individual traps are set using the dictionary in the <tt class="xref py py-attr docutils literal"><span class="pre">traps</span></tt> field of a context:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">setcontext</span><span class="p">(</span><span class="n">ExtendedContext</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="go">Decimal('Infinity')</span> <span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">traps</span><span class="p">[</span><span class="n">DivisionByZero</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<pyshell#112>"</span>, line <span class="m">1</span>, in <span class="n">-toplevel-</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="gr">DivisionByZero</span>: <span class="n">x / 0</span> </pre></div> </div> <p>Most programs adjust the current context only once, at the beginning of the program. And, in many applications, data is converted to <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> with a single cast inside a loop. With context set and decimals created, the bulk of the program manipulates the data no differently than with other Python numeric types.</p> </div> <div class="section" id="decimal-objects"> <span id="decimal-decimal"></span><h2>9.4.2. Decimal objects<a class="headerlink" href="#decimal-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="decimal.Decimal"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Decimal</tt><big>(</big><span class="optional">[</span><em>value</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal" title="Permalink to this definition">¶</a></dt> <dd><p>Construct a new <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> object based from <em>value</em>.</p> <p><em>value</em> can be an integer, string, tuple, <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>, or another <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> object. If no <em>value</em> is given, returns <tt class="docutils literal"><span class="pre">Decimal('0')</span></tt>. If <em>value</em> is a string, it should conform to the decimal numeric string syntax after leading and trailing whitespace characters are removed:</p> <div class="highlight-python"><pre>sign ::= '+' | '-' digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' indicator ::= 'e' | 'E' digits ::= digit [digit]... decimal-part ::= digits '.' [digits] | ['.'] digits exponent-part ::= indicator [sign] digits infinity ::= 'Infinity' | 'Inf' nan ::= 'NaN' [digits] | 'sNaN' [digits] numeric-value ::= decimal-part [exponent-part] | infinity numeric-string ::= [sign] numeric-value | [sign] nan</pre> </div> <p>If <em>value</em> is a unicode string then other Unicode decimal digits are also permitted where <tt class="docutils literal"><span class="pre">digit</span></tt> appears above. These include decimal digits from various other alphabets (for example, Arabic-Indic and Devanāgarī digits) along with the fullwidth digits <tt class="docutils literal"><span class="pre">u'\uff10'</span></tt> through <tt class="docutils literal"><span class="pre">u'\uff19'</span></tt>.</p> <p>If <em>value</em> is a <a class="reference internal" href="functions.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a>, it should have three components, a sign (<tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> for positive or <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt> for negative), a <a class="reference internal" href="functions.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a> of digits, and an integer exponent. For example, <tt class="docutils literal"><span class="pre">Decimal((0,</span> <span class="pre">(1,</span> <span class="pre">4,</span> <span class="pre">1,</span> <span class="pre">4),</span> <span class="pre">-3))</span></tt> returns <tt class="docutils literal"><span class="pre">Decimal('1.414')</span></tt>.</p> <p>If <em>value</em> is a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>, the binary floating point value is losslessly converted to its exact decimal equivalent. This conversion can often require 53 or more digits of precision. For example, <tt class="docutils literal"><span class="pre">Decimal(float('1.1'))</span></tt> converts to <tt class="docutils literal"><span class="pre">Decimal('1.100000000000000088817841970012523233890533447265625')</span></tt>.</p> <p>The <em>context</em> precision does not affect how many digits are stored. That is determined exclusively by the number of digits in <em>value</em>. For example, <tt class="docutils literal"><span class="pre">Decimal('3.00000')</span></tt> records all five zeros even if the context precision is only three.</p> <p>The purpose of the <em>context</em> argument is determining what to do if <em>value</em> is a malformed string. If the context traps <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt></a>, an exception is raised; otherwise, the constructor returns a new Decimal with the value of <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>.</p> <p>Once constructed, <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> objects are immutable.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>leading and trailing whitespace characters are permitted when creating a Decimal instance from a string.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.7: </span>The argument to the constructor is now permitted to be a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> instance.</p> <p>Decimal floating point objects share many properties with the other built-in numeric types such as <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> and <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>. All of the usual math operations and special methods apply. Likewise, decimal objects can be copied, pickled, printed, used as dictionary keys, used as set elements, compared, sorted, and coerced to another type (such as <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> or <a class="reference internal" href="functions.html#long" title="long"><tt class="xref py py-class docutils literal"><span class="pre">long</span></tt></a>).</p> <p>There are some small differences between arithmetic on Decimal objects and arithmetic on integers and floats. When the remainder operator <tt class="docutils literal"><span class="pre">%</span></tt> is applied to Decimal objects, the sign of the result is the sign of the <em>dividend</em> rather than the sign of the divisor:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">)</span> <span class="o">%</span> <span class="mi">4</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">)</span> <span class="o">%</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="go">Decimal('-3')</span> </pre></div> </div> <p>The integer division operator <tt class="docutils literal"><span class="pre">//</span></tt> behaves analogously, returning the integer part of the true quotient (truncating towards zero) rather than its floor, so as to preserve the usual identity <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">(x</span> <span class="pre">//</span> <span class="pre">y)</span> <span class="pre">*</span> <span class="pre">y</span> <span class="pre">+</span> <span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="o">-</span><span class="mi">7</span> <span class="o">//</span> <span class="mi">4</span> <span class="go">-2</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">7</span><span class="p">)</span> <span class="o">//</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="go">Decimal('-1')</span> </pre></div> </div> <p>The <tt class="docutils literal"><span class="pre">%</span></tt> and <tt class="docutils literal"><span class="pre">//</span></tt> operators implement the <tt class="docutils literal"><span class="pre">remainder</span></tt> and <tt class="docutils literal"><span class="pre">divide-integer</span></tt> operations (respectively) as described in the specification.</p> <p>Decimal objects cannot generally be combined with floats in arithmetic operations: an attempt to add a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> to a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>, for example, will raise a <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>. There’s one exception to this rule: it’s possible to use Python’s comparison operators to compare a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> instance <tt class="docutils literal"><span class="pre">x</span></tt> with a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance <tt class="docutils literal"><span class="pre">y</span></tt>. Without this exception, comparisons between <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> and <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> instances would follow the general rules for comparing objects of different types described in the <a class="reference internal" href="../reference/expressions.html#expressions"><em>Expressions</em></a> section of the reference manual, leading to confusing results.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.7: </span>A comparison between a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> instance <tt class="docutils literal"><span class="pre">x</span></tt> and a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance <tt class="docutils literal"><span class="pre">y</span></tt> now returns a result based on the values of <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt>. In earlier versions <tt class="docutils literal"><span class="pre">x</span> <span class="pre"><</span> <span class="pre">y</span></tt> returned the same (arbitrary) result for any <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance <tt class="docutils literal"><span class="pre">x</span></tt> and any <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> instance <tt class="docutils literal"><span class="pre">y</span></tt>.</p> <p>In addition to the standard numeric properties, decimal floating point objects also have a number of specialized methods:</p> <dl class="method"> <dt id="decimal.Decimal.adjusted"> <tt class="descname">adjusted</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.adjusted" title="Permalink to this definition">¶</a></dt> <dd><p>Return the adjusted exponent after shifting out the coefficient’s rightmost digits until only the lead digit remains: <tt class="docutils literal"><span class="pre">Decimal('321e+5').adjusted()</span></tt> returns seven. Used for determining the position of the most significant digit with respect to the decimal point.</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.as_tuple"> <tt class="descname">as_tuple</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.as_tuple" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="../glossary.html#term-named-tuple"><em class="xref std std-term">named tuple</em></a> representation of the number: <tt class="docutils literal"><span class="pre">DecimalTuple(sign,</span> <span class="pre">digits,</span> <span class="pre">exponent)</span></tt>.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>Use a named tuple.</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.canonical"> <tt class="descname">canonical</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.canonical" title="Permalink to this definition">¶</a></dt> <dd><p>Return the canonical encoding of the argument. Currently, the encoding of a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance is always canonical, so this operation returns its argument unchanged.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.compare"> <tt class="descname">compare</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.compare" title="Permalink to this definition">¶</a></dt> <dd><p>Compare the values of two Decimal instances. This operation behaves in the same way as the usual comparison method <a class="reference internal" href="../reference/datamodel.html#object.__cmp__" title="object.__cmp__"><tt class="xref py py-meth docutils literal"><span class="pre">__cmp__()</span></tt></a>, except that <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> returns a Decimal instance rather than an integer, and if either operand is a NaN then the result is a NaN:</p> <div class="highlight-python"><pre>a or b is a NaN ==> Decimal('NaN') a < b ==> Decimal('-1') a == b ==> Decimal('0') a > b ==> Decimal('1')</pre> </div> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.compare_signal"> <tt class="descname">compare_signal</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.compare_signal" title="Permalink to this definition">¶</a></dt> <dd><p>This operation is identical to the <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> method, except that all NaNs signal. That is, if neither operand is a signaling NaN then any quiet NaN operand is treated as though it were a signaling NaN.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.compare_total"> <tt class="descname">compare_total</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#decimal.Decimal.compare_total" title="Permalink to this definition">¶</a></dt> <dd><p>Compare two operands using their abstract representation rather than their numerical value. Similar to the <a class="reference internal" href="#decimal.Decimal.compare" title="decimal.Decimal.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> method, but the result gives a total ordering on <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instances. Two <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instances with the same numeric value but different representations compare unequal in this ordering:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'12.0'</span><span class="p">)</span><span class="o">.</span><span class="n">compare_total</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'12'</span><span class="p">))</span> <span class="go">Decimal('-1')</span> </pre></div> </div> <p>Quiet and signaling NaNs are also included in the total ordering. The result of this function is <tt class="docutils literal"><span class="pre">Decimal('0')</span></tt> if both operands have the same representation, <tt class="docutils literal"><span class="pre">Decimal('-1')</span></tt> if the first operand is lower in the total order than the second, and <tt class="docutils literal"><span class="pre">Decimal('1')</span></tt> if the first operand is higher in the total order than the second operand. See the specification for details of the total order.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.compare_total_mag"> <tt class="descname">compare_total_mag</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#decimal.Decimal.compare_total_mag" title="Permalink to this definition">¶</a></dt> <dd><p>Compare two operands using their abstract representation rather than their value as in <a class="reference internal" href="#decimal.Decimal.compare_total" title="decimal.Decimal.compare_total"><tt class="xref py py-meth docutils literal"><span class="pre">compare_total()</span></tt></a>, but ignoring the sign of each operand. <tt class="docutils literal"><span class="pre">x.compare_total_mag(y)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">x.copy_abs().compare_total(y.copy_abs())</span></tt>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.conjugate"> <tt class="descname">conjugate</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.conjugate" title="Permalink to this definition">¶</a></dt> <dd><p>Just returns self, this method is only to comply with the Decimal Specification.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.copy_abs"> <tt class="descname">copy_abs</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.copy_abs" title="Permalink to this definition">¶</a></dt> <dd><p>Return the absolute value of the argument. This operation is unaffected by the context and is quiet: no flags are changed and no rounding is performed.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.copy_negate"> <tt class="descname">copy_negate</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.copy_negate" title="Permalink to this definition">¶</a></dt> <dd><p>Return the negation of the argument. This operation is unaffected by the context and is quiet: no flags are changed and no rounding is performed.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.copy_sign"> <tt class="descname">copy_sign</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#decimal.Decimal.copy_sign" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of the first operand with the sign set to be the same as the sign of the second operand. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'2.3'</span><span class="p">)</span><span class="o">.</span><span class="n">copy_sign</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'-1.5'</span><span class="p">))</span> <span class="go">Decimal('-2.3')</span> </pre></div> </div> <p>This operation is unaffected by the context and is quiet: no flags are changed and no rounding is performed.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.exp"> <tt class="descname">exp</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.exp" title="Permalink to this definition">¶</a></dt> <dd><p>Return the value of the (natural) exponential function <tt class="docutils literal"><span class="pre">e**x</span></tt> at the given number. The result is correctly rounded using the <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> rounding mode.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span> <span class="go">Decimal('2.718281828459045235360287471')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">321</span><span class="p">)</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span> <span class="go">Decimal('2.561702493119680037517373933E+139')</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.from_float"> <tt class="descname">from_float</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#decimal.Decimal.from_float" title="Permalink to this definition">¶</a></dt> <dd><p>Classmethod that converts a float to a decimal number, exactly.</p> <p>Note <cite>Decimal.from_float(0.1)</cite> is not the same as <cite>Decimal(‘0.1’)</cite>. Since 0.1 is not exactly representable in binary floating point, the value is stored as the nearest representable value which is <cite>0x1.999999999999ap-4</cite>. That equivalent value in decimal is <cite>0.1000000000000000055511151231257827021181583404541015625</cite>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">From Python 2.7 onwards, a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance can also be constructed directly from a <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>.</p> </div> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span> <span class="go">Decimal('0.1000000000000000055511151231257827021181583404541015625')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s">'nan'</span><span class="p">))</span> <span class="go">Decimal('NaN')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s">'inf'</span><span class="p">))</span> <span class="go">Decimal('Infinity')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s">'-inf'</span><span class="p">))</span> <span class="go">Decimal('-Infinity')</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.fma"> <tt class="descname">fma</tt><big>(</big><em>other</em>, <em>third</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.fma" title="Permalink to this definition">¶</a></dt> <dd><p>Fused multiply-add. Return self*other+third with no rounding of the intermediate product self*other.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">fma</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="go">Decimal('11')</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_canonical"> <tt class="descname">is_canonical</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_canonical" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is canonical and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise. Currently, a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance is always canonical, so this operation always returns <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_finite"> <tt class="descname">is_finite</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_finite" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is a finite number, and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> if the argument is an infinity or a NaN.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_infinite"> <tt class="descname">is_infinite</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_infinite" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is either positive or negative infinity and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_nan"> <tt class="descname">is_nan</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_nan" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is a (quiet or signaling) NaN and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_normal"> <tt class="descname">is_normal</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_normal" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is a <em>normal</em> finite non-zero number with an adjusted exponent greater than or equal to <em>Emin</em>. Return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> if the argument is zero, subnormal, infinite or a NaN. Note, the term <em>normal</em> is used here in a different sense with the <a class="reference internal" href="#decimal.Decimal.normalize" title="decimal.Decimal.normalize"><tt class="xref py py-meth docutils literal"><span class="pre">normalize()</span></tt></a> method which is used to create canonical values.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_qnan"> <tt class="descname">is_qnan</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_qnan" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is a quiet NaN, and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_signed"> <tt class="descname">is_signed</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_signed" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument has a negative sign and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise. Note that zeros and NaNs can both carry signs.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_snan"> <tt class="descname">is_snan</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_snan" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is a signaling NaN and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_subnormal"> <tt class="descname">is_subnormal</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_subnormal" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is subnormal, and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise. A number is subnormal is if it is nonzero, finite, and has an adjusted exponent less than <em>Emin</em>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.is_zero"> <tt class="descname">is_zero</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.is_zero" title="Permalink to this definition">¶</a></dt> <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the argument is a (positive or negative) zero and <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> otherwise.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.ln"> <tt class="descname">ln</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.ln" title="Permalink to this definition">¶</a></dt> <dd><p>Return the natural (base e) logarithm of the operand. The result is correctly rounded using the <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> rounding mode.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.log10"> <tt class="descname">log10</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.log10" title="Permalink to this definition">¶</a></dt> <dd><p>Return the base ten logarithm of the operand. The result is correctly rounded using the <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> rounding mode.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.logb"> <tt class="descname">logb</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logb" title="Permalink to this definition">¶</a></dt> <dd><p>For a nonzero number, return the adjusted exponent of its operand as a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance. If the operand is a zero then <tt class="docutils literal"><span class="pre">Decimal('-Infinity')</span></tt> is returned and the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-const docutils literal"><span class="pre">DivisionByZero</span></tt></a> flag is raised. If the operand is an infinity then <tt class="docutils literal"><span class="pre">Decimal('Infinity')</span></tt> is returned.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.logical_and"> <tt class="descname">logical_and</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_and" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#decimal.Decimal.logical_and" title="decimal.Decimal.logical_and"><tt class="xref py py-meth docutils literal"><span class="pre">logical_and()</span></tt></a> is a logical operation which takes two <em>logical operands</em> (see <a class="reference internal" href="#logical-operands-label"><em>Logical operands</em></a>). The result is the digit-wise <tt class="docutils literal"><span class="pre">and</span></tt> of the two operands.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.logical_invert"> <tt class="descname">logical_invert</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_invert" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#decimal.Decimal.logical_invert" title="decimal.Decimal.logical_invert"><tt class="xref py py-meth docutils literal"><span class="pre">logical_invert()</span></tt></a> is a logical operation. The result is the digit-wise inversion of the operand.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.logical_or"> <tt class="descname">logical_or</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_or" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#decimal.Decimal.logical_or" title="decimal.Decimal.logical_or"><tt class="xref py py-meth docutils literal"><span class="pre">logical_or()</span></tt></a> is a logical operation which takes two <em>logical operands</em> (see <a class="reference internal" href="#logical-operands-label"><em>Logical operands</em></a>). The result is the digit-wise <tt class="docutils literal"><span class="pre">or</span></tt> of the two operands.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.logical_xor"> <tt class="descname">logical_xor</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.logical_xor" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#decimal.Decimal.logical_xor" title="decimal.Decimal.logical_xor"><tt class="xref py py-meth docutils literal"><span class="pre">logical_xor()</span></tt></a> is a logical operation which takes two <em>logical operands</em> (see <a class="reference internal" href="#logical-operands-label"><em>Logical operands</em></a>). The result is the digit-wise exclusive or of the two operands.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.max"> <tt class="descname">max</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.max" title="Permalink to this definition">¶</a></dt> <dd><p>Like <tt class="docutils literal"><span class="pre">max(self,</span> <span class="pre">other)</span></tt> except that the context rounding rule is applied before returning and that <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> values are either signaled or ignored (depending on the context and whether they are signaling or quiet).</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.max_mag"> <tt class="descname">max_mag</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.max_mag" title="Permalink to this definition">¶</a></dt> <dd><p>Similar to the <a class="reference internal" href="#decimal.Decimal.max" title="decimal.Decimal.max"><tt class="xref py py-meth docutils literal"><span class="pre">max()</span></tt></a> method, but the comparison is done using the absolute values of the operands.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.min"> <tt class="descname">min</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.min" title="Permalink to this definition">¶</a></dt> <dd><p>Like <tt class="docutils literal"><span class="pre">min(self,</span> <span class="pre">other)</span></tt> except that the context rounding rule is applied before returning and that <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> values are either signaled or ignored (depending on the context and whether they are signaling or quiet).</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.min_mag"> <tt class="descname">min_mag</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.min_mag" title="Permalink to this definition">¶</a></dt> <dd><p>Similar to the <a class="reference internal" href="#decimal.Decimal.min" title="decimal.Decimal.min"><tt class="xref py py-meth docutils literal"><span class="pre">min()</span></tt></a> method, but the comparison is done using the absolute values of the operands.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.next_minus"> <tt class="descname">next_minus</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.next_minus" title="Permalink to this definition">¶</a></dt> <dd><p>Return the largest number representable in the given context (or in the current thread’s context if no context is given) that is smaller than the given operand.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.next_plus"> <tt class="descname">next_plus</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.next_plus" title="Permalink to this definition">¶</a></dt> <dd><p>Return the smallest number representable in the given context (or in the current thread’s context if no context is given) that is larger than the given operand.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.next_toward"> <tt class="descname">next_toward</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.next_toward" title="Permalink to this definition">¶</a></dt> <dd><p>If the two operands are unequal, return the number closest to the first operand in the direction of the second operand. If both operands are numerically equal, return a copy of the first operand with the sign set to be the same as the sign of the second operand.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.normalize"> <tt class="descname">normalize</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.normalize" title="Permalink to this definition">¶</a></dt> <dd><p>Normalize the number by stripping the rightmost trailing zeros and converting any result equal to <tt class="xref py py-const docutils literal"><span class="pre">Decimal('0')</span></tt> to <tt class="xref py py-const docutils literal"><span class="pre">Decimal('0e0')</span></tt>. Used for producing canonical values for attributes of an equivalence class. For example, <tt class="docutils literal"><span class="pre">Decimal('32.100')</span></tt> and <tt class="docutils literal"><span class="pre">Decimal('0.321000e+2')</span></tt> both normalize to the equivalent value <tt class="docutils literal"><span class="pre">Decimal('32.1')</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.number_class"> <tt class="descname">number_class</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.number_class" title="Permalink to this definition">¶</a></dt> <dd><p>Return a string describing the <em>class</em> of the operand. The returned value is one of the following ten strings.</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">"-Infinity"</span></tt>, indicating that the operand is negative infinity.</li> <li><tt class="docutils literal"><span class="pre">"-Normal"</span></tt>, indicating that the operand is a negative normal number.</li> <li><tt class="docutils literal"><span class="pre">"-Subnormal"</span></tt>, indicating that the operand is negative and subnormal.</li> <li><tt class="docutils literal"><span class="pre">"-Zero"</span></tt>, indicating that the operand is a negative zero.</li> <li><tt class="docutils literal"><span class="pre">"+Zero"</span></tt>, indicating that the operand is a positive zero.</li> <li><tt class="docutils literal"><span class="pre">"+Subnormal"</span></tt>, indicating that the operand is positive and subnormal.</li> <li><tt class="docutils literal"><span class="pre">"+Normal"</span></tt>, indicating that the operand is a positive normal number.</li> <li><tt class="docutils literal"><span class="pre">"+Infinity"</span></tt>, indicating that the operand is positive infinity.</li> <li><tt class="docutils literal"><span class="pre">"NaN"</span></tt>, indicating that the operand is a quiet NaN (Not a Number).</li> <li><tt class="docutils literal"><span class="pre">"sNaN"</span></tt>, indicating that the operand is a signaling NaN.</li> </ul> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.quantize"> <tt class="descname">quantize</tt><big>(</big><em>exp</em><span class="optional">[</span>, <em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">[</span>, <em>watchexp</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.quantize" title="Permalink to this definition">¶</a></dt> <dd><p>Return a value equal to the first operand after rounding and having the exponent of the second operand.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'1.41421356'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'1.000'</span><span class="p">))</span> <span class="go">Decimal('1.414')</span> </pre></div> </div> <p>Unlike other operations, if the length of the coefficient after the quantize operation would be greater than precision, then an <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt></a> is signaled. This guarantees that, unless there is an error condition, the quantized exponent is always equal to that of the right-hand operand.</p> <p>Also unlike other operations, quantize never signals Underflow, even if the result is subnormal and inexact.</p> <p>If the exponent of the second operand is larger than that of the first then rounding may be necessary. In this case, the rounding mode is determined by the <tt class="docutils literal"><span class="pre">rounding</span></tt> argument if given, else by the given <tt class="docutils literal"><span class="pre">context</span></tt> argument; if neither argument is given the rounding mode of the current thread’s context is used.</p> <p>If <em>watchexp</em> is set (default), then an error is returned whenever the resulting exponent is greater than <tt class="xref py py-attr docutils literal"><span class="pre">Emax</span></tt> or less than <tt class="xref py py-attr docutils literal"><span class="pre">Etiny</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.radix"> <tt class="descname">radix</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Decimal.radix" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">Decimal(10)</span></tt>, the radix (base) in which the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> class does all its arithmetic. Included for compatibility with the specification.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.remainder_near"> <tt class="descname">remainder_near</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.remainder_near" title="Permalink to this definition">¶</a></dt> <dd><p>Return the remainder from dividing <em>self</em> by <em>other</em>. This differs from <tt class="docutils literal"><span class="pre">self</span> <span class="pre">%</span> <span class="pre">other</span></tt> in that the sign of the remainder is chosen so as to minimize its absolute value. More precisely, the return value is <tt class="docutils literal"><span class="pre">self</span> <span class="pre">-</span> <span class="pre">n</span> <span class="pre">*</span> <span class="pre">other</span></tt> where <tt class="docutils literal"><span class="pre">n</span></tt> is the integer nearest to the exact value of <tt class="docutils literal"><span class="pre">self</span> <span class="pre">/</span> <span class="pre">other</span></tt>, and if two integers are equally near then the even one is chosen.</p> <p>If the result is zero then its sign will be the sign of <em>self</em>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">18</span><span class="p">)</span><span class="o">.</span><span class="n">remainder_near</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span> <span class="go">Decimal('-2')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span><span class="o">.</span><span class="n">remainder_near</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span> <span class="go">Decimal('5')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">35</span><span class="p">)</span><span class="o">.</span><span class="n">remainder_near</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span> <span class="go">Decimal('-5')</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.rotate"> <tt class="descname">rotate</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.rotate" title="Permalink to this definition">¶</a></dt> <dd><p>Return the result of rotating the digits of the first operand by an amount specified by the second operand. The second operand must be an integer in the range -precision through precision. The absolute value of the second operand gives the number of places to rotate. If the second operand is positive then rotation is to the left; otherwise rotation is to the right. The coefficient of the first operand is padded on the left with zeros to length precision if necessary. The sign and exponent of the first operand are unchanged.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.same_quantum"> <tt class="descname">same_quantum</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.same_quantum" title="Permalink to this definition">¶</a></dt> <dd><p>Test whether self and other have the same exponent or whether both are <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.scaleb"> <tt class="descname">scaleb</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.scaleb" title="Permalink to this definition">¶</a></dt> <dd><p>Return the first operand with exponent adjusted by the second. Equivalently, return the first operand multiplied by <tt class="docutils literal"><span class="pre">10**other</span></tt>. The second operand must be an integer.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.shift"> <tt class="descname">shift</tt><big>(</big><em>other</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.shift" title="Permalink to this definition">¶</a></dt> <dd><p>Return the result of shifting the digits of the first operand by an amount specified by the second operand. The second operand must be an integer in the range -precision through precision. The absolute value of the second operand gives the number of places to shift. If the second operand is positive then the shift is to the left; otherwise the shift is to the right. Digits shifted into the coefficient are zeros. The sign and exponent of the first operand are unchanged.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.sqrt"> <tt class="descname">sqrt</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.sqrt" title="Permalink to this definition">¶</a></dt> <dd><p>Return the square root of the argument to full precision.</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.to_eng_string"> <tt class="descname">to_eng_string</tt><big>(</big><span class="optional">[</span><em>context</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_eng_string" title="Permalink to this definition">¶</a></dt> <dd><p>Convert to an engineering-type string.</p> <p>Engineering notation has an exponent which is a multiple of 3, so there are up to 3 digits left of the decimal place. For example, converts <tt class="docutils literal"><span class="pre">Decimal('123E+1')</span></tt> to <tt class="docutils literal"><span class="pre">Decimal('1.23E+3')</span></tt></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.to_integral"> <tt class="descname">to_integral</tt><big>(</big><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_integral" title="Permalink to this definition">¶</a></dt> <dd><p>Identical to the <a class="reference internal" href="#decimal.Decimal.to_integral_value" title="decimal.Decimal.to_integral_value"><tt class="xref py py-meth docutils literal"><span class="pre">to_integral_value()</span></tt></a> method. The <tt class="docutils literal"><span class="pre">to_integral</span></tt> name has been kept for compatibility with older versions.</p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.to_integral_exact"> <tt class="descname">to_integral_exact</tt><big>(</big><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_integral_exact" title="Permalink to this definition">¶</a></dt> <dd><p>Round to the nearest integer, signaling <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a> or <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a> as appropriate if rounding occurs. The rounding mode is determined by the <tt class="docutils literal"><span class="pre">rounding</span></tt> parameter if given, else by the given <tt class="docutils literal"><span class="pre">context</span></tt>. If neither parameter is given then the rounding mode of the current context is used.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Decimal.to_integral_value"> <tt class="descname">to_integral_value</tt><big>(</big><span class="optional">[</span><em>rounding</em><span class="optional">[</span>, <em>context</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Decimal.to_integral_value" title="Permalink to this definition">¶</a></dt> <dd><p>Round to the nearest integer without signaling <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a> or <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a>. If given, applies <em>rounding</em>; otherwise, uses the rounding method in either the supplied <em>context</em> or the current context.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>renamed from <tt class="docutils literal"><span class="pre">to_integral</span></tt> to <tt class="docutils literal"><span class="pre">to_integral_value</span></tt>. The old name remains valid for compatibility.</p> </dd></dl> </dd></dl> <div class="section" id="logical-operands"> <span id="logical-operands-label"></span><h3>9.4.2.1. Logical operands<a class="headerlink" href="#logical-operands" title="Permalink to this headline">¶</a></h3> <p>The <tt class="xref py py-meth docutils literal"><span class="pre">logical_and()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">logical_invert()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">logical_or()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">logical_xor()</span></tt> methods expect their arguments to be <em>logical operands</em>. A <em>logical operand</em> is a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance whose exponent and sign are both zero, and whose digits are all either <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt>.</p> </div> </div> <div class="section" id="context-objects"> <span id="decimal-context"></span><h2>9.4.3. Context objects<a class="headerlink" href="#context-objects" title="Permalink to this headline">¶</a></h2> <p>Contexts are environments for arithmetic operations. They govern precision, set rules for rounding, determine which signals are treated as exceptions, and limit the range for exponents.</p> <p>Each thread has its own current context which is accessed or changed using the <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> and <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a> functions:</p> <dl class="function"> <dt id="decimal.getcontext"> <tt class="descclassname">decimal.</tt><tt class="descname">getcontext</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.getcontext" title="Permalink to this definition">¶</a></dt> <dd><p>Return the current context for the active thread.</p> </dd></dl> <dl class="function"> <dt id="decimal.setcontext"> <tt class="descclassname">decimal.</tt><tt class="descname">setcontext</tt><big>(</big><em>c</em><big>)</big><a class="headerlink" href="#decimal.setcontext" title="Permalink to this definition">¶</a></dt> <dd><p>Set the current context for the active thread to <em>c</em>.</p> </dd></dl> <p>Beginning with Python 2.5, you can also use the <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement and the <a class="reference internal" href="#decimal.localcontext" title="decimal.localcontext"><tt class="xref py py-func docutils literal"><span class="pre">localcontext()</span></tt></a> function to temporarily change the active context.</p> <dl class="function"> <dt id="decimal.localcontext"> <tt class="descclassname">decimal.</tt><tt class="descname">localcontext</tt><big>(</big><span class="optional">[</span><em>c</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.localcontext" title="Permalink to this definition">¶</a></dt> <dd><p>Return a context manager that will set the current context for the active thread to a copy of <em>c</em> on entry to the with-statement and restore the previous context when exiting the with-statement. If no context is specified, a copy of the current context is used.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> <p>For example, the following code sets the current decimal precision to 42 places, performs a calculation, and then automatically restores the previous context:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="n">localcontext</span> <span class="k">with</span> <span class="n">localcontext</span><span class="p">()</span> <span class="k">as</span> <span class="n">ctx</span><span class="p">:</span> <span class="n">ctx</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">42</span> <span class="c"># Perform a high precision calculation</span> <span class="n">s</span> <span class="o">=</span> <span class="n">calculate_something</span><span class="p">()</span> <span class="n">s</span> <span class="o">=</span> <span class="o">+</span><span class="n">s</span> <span class="c"># Round the final result back to the default precision</span> <span class="k">with</span> <span class="n">localcontext</span><span class="p">(</span><span class="n">BasicContext</span><span class="p">):</span> <span class="c"># temporarily use the BasicContext</span> <span class="k">print</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="k">print</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">355</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">113</span><span class="p">)</span> </pre></div> </div> </dd></dl> <p>New contexts can also be created using the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor described below. In addition, the module provides three pre-made contexts:</p> <dl class="class"> <dt id="decimal.BasicContext"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">BasicContext</tt><a class="headerlink" href="#decimal.BasicContext" title="Permalink to this definition">¶</a></dt> <dd><p>This is a standard context defined by the General Decimal Arithmetic Specification. Precision is set to nine. Rounding is set to <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></tt>. All flags are cleared. All traps are enabled (treated as exceptions) except <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a>, <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-const docutils literal"><span class="pre">Rounded</span></tt></a>, and <a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><tt class="xref py py-const docutils literal"><span class="pre">Subnormal</span></tt></a>.</p> <p>Because many of the traps are enabled, this context is useful for debugging.</p> </dd></dl> <dl class="class"> <dt id="decimal.ExtendedContext"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">ExtendedContext</tt><a class="headerlink" href="#decimal.ExtendedContext" title="Permalink to this definition">¶</a></dt> <dd><p>This is a standard context defined by the General Decimal Arithmetic Specification. Precision is set to nine. Rounding is set to <tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt>. All flags are cleared. No traps are enabled (so that exceptions are not raised during computations).</p> <p>Because the traps are disabled, this context is useful for applications that prefer to have result value of <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt> instead of raising exceptions. This allows an application to complete a run in the presence of conditions that would otherwise halt the program.</p> </dd></dl> <dl class="class"> <dt id="decimal.DefaultContext"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">DefaultContext</tt><a class="headerlink" href="#decimal.DefaultContext" title="Permalink to this definition">¶</a></dt> <dd><p>This context is used by the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor as a prototype for new contexts. Changing a field (such a precision) has the effect of changing the default for new contexts created by the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor.</p> <p>This context is most useful in multi-threaded environments. Changing one of the fields before threads are started has the effect of setting system-wide defaults. Changing the fields after threads have started is not recommended as it would require thread synchronization to prevent race conditions.</p> <p>In single threaded environments, it is preferable to not use this context at all. Instead, simply create contexts explicitly as described below.</p> <p>The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled traps for Overflow, InvalidOperation, and DivisionByZero.</p> </dd></dl> <p>In addition to the three supplied contexts, new contexts can be created with the <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> constructor.</p> <dl class="class"> <dt id="decimal.Context"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Context</tt><big>(</big><em>prec=None</em>, <em>rounding=None</em>, <em>traps=None</em>, <em>flags=None</em>, <em>Emin=None</em>, <em>Emax=None</em>, <em>capitals=1</em><big>)</big><a class="headerlink" href="#decimal.Context" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a new context. If a field is not specified or is <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>, the default values are copied from the <a class="reference internal" href="#decimal.DefaultContext" title="decimal.DefaultContext"><tt class="xref py py-const docutils literal"><span class="pre">DefaultContext</span></tt></a>. If the <em>flags</em> field is not specified or is <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></a>, all flags are cleared.</p> <p>The <em>prec</em> field is a positive integer that sets the precision for arithmetic operations in the context.</p> <p>The <em>rounding</em> option is one of:</p> <ul class="simple"> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_CEILING</span></tt> (towards <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>),</li> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_DOWN</span></tt> (towards zero),</li> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_FLOOR</span></tt> (towards <tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt>),</li> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_DOWN</span></tt> (to nearest with ties going towards zero),</li> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_EVEN</span></tt> (to nearest with ties going to nearest even integer),</li> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_HALF_UP</span></tt> (to nearest with ties going away from zero), or</li> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_UP</span></tt> (away from zero).</li> <li><tt class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></tt> (away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise towards zero)</li> </ul> <p>The <em>traps</em> and <em>flags</em> fields list any signals to be set. Generally, new contexts should only set traps and leave the flags clear.</p> <p>The <em>Emin</em> and <em>Emax</em> fields are integers specifying the outer limits allowable for exponents.</p> <p>The <em>capitals</em> field is either <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt> (the default). If set to <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt>, exponents are printed with a capital <tt class="xref py py-const docutils literal"><span class="pre">E</span></tt>; otherwise, a lowercase <tt class="xref py py-const docutils literal"><span class="pre">e</span></tt> is used: <tt class="xref py py-const docutils literal"><span class="pre">Decimal('6.02e+23')</span></tt>.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>The <tt class="xref py py-const docutils literal"><span class="pre">ROUND_05UP</span></tt> rounding mode was added.</p> <p>The <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> class defines several general purpose methods as well as a large number of methods for doing arithmetic directly in a given context. In addition, for each of the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> methods described above (with the exception of the <tt class="xref py py-meth docutils literal"><span class="pre">adjusted()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">as_tuple()</span></tt> methods) there is a corresponding <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> method. For example, for a <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> instance <tt class="docutils literal"><span class="pre">C</span></tt> and <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instance <tt class="docutils literal"><span class="pre">x</span></tt>, <tt class="docutils literal"><span class="pre">C.exp(x)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">x.exp(context=C)</span></tt>. Each <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> method accepts a Python integer (an instance of <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> or <a class="reference internal" href="functions.html#long" title="long"><tt class="xref py py-class docutils literal"><span class="pre">long</span></tt></a>) anywhere that a Decimal instance is accepted.</p> <dl class="method"> <dt id="decimal.Context.clear_flags"> <tt class="descname">clear_flags</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.clear_flags" title="Permalink to this definition">¶</a></dt> <dd><p>Resets all of the flags to <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.copy"> <tt class="descname">copy</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.copy" title="Permalink to this definition">¶</a></dt> <dd><p>Return a duplicate of the context.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.copy_decimal"> <tt class="descname">copy_decimal</tt><big>(</big><em>num</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_decimal" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of the Decimal instance num.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.create_decimal"> <tt class="descname">create_decimal</tt><big>(</big><em>num</em><big>)</big><a class="headerlink" href="#decimal.Context.create_decimal" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a new Decimal instance from <em>num</em> but using <em>self</em> as context. Unlike the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> constructor, the context precision, rounding method, flags, and traps are applied to the conversion.</p> <p>This is useful because constants are often given to a greater precision than is needed by the application. Another benefit is that rounding immediately eliminates unintended effects from digits beyond the current precision. In the following example, using unrounded inputs means that adding zero to a sum can change the result:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">3</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.4445'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'1.0023'</span><span class="p">)</span> <span class="go">Decimal('4.45')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.4445'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'1.0023'</span><span class="p">)</span> <span class="go">Decimal('4.44')</span> </pre></div> </div> <p>This method implements the to-number operation of the IBM specification. If the argument is a string, no leading or trailing whitespace is permitted.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.create_decimal_from_float"> <tt class="descname">create_decimal_from_float</tt><big>(</big><em>f</em><big>)</big><a class="headerlink" href="#decimal.Context.create_decimal_from_float" title="Permalink to this definition">¶</a></dt> <dd><p>Creates a new Decimal instance from a float <em>f</em> but rounding using <em>self</em> as the context. Unlike the <a class="reference internal" href="#decimal.Decimal.from_float" title="decimal.Decimal.from_float"><tt class="xref py py-meth docutils literal"><span class="pre">Decimal.from_float()</span></tt></a> class method, the context precision, rounding method, flags, and traps are applied to the conversion.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">context</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_DOWN</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">context</span><span class="o">.</span><span class="n">create_decimal_from_float</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="go">Decimal('3.1415')</span> <span class="gp">>>> </span><span class="n">context</span> <span class="o">=</span> <span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">traps</span><span class="o">=</span><span class="p">[</span><span class="n">Inexact</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">context</span><span class="o">.</span><span class="n">create_decimal_from_float</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> <span class="o">...</span> <span class="gr">Inexact</span>: <span class="n">None</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <dl class="method"> <dt id="decimal.Context.Etiny"> <tt class="descname">Etiny</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.Etiny" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a value equal to <tt class="docutils literal"><span class="pre">Emin</span> <span class="pre">-</span> <span class="pre">prec</span> <span class="pre">+</span> <span class="pre">1</span></tt> which is the minimum exponent value for subnormal results. When underflow occurs, the exponent is set to <a class="reference internal" href="#decimal.Context.Etiny" title="decimal.Context.Etiny"><tt class="xref py py-const docutils literal"><span class="pre">Etiny</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.Etop"> <tt class="descname">Etop</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.Etop" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a value equal to <tt class="docutils literal"><span class="pre">Emax</span> <span class="pre">-</span> <span class="pre">prec</span> <span class="pre">+</span> <span class="pre">1</span></tt>.</p> </dd></dl> <p>The usual approach to working with decimals is to create <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instances and then apply arithmetic operations which take place within the current context for the active thread. An alternative approach is to use context methods for calculating within a specific context. The methods are similar to those for the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> class and are only briefly recounted here.</p> <dl class="method"> <dt id="decimal.Context.abs"> <tt class="descname">abs</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.abs" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the absolute value of <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.add"> <tt class="descname">add</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.add" title="Permalink to this definition">¶</a></dt> <dd><p>Return the sum of <em>x</em> and <em>y</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.canonical"> <tt class="descname">canonical</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.canonical" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the same Decimal object <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.compare"> <tt class="descname">compare</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare" title="Permalink to this definition">¶</a></dt> <dd><p>Compares <em>x</em> and <em>y</em> numerically.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.compare_signal"> <tt class="descname">compare_signal</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare_signal" title="Permalink to this definition">¶</a></dt> <dd><p>Compares the values of the two operands numerically.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.compare_total"> <tt class="descname">compare_total</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare_total" title="Permalink to this definition">¶</a></dt> <dd><p>Compares two operands using their abstract representation.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.compare_total_mag"> <tt class="descname">compare_total_mag</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.compare_total_mag" title="Permalink to this definition">¶</a></dt> <dd><p>Compares two operands using their abstract representation, ignoring sign.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.copy_abs"> <tt class="descname">copy_abs</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_abs" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a copy of <em>x</em> with the sign set to 0.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.copy_negate"> <tt class="descname">copy_negate</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_negate" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a copy of <em>x</em> with the sign inverted.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.copy_sign"> <tt class="descname">copy_sign</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.copy_sign" title="Permalink to this definition">¶</a></dt> <dd><p>Copies the sign from <em>y</em> to <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.divide"> <tt class="descname">divide</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.divide" title="Permalink to this definition">¶</a></dt> <dd><p>Return <em>x</em> divided by <em>y</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.divide_int"> <tt class="descname">divide_int</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.divide_int" title="Permalink to this definition">¶</a></dt> <dd><p>Return <em>x</em> divided by <em>y</em>, truncated to an integer.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.divmod"> <tt class="descname">divmod</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.divmod" title="Permalink to this definition">¶</a></dt> <dd><p>Divides two numbers and returns the integer part of the result.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.exp"> <tt class="descname">exp</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.exp" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <cite>e ** x</cite>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.fma"> <tt class="descname">fma</tt><big>(</big><em>x</em>, <em>y</em>, <em>z</em><big>)</big><a class="headerlink" href="#decimal.Context.fma" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <em>x</em> multiplied by <em>y</em>, plus <em>z</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_canonical"> <tt class="descname">is_canonical</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_canonical" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is canonical; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_finite"> <tt class="descname">is_finite</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_finite" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is finite; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_infinite"> <tt class="descname">is_infinite</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_infinite" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is infinite; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_nan"> <tt class="descname">is_nan</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_nan" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is a qNaN or sNaN; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_normal"> <tt class="descname">is_normal</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_normal" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is a normal number; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_qnan"> <tt class="descname">is_qnan</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_qnan" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is a quiet NaN; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_signed"> <tt class="descname">is_signed</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_signed" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is negative; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_snan"> <tt class="descname">is_snan</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_snan" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is a signaling NaN; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_subnormal"> <tt class="descname">is_subnormal</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_subnormal" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is subnormal; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.is_zero"> <tt class="descname">is_zero</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.is_zero" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if <em>x</em> is a zero; otherwise returns False.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.ln"> <tt class="descname">ln</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.ln" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the natural (base e) logarithm of <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.log10"> <tt class="descname">log10</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.log10" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the base 10 logarithm of <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.logb"> <tt class="descname">logb</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.logb" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the exponent of the magnitude of the operand’s MSD.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.logical_and"> <tt class="descname">logical_and</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_and" title="Permalink to this definition">¶</a></dt> <dd><p>Applies the logical operation <em>and</em> between each operand’s digits.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.logical_invert"> <tt class="descname">logical_invert</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_invert" title="Permalink to this definition">¶</a></dt> <dd><p>Invert all the digits in <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.logical_or"> <tt class="descname">logical_or</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_or" title="Permalink to this definition">¶</a></dt> <dd><p>Applies the logical operation <em>or</em> between each operand’s digits.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.logical_xor"> <tt class="descname">logical_xor</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.logical_xor" title="Permalink to this definition">¶</a></dt> <dd><p>Applies the logical operation <em>xor</em> between each operand’s digits.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.max"> <tt class="descname">max</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.max" title="Permalink to this definition">¶</a></dt> <dd><p>Compares two values numerically and returns the maximum.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.max_mag"> <tt class="descname">max_mag</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.max_mag" title="Permalink to this definition">¶</a></dt> <dd><p>Compares the values numerically with their sign ignored.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.min"> <tt class="descname">min</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.min" title="Permalink to this definition">¶</a></dt> <dd><p>Compares two values numerically and returns the minimum.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.min_mag"> <tt class="descname">min_mag</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.min_mag" title="Permalink to this definition">¶</a></dt> <dd><p>Compares the values numerically with their sign ignored.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.minus"> <tt class="descname">minus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.minus" title="Permalink to this definition">¶</a></dt> <dd><p>Minus corresponds to the unary prefix minus operator in Python.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.multiply"> <tt class="descname">multiply</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.multiply" title="Permalink to this definition">¶</a></dt> <dd><p>Return the product of <em>x</em> and <em>y</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.next_minus"> <tt class="descname">next_minus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.next_minus" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the largest representable number smaller than <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.next_plus"> <tt class="descname">next_plus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.next_plus" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the smallest representable number larger than <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.next_toward"> <tt class="descname">next_toward</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.next_toward" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the number closest to <em>x</em>, in direction towards <em>y</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.normalize"> <tt class="descname">normalize</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.normalize" title="Permalink to this definition">¶</a></dt> <dd><p>Reduces <em>x</em> to its simplest form.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.number_class"> <tt class="descname">number_class</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.number_class" title="Permalink to this definition">¶</a></dt> <dd><p>Returns an indication of the class of <em>x</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.plus"> <tt class="descname">plus</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.plus" title="Permalink to this definition">¶</a></dt> <dd><p>Plus corresponds to the unary prefix plus operator in Python. This operation applies the context precision and rounding, so it is <em>not</em> an identity operation.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.power"> <tt class="descname">power</tt><big>(</big><em>x</em>, <em>y</em><span class="optional">[</span>, <em>modulo</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#decimal.Context.power" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">x</span></tt> to the power of <tt class="docutils literal"><span class="pre">y</span></tt>, reduced modulo <tt class="docutils literal"><span class="pre">modulo</span></tt> if given.</p> <p>With two arguments, compute <tt class="docutils literal"><span class="pre">x**y</span></tt>. If <tt class="docutils literal"><span class="pre">x</span></tt> is negative then <tt class="docutils literal"><span class="pre">y</span></tt> must be integral. The result will be inexact unless <tt class="docutils literal"><span class="pre">y</span></tt> is integral and the result is finite and can be expressed exactly in ‘precision’ digits. The result should always be correctly rounded, using the rounding mode of the current thread’s context.</p> <p>With three arguments, compute <tt class="docutils literal"><span class="pre">(x**y)</span> <span class="pre">%</span> <span class="pre">modulo</span></tt>. For the three argument form, the following restrictions on the arguments hold:</p> <blockquote> <div><ul class="simple"> <li>all three arguments must be integral</li> <li><tt class="docutils literal"><span class="pre">y</span></tt> must be nonnegative</li> <li>at least one of <tt class="docutils literal"><span class="pre">x</span></tt> or <tt class="docutils literal"><span class="pre">y</span></tt> must be nonzero</li> <li><tt class="docutils literal"><span class="pre">modulo</span></tt> must be nonzero and have at most ‘precision’ digits</li> </ul> </div></blockquote> <p>The value resulting from <tt class="docutils literal"><span class="pre">Context.power(x,</span> <span class="pre">y,</span> <span class="pre">modulo)</span></tt> is equal to the value that would be obtained by computing <tt class="docutils literal"><span class="pre">(x**y)</span> <span class="pre">%</span> <span class="pre">modulo</span></tt> with unbounded precision, but is computed more efficiently. The exponent of the result is zero, regardless of the exponents of <tt class="docutils literal"><span class="pre">x</span></tt>, <tt class="docutils literal"><span class="pre">y</span></tt> and <tt class="docutils literal"><span class="pre">modulo</span></tt>. The result is always exact.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span><tt class="docutils literal"><span class="pre">y</span></tt> may now be nonintegral in <tt class="docutils literal"><span class="pre">x**y</span></tt>. Stricter requirements for the three-argument version.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.quantize"> <tt class="descname">quantize</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.quantize" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a value equal to <em>x</em> (rounded), having the exponent of <em>y</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.radix"> <tt class="descname">radix</tt><big>(</big><big>)</big><a class="headerlink" href="#decimal.Context.radix" title="Permalink to this definition">¶</a></dt> <dd><p>Just returns 10, as this is Decimal, :)</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.remainder"> <tt class="descname">remainder</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.remainder" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the remainder from integer division.</p> <p>The sign of the result, if non-zero, is the same as that of the original dividend.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.remainder_near"> <tt class="descname">remainder_near</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.remainder_near" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <tt class="docutils literal"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span> <span class="pre">*</span> <span class="pre">n</span></tt>, where <em>n</em> is the integer nearest the exact value of <tt class="docutils literal"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></tt> (if the result is 0 then its sign will be the sign of <em>x</em>).</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.rotate"> <tt class="descname">rotate</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.rotate" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a rotated copy of <em>x</em>, <em>y</em> times.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.same_quantum"> <tt class="descname">same_quantum</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.same_quantum" title="Permalink to this definition">¶</a></dt> <dd><p>Returns True if the two operands have the same exponent.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.scaleb"> <tt class="descname">scaleb</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.scaleb" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the first operand after adding the second value its exp.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.shift"> <tt class="descname">shift</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.shift" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a shifted copy of <em>x</em>, <em>y</em> times.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.sqrt"> <tt class="descname">sqrt</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.sqrt" title="Permalink to this definition">¶</a></dt> <dd><p>Square root of a non-negative number to context precision.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.subtract"> <tt class="descname">subtract</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#decimal.Context.subtract" title="Permalink to this definition">¶</a></dt> <dd><p>Return the difference between <em>x</em> and <em>y</em>.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.to_eng_string"> <tt class="descname">to_eng_string</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.to_eng_string" title="Permalink to this definition">¶</a></dt> <dd><p>Converts a number to a string, using scientific notation.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.to_integral_exact"> <tt class="descname">to_integral_exact</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.to_integral_exact" title="Permalink to this definition">¶</a></dt> <dd><p>Rounds to an integer.</p> </dd></dl> <dl class="method"> <dt id="decimal.Context.to_sci_string"> <tt class="descname">to_sci_string</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#decimal.Context.to_sci_string" title="Permalink to this definition">¶</a></dt> <dd><p>Converts a number to a string using scientific notation.</p> </dd></dl> </dd></dl> </div> <div class="section" id="signals"> <span id="decimal-signals"></span><h2>9.4.4. Signals<a class="headerlink" href="#signals" title="Permalink to this headline">¶</a></h2> <p>Signals represent conditions that arise during computation. Each corresponds to one context flag and one context trap enabler.</p> <p>The context flag is set whenever the condition is encountered. After the computation, flags may be checked for informational purposes (for instance, to determine whether a computation was exact). After checking the flags, be sure to clear all flags before starting the next computation.</p> <p>If the context’s trap enabler is set for the signal, then the condition causes a Python exception to be raised. For example, if the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-class docutils literal"><span class="pre">DivisionByZero</span></tt></a> trap is set, then a <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-exc docutils literal"><span class="pre">DivisionByZero</span></tt></a> exception is raised upon encountering the condition.</p> <dl class="class"> <dt id="decimal.Clamped"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Clamped</tt><a class="headerlink" href="#decimal.Clamped" title="Permalink to this definition">¶</a></dt> <dd><p>Altered an exponent to fit representation constraints.</p> <p>Typically, clamping occurs when an exponent falls outside the context’s <tt class="xref py py-attr docutils literal"><span class="pre">Emin</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">Emax</span></tt> limits. If possible, the exponent is reduced to fit by adding zeros to the coefficient.</p> </dd></dl> <dl class="class"> <dt id="decimal.DecimalException"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">DecimalException</tt><a class="headerlink" href="#decimal.DecimalException" title="Permalink to this definition">¶</a></dt> <dd><p>Base class for other signals and a subclass of <a class="reference internal" href="exceptions.html#exceptions.ArithmeticError" title="exceptions.ArithmeticError"><tt class="xref py py-exc docutils literal"><span class="pre">ArithmeticError</span></tt></a>.</p> </dd></dl> <dl class="class"> <dt id="decimal.DivisionByZero"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">DivisionByZero</tt><a class="headerlink" href="#decimal.DivisionByZero" title="Permalink to this definition">¶</a></dt> <dd><p>Signals the division of a non-infinite number by zero.</p> <p>Can occur with division, modulo division, or when raising a number to a negative power. If this signal is not trapped, returns <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt> with the sign determined by the inputs to the calculation.</p> </dd></dl> <dl class="class"> <dt id="decimal.Inexact"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Inexact</tt><a class="headerlink" href="#decimal.Inexact" title="Permalink to this definition">¶</a></dt> <dd><p>Indicates that rounding occurred and the result is not exact.</p> <p>Signals when non-zero digits were discarded during rounding. The rounded result is returned. The signal flag or trap is used to detect when results are inexact.</p> </dd></dl> <dl class="class"> <dt id="decimal.InvalidOperation"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">InvalidOperation</tt><a class="headerlink" href="#decimal.InvalidOperation" title="Permalink to this definition">¶</a></dt> <dd><p>An invalid operation was performed.</p> <p>Indicates that an operation was requested that does not make sense. If not trapped, returns <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>. Possible causes include:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">Infinity</span> <span class="o">-</span> <span class="n">Infinity</span> <span class="mi">0</span> <span class="o">*</span> <span class="n">Infinity</span> <span class="n">Infinity</span> <span class="o">/</span> <span class="n">Infinity</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">0</span> <span class="n">Infinity</span> <span class="o">%</span> <span class="n">x</span> <span class="n">x</span><span class="o">.</span><span class="n">_rescale</span><span class="p">(</span> <span class="n">non</span><span class="o">-</span><span class="n">integer</span> <span class="p">)</span> <span class="n">sqrt</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">)</span> <span class="ow">and</span> <span class="n">x</span> <span class="o">></span> <span class="mi">0</span> <span class="mi">0</span> <span class="o">**</span> <span class="mi">0</span> <span class="n">x</span> <span class="o">**</span> <span class="p">(</span><span class="n">non</span><span class="o">-</span><span class="n">integer</span><span class="p">)</span> <span class="n">x</span> <span class="o">**</span> <span class="n">Infinity</span> </pre></div> </div> </dd></dl> <dl class="class"> <dt id="decimal.Overflow"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Overflow</tt><a class="headerlink" href="#decimal.Overflow" title="Permalink to this definition">¶</a></dt> <dd><p>Numerical overflow.</p> <p>Indicates the exponent is larger than <tt class="xref py py-attr docutils literal"><span class="pre">Emax</span></tt> after rounding has occurred. If not trapped, the result depends on the rounding mode, either pulling inward to the largest representable finite number or rounding outward to <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>. In either case, <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-class docutils literal"><span class="pre">Inexact</span></tt></a> and <a class="reference internal" href="#decimal.Rounded" title="decimal.Rounded"><tt class="xref py py-class docutils literal"><span class="pre">Rounded</span></tt></a> are also signaled.</p> </dd></dl> <dl class="class"> <dt id="decimal.Rounded"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Rounded</tt><a class="headerlink" href="#decimal.Rounded" title="Permalink to this definition">¶</a></dt> <dd><p>Rounding occurred though possibly no information was lost.</p> <p>Signaled whenever rounding discards digits; even if those digits are zero (such as rounding <tt class="xref py py-const docutils literal"><span class="pre">5.00</span></tt> to <tt class="xref py py-const docutils literal"><span class="pre">5.0</span></tt>). If not trapped, returns the result unchanged. This signal is used to detect loss of significant digits.</p> </dd></dl> <dl class="class"> <dt id="decimal.Subnormal"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Subnormal</tt><a class="headerlink" href="#decimal.Subnormal" title="Permalink to this definition">¶</a></dt> <dd><p>Exponent was lower than <tt class="xref py py-attr docutils literal"><span class="pre">Emin</span></tt> prior to rounding.</p> <p>Occurs when an operation result is subnormal (the exponent is too small). If not trapped, returns the result unchanged.</p> </dd></dl> <dl class="class"> <dt id="decimal.Underflow"> <em class="property">class </em><tt class="descclassname">decimal.</tt><tt class="descname">Underflow</tt><a class="headerlink" href="#decimal.Underflow" title="Permalink to this definition">¶</a></dt> <dd><p>Numerical underflow with result rounded to zero.</p> <p>Occurs when a subnormal result is pushed to zero by rounding. <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-class docutils literal"><span class="pre">Inexact</span></tt></a> and <a class="reference internal" href="#decimal.Subnormal" title="decimal.Subnormal"><tt class="xref py py-class docutils literal"><span class="pre">Subnormal</span></tt></a> are also signaled.</p> </dd></dl> <p>The following table summarizes the hierarchy of signals:</p> <div class="highlight-python"><pre>exceptions.ArithmeticError(exceptions.StandardError) DecimalException Clamped DivisionByZero(DecimalException, exceptions.ZeroDivisionError) Inexact Overflow(Inexact, Rounded) Underflow(Inexact, Rounded, Subnormal) InvalidOperation Rounded Subnormal</pre> </div> </div> <div class="section" id="floating-point-notes"> <span id="decimal-notes"></span><h2>9.4.5. Floating Point Notes<a class="headerlink" href="#floating-point-notes" title="Permalink to this headline">¶</a></h2> <div class="section" id="mitigating-round-off-error-with-increased-precision"> <h3>9.4.5.1. Mitigating round-off error with increased precision<a class="headerlink" href="#mitigating-round-off-error-with-increased-precision" title="Permalink to this headline">¶</a></h3> <p>The use of decimal floating point eliminates decimal representation error (making it possible to represent <tt class="xref py py-const docutils literal"><span class="pre">0.1</span></tt> exactly); however, some operations can still incur round-off error when non-zero digits exceed the fixed precision.</p> <p>The effects of round-off error can be amplified by the addition or subtraction of nearly offsetting quantities resulting in loss of significance. Knuth provides two instructive examples where rounded floating point arithmetic with insufficient precision causes the breakdown of the associative and distributive properties of addition:</p> <div class="highlight-python"><pre># Examples from Seminumerical Algorithms, Section 4.2.2. >>> from decimal import Decimal, getcontext >>> getcontext().prec = 8 >>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111') >>> (u + v) + w Decimal('9.5111111') >>> u + (v + w) Decimal('10') >>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003') >>> (u*v) + (u*w) Decimal('0.01') >>> u * (v+w) Decimal('0.0060000')</pre> </div> <p>The <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module makes it possible to restore the identities by expanding the precision sufficiently to avoid loss of significance:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">20</span> <span class="gp">>>> </span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">11111113</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">11111111</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'7.51111111'</span><span class="p">)</span> <span class="gp">>>> </span><span class="p">(</span><span class="n">u</span> <span class="o">+</span> <span class="n">v</span><span class="p">)</span> <span class="o">+</span> <span class="n">w</span> <span class="go">Decimal('9.51111111')</span> <span class="gp">>>> </span><span class="n">u</span> <span class="o">+</span> <span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">w</span><span class="p">)</span> <span class="go">Decimal('9.51111111')</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">w</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">20000</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="o">-</span><span class="mi">6</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'6.0000003'</span><span class="p">)</span> <span class="gp">>>> </span><span class="p">(</span><span class="n">u</span><span class="o">*</span><span class="n">v</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">u</span><span class="o">*</span><span class="n">w</span><span class="p">)</span> <span class="go">Decimal('0.0060000')</span> <span class="gp">>>> </span><span class="n">u</span> <span class="o">*</span> <span class="p">(</span><span class="n">v</span><span class="o">+</span><span class="n">w</span><span class="p">)</span> <span class="go">Decimal('0.0060000')</span> </pre></div> </div> </div> <div class="section" id="special-values"> <h3>9.4.5.2. Special values<a class="headerlink" href="#special-values" title="Permalink to this headline">¶</a></h3> <p>The number system for the <a class="reference internal" href="#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module provides special values including <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">sNaN</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">-Infinity</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">Infinity</span></tt>, and two zeros, <tt class="xref py py-const docutils literal"><span class="pre">+0</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">-0</span></tt>.</p> <p>Infinities can be constructed directly with: <tt class="docutils literal"><span class="pre">Decimal('Infinity')</span></tt>. Also, they can arise from dividing by zero when the <a class="reference internal" href="#decimal.DivisionByZero" title="decimal.DivisionByZero"><tt class="xref py py-exc docutils literal"><span class="pre">DivisionByZero</span></tt></a> signal is not trapped. Likewise, when the <a class="reference internal" href="#decimal.Overflow" title="decimal.Overflow"><tt class="xref py py-exc docutils literal"><span class="pre">Overflow</span></tt></a> signal is not trapped, infinity can result from rounding beyond the limits of the largest representable number.</p> <p>The infinities are signed (affine) and can be used in arithmetic operations where they get treated as very large, indeterminate numbers. For instance, adding a constant to infinity gives another infinite result.</p> <p>Some operations are indeterminate and return <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>, or if the <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-exc docutils literal"><span class="pre">InvalidOperation</span></tt></a> signal is trapped, raise an exception. For example, <tt class="docutils literal"><span class="pre">0/0</span></tt> returns <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> which means “not a number”. This variety of <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> is quiet and, once created, will flow through other computations always resulting in another <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>. This behavior can be useful for a series of computations that occasionally have missing inputs — it allows the calculation to proceed while flagging specific results as invalid.</p> <p>A variant is <tt class="xref py py-const docutils literal"><span class="pre">sNaN</span></tt> which signals rather than remaining quiet after every operation. This is a useful return value when an invalid result needs to interrupt a calculation for special handling.</p> <p>The behavior of Python’s comparison operators can be a little surprising where a <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> is involved. A test for equality where one of the operands is a quiet or signaling <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> always returns <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> (even when doing <tt class="docutils literal"><span class="pre">Decimal('NaN')==Decimal('NaN')</span></tt>), while a test for inequality always returns <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a>. An attempt to compare two Decimals using any of the <tt class="docutils literal"><span class="pre"><</span></tt>, <tt class="docutils literal"><span class="pre"><=</span></tt>, <tt class="docutils literal"><span class="pre">></span></tt> or <tt class="docutils literal"><span class="pre">>=</span></tt> operators will raise the <a class="reference internal" href="#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-exc docutils literal"><span class="pre">InvalidOperation</span></tt></a> signal if either operand is a <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt>, and return <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> if this signal is not trapped. Note that the General Decimal Arithmetic specification does not specify the behavior of direct comparisons; these rules for comparisons involving a <tt class="xref py py-const docutils literal"><span class="pre">NaN</span></tt> were taken from the IEEE 854 standard (see Table 3 in section 5.7). To ensure strict standards-compliance, use the <tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">compare-signal()</span></tt> methods instead.</p> <p>The signed zeros can result from calculations that underflow. They keep the sign that would have resulted if the calculation had been carried out to greater precision. Since their magnitude is zero, both positive and negative zeros are treated as equal and their sign is informational.</p> <p>In addition to the two signed zeros which are distinct yet equal, there are various representations of zero with differing precisions yet equivalent in value. This takes a bit of getting used to. For an eye accustomed to normalized floating point representations, it is not immediately obvious that the following calculation returns a value equal to zero:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">1</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'Infinity'</span><span class="p">)</span> <span class="go">Decimal('0E-1000000026')</span> </pre></div> </div> </div> </div> <div class="section" id="working-with-threads"> <span id="decimal-threads"></span><h2>9.4.6. Working with threads<a class="headerlink" href="#working-with-threads" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> function accesses a different <a class="reference internal" href="#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> object for each thread. Having separate thread contexts means that threads may make changes (such as <tt class="docutils literal"><span class="pre">getcontext.prec=10</span></tt>) without interfering with other threads.</p> <p>Likewise, the <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a> function automatically assigns its target to the current thread.</p> <p>If <a class="reference internal" href="#decimal.setcontext" title="decimal.setcontext"><tt class="xref py py-func docutils literal"><span class="pre">setcontext()</span></tt></a> has not been called before <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a>, then <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a> will automatically create a new context for use in the current thread.</p> <p>The new context is copied from a prototype context called <em>DefaultContext</em>. To control the defaults so that each thread will use the same values throughout the application, directly modify the <em>DefaultContext</em> object. This should be done <em>before</em> any threads are started so that there won’t be a race condition between threads calling <a class="reference internal" href="#decimal.getcontext" title="decimal.getcontext"><tt class="xref py py-func docutils literal"><span class="pre">getcontext()</span></tt></a>. For example:</p> <div class="highlight-python"><pre># Set applicationwide defaults for all threads about to be launched DefaultContext.prec = 12 DefaultContext.rounding = ROUND_DOWN DefaultContext.traps = ExtendedContext.traps.copy() DefaultContext.traps[InvalidOperation] = 1 setcontext(DefaultContext) # Afterwards, the threads can be started t1.start() t2.start() t3.start() . . .</pre> </div> </div> <div class="section" id="recipes"> <span id="decimal-recipes"></span><h2>9.4.7. Recipes<a class="headerlink" href="#recipes" title="Permalink to this headline">¶</a></h2> <p>Here are a few recipes that serve as utility functions and that demonstrate ways to work with the <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> class:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">moneyfmt</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">places</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">curr</span><span class="o">=</span><span class="s">''</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">','</span><span class="p">,</span> <span class="n">dp</span><span class="o">=</span><span class="s">'.'</span><span class="p">,</span> <span class="n">pos</span><span class="o">=</span><span class="s">''</span><span class="p">,</span> <span class="n">neg</span><span class="o">=</span><span class="s">'-'</span><span class="p">,</span> <span class="n">trailneg</span><span class="o">=</span><span class="s">''</span><span class="p">):</span> <span class="sd">"""Convert Decimal to a money formatted string.</span> <span class="sd"> places: required number of places after the decimal point</span> <span class="sd"> curr: optional currency symbol before the sign (may be blank)</span> <span class="sd"> sep: optional grouping separator (comma, period, space, or blank)</span> <span class="sd"> dp: decimal point indicator (comma or period)</span> <span class="sd"> only specify as blank when places is zero</span> <span class="sd"> pos: optional sign for positive numbers: '+', space or blank</span> <span class="sd"> neg: optional sign for negative numbers: '-', '(', space or blank</span> <span class="sd"> trailneg:optional trailing minus indicator: '-', ')', space or blank</span> <span class="sd"> >>> d = Decimal('-1234567.8901')</span> <span class="sd"> >>> moneyfmt(d, curr='$')</span> <span class="sd"> '-$1,234,567.89'</span> <span class="sd"> >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')</span> <span class="sd"> '1.234.568-'</span> <span class="sd"> >>> moneyfmt(d, curr='$', neg='(', trailneg=')')</span> <span class="sd"> '($1,234,567.89)'</span> <span class="sd"> >>> moneyfmt(Decimal(123456789), sep=' ')</span> <span class="sd"> '123 456 789.00'</span> <span class="sd"> >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')</span> <span class="sd"> '<0.02>'</span> <span class="sd"> """</span> <span class="n">q</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">**</span> <span class="o">-</span><span class="n">places</span> <span class="c"># 2 places --> '0.01'</span> <span class="n">sign</span><span class="p">,</span> <span class="n">digits</span><span class="p">,</span> <span class="n">exp</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">q</span><span class="p">)</span><span class="o">.</span><span class="n">as_tuple</span><span class="p">()</span> <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">digits</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">digits</span><span class="p">)</span> <span class="n">build</span><span class="p">,</span> <span class="nb">next</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">,</span> <span class="n">digits</span><span class="o">.</span><span class="n">pop</span> <span class="k">if</span> <span class="n">sign</span><span class="p">:</span> <span class="n">build</span><span class="p">(</span><span class="n">trailneg</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">places</span><span class="p">):</span> <span class="n">build</span><span class="p">(</span><span class="nb">next</span><span class="p">()</span> <span class="k">if</span> <span class="n">digits</span> <span class="k">else</span> <span class="s">'0'</span><span class="p">)</span> <span class="n">build</span><span class="p">(</span><span class="n">dp</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">digits</span><span class="p">:</span> <span class="n">build</span><span class="p">(</span><span class="s">'0'</span><span class="p">)</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">while</span> <span class="n">digits</span><span class="p">:</span> <span class="n">build</span><span class="p">(</span><span class="nb">next</span><span class="p">())</span> <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">digits</span><span class="p">:</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span> <span class="n">build</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span> <span class="n">build</span><span class="p">(</span><span class="n">curr</span><span class="p">)</span> <span class="n">build</span><span class="p">(</span><span class="n">neg</span> <span class="k">if</span> <span class="n">sign</span> <span class="k">else</span> <span class="n">pos</span><span class="p">)</span> <span class="k">return</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">result</span><span class="p">))</span> <span class="k">def</span> <span class="nf">pi</span><span class="p">():</span> <span class="sd">"""Compute Pi to the current precision.</span> <span class="sd"> >>> print pi()</span> <span class="sd"> 3.141592653589793238462643383</span> <span class="sd"> """</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span> <span class="c"># extra digits for intermediate steps</span> <span class="n">three</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c"># substitute "three=3.0" for regular floats</span> <span class="n">lasts</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">na</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">da</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">three</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">24</span> <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span> <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span> <span class="n">n</span><span class="p">,</span> <span class="n">na</span> <span class="o">=</span> <span class="n">n</span><span class="o">+</span><span class="n">na</span><span class="p">,</span> <span class="n">na</span><span class="o">+</span><span class="mi">8</span> <span class="n">d</span><span class="p">,</span> <span class="n">da</span> <span class="o">=</span> <span class="n">d</span><span class="o">+</span><span class="n">da</span><span class="p">,</span> <span class="n">da</span><span class="o">+</span><span class="mi">32</span> <span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="n">t</span> <span class="o">*</span> <span class="n">n</span><span class="p">)</span> <span class="o">/</span> <span class="n">d</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">t</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span> <span class="k">return</span> <span class="o">+</span><span class="n">s</span> <span class="c"># unary plus applies the new precision</span> <span class="k">def</span> <span class="nf">exp</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="sd">"""Return e raised to the power of x. Result type matches input type.</span> <span class="sd"> >>> print exp(Decimal(1))</span> <span class="sd"> 2.718281828459045235360287471</span> <span class="sd"> >>> print exp(Decimal(2))</span> <span class="sd"> 7.389056098930650227230427461</span> <span class="sd"> >>> print exp(2.0)</span> <span class="sd"> 7.38905609893</span> <span class="sd"> >>> print exp(2+0j)</span> <span class="sd"> (7.38905609893+0j)</span> <span class="sd"> """</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span> <span class="n">i</span><span class="p">,</span> <span class="n">lasts</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">fact</span><span class="p">,</span> <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span> <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span> <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span> <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span> <span class="n">fact</span> <span class="o">*=</span> <span class="n">i</span> <span class="n">num</span> <span class="o">*=</span> <span class="n">x</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">num</span> <span class="o">/</span> <span class="n">fact</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span> <span class="k">return</span> <span class="o">+</span><span class="n">s</span> <span class="k">def</span> <span class="nf">cos</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="sd">"""Return the cosine of x as measured in radians.</span> <span class="sd"> >>> print cos(Decimal('0.5'))</span> <span class="sd"> 0.8775825618903727161162815826</span> <span class="sd"> >>> print cos(0.5)</span> <span class="sd"> 0.87758256189</span> <span class="sd"> >>> print cos(0.5+0j)</span> <span class="sd"> (0.87758256189+0j)</span> <span class="sd"> """</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span> <span class="n">i</span><span class="p">,</span> <span class="n">lasts</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">fact</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">sign</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span> <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span> <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span> <span class="n">i</span> <span class="o">+=</span> <span class="mi">2</span> <span class="n">fact</span> <span class="o">*=</span> <span class="n">i</span> <span class="o">*</span> <span class="p">(</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">num</span> <span class="o">*=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="n">sign</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">num</span> <span class="o">/</span> <span class="n">fact</span> <span class="o">*</span> <span class="n">sign</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span> <span class="k">return</span> <span class="o">+</span><span class="n">s</span> <span class="k">def</span> <span class="nf">sin</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="sd">"""Return the sine of x as measured in radians.</span> <span class="sd"> >>> print sin(Decimal('0.5'))</span> <span class="sd"> 0.4794255386042030002732879352</span> <span class="sd"> >>> print sin(0.5)</span> <span class="sd"> 0.479425538604</span> <span class="sd"> >>> print sin(0.5+0j)</span> <span class="sd"> (0.479425538604+0j)</span> <span class="sd"> """</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">+=</span> <span class="mi">2</span> <span class="n">i</span><span class="p">,</span> <span class="n">lasts</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">fact</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">sign</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">1</span> <span class="k">while</span> <span class="n">s</span> <span class="o">!=</span> <span class="n">lasts</span><span class="p">:</span> <span class="n">lasts</span> <span class="o">=</span> <span class="n">s</span> <span class="n">i</span> <span class="o">+=</span> <span class="mi">2</span> <span class="n">fact</span> <span class="o">*=</span> <span class="n">i</span> <span class="o">*</span> <span class="p">(</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="n">num</span> <span class="o">*=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="n">sign</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">num</span> <span class="o">/</span> <span class="n">fact</span> <span class="o">*</span> <span class="n">sign</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">-=</span> <span class="mi">2</span> <span class="k">return</span> <span class="o">+</span><span class="n">s</span> </pre></div> </div> </div> <div class="section" id="decimal-faq"> <span id="id1"></span><h2>9.4.8. Decimal FAQ<a class="headerlink" href="#decimal-faq" title="Permalink to this headline">¶</a></h2> <p>Q. It is cumbersome to type <tt class="docutils literal"><span class="pre">decimal.Decimal('1234.5')</span></tt>. Is there a way to minimize typing when using the interactive interpreter?</p> <p>A. Some users abbreviate the constructor to just a single letter:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">D</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span> <span class="gp">>>> </span><span class="n">D</span><span class="p">(</span><span class="s">'1.23'</span><span class="p">)</span> <span class="o">+</span> <span class="n">D</span><span class="p">(</span><span class="s">'3.45'</span><span class="p">)</span> <span class="go">Decimal('4.68')</span> </pre></div> </div> <p>Q. In a fixed-point application with two decimal places, some inputs have many places and need to be rounded. Others are not supposed to have excess digits and need to be validated. What methods should be used?</p> <p>A. The <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> method rounds to a fixed number of decimal places. If the <a class="reference internal" href="#decimal.Inexact" title="decimal.Inexact"><tt class="xref py py-const docutils literal"><span class="pre">Inexact</span></tt></a> trap is set, it is also useful for validation:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">TWOPLACES</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">**</span> <span class="o">-</span><span class="mi">2</span> <span class="c"># same as Decimal('0.01')</span> </pre></div> </div> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># Round to two places</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.214'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">)</span> <span class="go">Decimal('3.21')</span> </pre></div> </div> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># Validate that a number does not exceed two places</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.21'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">Context</span><span class="p">(</span><span class="n">traps</span><span class="o">=</span><span class="p">[</span><span class="n">Inexact</span><span class="p">]))</span> <span class="go">Decimal('3.21')</span> </pre></div> </div> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.214'</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">Context</span><span class="p">(</span><span class="n">traps</span><span class="o">=</span><span class="p">[</span><span class="n">Inexact</span><span class="p">]))</span> <span class="gt">Traceback (most recent call last):</span> <span class="c">...</span> <span class="gr">Inexact</span>: <span class="n">None</span> </pre></div> </div> <p>Q. Once I have valid two place inputs, how do I maintain that invariant throughout an application?</p> <p>A. Some operations like addition, subtraction, and multiplication by an integer will automatically preserve fixed point. Others operations, like division and non-integer multiplication, will change the number of decimal places and need to be followed-up with a <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> step:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'102.72'</span><span class="p">)</span> <span class="c"># Initial fixed-point values</span> <span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'3.17'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="c"># Addition preserves fixed-point</span> <span class="go">Decimal('105.89')</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span> <span class="go">Decimal('99.55')</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">*</span> <span class="mi">42</span> <span class="c"># So does integer multiplication</span> <span class="go">Decimal('4314.24')</span> <span class="gp">>>> </span><span class="p">(</span><span class="n">a</span> <span class="o">*</span> <span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">)</span> <span class="c"># Must quantize non-integer multiplication</span> <span class="go">Decimal('325.62')</span> <span class="gp">>>> </span><span class="p">(</span><span class="n">b</span> <span class="o">/</span> <span class="n">a</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">TWOPLACES</span><span class="p">)</span> <span class="c"># And quantize division</span> <span class="go">Decimal('0.03')</span> </pre></div> </div> <p>In developing fixed-point applications, it is convenient to define functions to handle the <tt class="xref py py-meth docutils literal"><span class="pre">quantize()</span></tt> step:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">mul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">fp</span><span class="o">=</span><span class="n">TWOPLACES</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">div</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">fp</span><span class="o">=</span><span class="n">TWOPLACES</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span> </pre></div> </div> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">mul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="c"># Automatically preserve fixed-point</span> <span class="go">Decimal('325.62')</span> <span class="gp">>>> </span><span class="n">div</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span> <span class="go">Decimal('0.03')</span> </pre></div> </div> <p>Q. There are many ways to express the same value. The numbers <tt class="xref py py-const docutils literal"><span class="pre">200</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">200.000</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">2E2</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">02E+4</span></tt> all have the same value at various precisions. Is there a way to transform them to a single recognizable canonical value?</p> <p>A. The <tt class="xref py py-meth docutils literal"><span class="pre">normalize()</span></tt> method maps all equivalent values to a single representative:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">values</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">Decimal</span><span class="p">,</span> <span class="s">'200 200.000 2E2 .02E+4'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="gp">>>> </span><span class="p">[</span><span class="n">v</span><span class="o">.</span><span class="n">normalize</span><span class="p">()</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">values</span><span class="p">]</span> <span class="go">[Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]</span> </pre></div> </div> <p>Q. Some decimal values always print with exponential notation. Is there a way to get a non-exponential representation?</p> <p>A. For some values, exponential notation is the only way to express the number of significant places in the coefficient. For example, expressing <tt class="xref py py-const docutils literal"><span class="pre">5.0E+3</span></tt> as <tt class="xref py py-const docutils literal"><span class="pre">5000</span></tt> keeps the value constant but cannot show the original’s two-place significance.</p> <p>If an application does not care about tracking significance, it is easy to remove the exponent and trailing zeros, losing significance, but keeping the value unchanged:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">remove_exponent</span><span class="p">(</span><span class="n">d</span><span class="p">):</span> <span class="sd">'''Remove exponent and trailing zeros.</span> <span class="sd"> >>> remove_exponent(Decimal('5E+3'))</span> <span class="sd"> Decimal('5000')</span> <span class="sd"> '''</span> <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="k">if</span> <span class="n">d</span> <span class="o">==</span> <span class="n">d</span><span class="o">.</span><span class="n">to_integral</span><span class="p">()</span> <span class="k">else</span> <span class="n">d</span><span class="o">.</span><span class="n">normalize</span><span class="p">()</span> </pre></div> </div> <p>Q. Is there a way to convert a regular float to a <a class="reference internal" href="#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>?</p> <p>A. Yes, any binary floating point number can be exactly expressed as a Decimal though an exact conversion may take more precision than intuition would suggest:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="go">Decimal('3.141592653589793115997963468544185161590576171875')</span> </pre></div> </div> <p>Q. Within a complex calculation, how can I make sure that I haven’t gotten a spurious result because of insufficient precision or rounding anomalies.</p> <p>A. The decimal module makes it easy to test results. A best practice is to re-run calculations using greater precision and with various rounding modes. Widely differing results indicate insufficient precision, rounding mode issues, ill-conditioned inputs, or a numerically unstable algorithm.</p> <p>Q. I noticed that context precision is applied to the results of operations but not to the inputs. Is there anything to watch out for when mixing values of different precisions?</p> <p>A. Yes. The principle is that all values are considered to be exact and so is the arithmetic on those values. Only the results are rounded. The advantage for inputs is that “what you type is what you get”. A disadvantage is that the results can look odd if you forget that the inputs haven’t been rounded:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">3</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.104'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'2.104'</span><span class="p">)</span> <span class="go">Decimal('5.21')</span> <span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="s">'3.104'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'0.000'</span><span class="p">)</span> <span class="o">+</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">'2.104'</span><span class="p">)</span> <span class="go">Decimal('5.20')</span> </pre></div> </div> <p>The solution is either to increase precision or to force rounding of inputs using the unary plus operation:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">3</span> <span class="gp">>>> </span><span class="o">+</span><span class="n">Decimal</span><span class="p">(</span><span class="s">'1.23456789'</span><span class="p">)</span> <span class="c"># unary plus triggers rounding</span> <span class="go">Decimal('1.23')</span> </pre></div> </div> <p>Alternatively, inputs can be rounded upon creation using the <a class="reference internal" href="#decimal.Context.create_decimal" title="decimal.Context.create_decimal"><tt class="xref py py-meth docutils literal"><span class="pre">Context.create_decimal()</span></tt></a> method:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_DOWN</span><span class="p">)</span><span class="o">.</span><span class="n">create_decimal</span><span class="p">(</span><span class="s">'1.2345678'</span><span class="p">)</span> <span class="go">Decimal('1.2345')</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">9.4. <tt class="docutils literal"><span class="pre">decimal</span></tt> — Decimal fixed point and floating point arithmetic</a><ul> <li><a class="reference internal" href="#quick-start-tutorial">9.4.1. Quick-start Tutorial</a></li> <li><a class="reference internal" href="#decimal-objects">9.4.2. Decimal objects</a><ul> <li><a class="reference internal" href="#logical-operands">9.4.2.1. Logical operands</a></li> </ul> </li> <li><a class="reference internal" href="#context-objects">9.4.3. Context objects</a></li> <li><a class="reference internal" href="#signals">9.4.4. Signals</a></li> <li><a class="reference internal" href="#floating-point-notes">9.4.5. Floating Point Notes</a><ul> <li><a class="reference internal" href="#mitigating-round-off-error-with-increased-precision">9.4.5.1. Mitigating round-off error with increased precision</a></li> <li><a class="reference internal" href="#special-values">9.4.5.2. Special values</a></li> </ul> </li> <li><a class="reference internal" href="#working-with-threads">9.4.6. Working with threads</a></li> <li><a class="reference internal" href="#recipes">9.4.7. Recipes</a></li> <li><a class="reference internal" href="#decimal-faq">9.4.8. Decimal FAQ</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="cmath.html" title="previous chapter">9.3. <tt class="docutils literal"><span class="pre">cmath</span></tt> — Mathematical functions for complex numbers</a></p> <h4>Next topic</h4> <p class="topless"><a href="fractions.html" title="next chapter">9.5. <tt class="docutils literal"><span class="pre">fractions</span></tt> — Rational numbers</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/decimal.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="fractions.html" title="9.5. fractions — Rational numbers" >next</a> |</li> <li class="right" > <a href="cmath.html" title="9.3. cmath — Mathematical functions for complex numbers" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="numeric.html" >9. Numeric and Mathematical Modules</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close