File: Precision.py

package info (click to toggle)
python-numarray 1.5.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,668 kB
  • ctags: 11,384
  • sloc: ansic: 113,864; python: 22,422; makefile: 197; sh: 11
file content (112 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
""" A simple hack to start thinking about a better way to handle
specification of typecodes.

TODO:
    The code_table should proably be cached somehow
    Float/Complex needs to have precision and range specifiers
"""

from numarray import zeros
import string

# dvhart - added the character 'u' for the UnisgnedInteger 'list of codes' ?
# typecodes = {'Integer':'1sil', 'UnsignedInteger':'Bbwu', 'Float':'fd', 'Complex':'FD'}
from numarray.numerictypes import typecodes

def _get_precisions(typecodes):
    lst = []
    for t in typecodes:
        lst.append( (zeros( (1,), t ).itemsize()*8, t) )
    return lst

def _fill_table(typecodes, table={}):
    for key, value in typecodes.items():
        table[key] = _get_precisions(value)
    return table

_code_table = _fill_table(typecodes)

class PrecisionError(Exception):
    pass

def _lookup(table, key, required_bits):
    lst = table[key]
    for bits, typecode in lst:
        if bits >= required_bits:
            return typecode
    raise PrecisionError, key+" of "+str(required_bits)+" bits not available on this system"

# Character = 'c'

try:
    UnsignedInt8 = _lookup(_code_table, "UnsignedInteger", 8)
    UInt8 = UnsignedInt8
except(PrecisionError):
    pass
try:
    UnsignedInt16 = _lookup(_code_table, "UnsignedInteger", 16)
    UInt16 = UnsignedInt16
except(PrecisionError):
    pass
try:
    UnsignedInt32 = _lookup(_code_table, "UnsignedInteger", 32)
    UInt32 = UnsignedInt32
except(PrecisionError):
    pass
try:
    UnsignedInt64 = _lookup(_code_table, "UnsignedInteger", 64)
    UInt64 = UnsignedInt64
except(PrecisionError):
    pass
try:
    UnsignedInt128 = _lookup(_code_table, "UnsignedInteger", 128)
    UInt128 = UnsignedInt128
except(PrecisionError):
    pass
UnsignedInteger = 'u'
UInt = UnsignedInteger


try: Int0 = _lookup(_code_table, 'Integer', 0)
except(PrecisionError): pass
try: Int8 = _lookup(_code_table, 'Integer', 8)
except(PrecisionError): pass
try: Int16 = _lookup(_code_table, 'Integer', 16)
except(PrecisionError): pass
try: Int32 = _lookup(_code_table, 'Integer', 32)
except(PrecisionError): pass
try: Int64 = _lookup(_code_table, 'Integer', 64)
except(PrecisionError): pass
try: Int128 = _lookup(_code_table, 'Integer', 128)
except(PrecisionError): pass
Int = 'l'

try: Float0 = _lookup(_code_table, 'Float', 0)
except(PrecisionError): pass
try: Float8 = _lookup(_code_table, 'Float', 8)
except(PrecisionError): pass
try: Float16 = _lookup(_code_table, 'Float', 16)
except(PrecisionError): pass
try: Float32 = _lookup(_code_table, 'Float', 32)
except(PrecisionError): pass
try: Float64 = _lookup(_code_table, 'Float', 64)
except(PrecisionError): pass
try: Float128 = _lookup(_code_table, 'Float', 128)
except(PrecisionError): pass
Float = 'd'

try: Complex0 = _lookup(_code_table, 'Complex', 0)
except(PrecisionError): pass
try: Complex8 = _lookup(_code_table, 'Complex', 16)
except(PrecisionError): pass
try: Complex16 = _lookup(_code_table, 'Complex', 32)
except(PrecisionError): pass
try: Complex32 = _lookup(_code_table, 'Complex', 64)
except(PrecisionError): pass
try: Complex64 = _lookup(_code_table, 'Complex', 128)
except(PrecisionError): pass
try: Complex128 = _lookup(_code_table, 'Complex', 256)
except(PrecisionError): pass
Complex = 'D'

# PyObject = 'O'