1
""" Machine limits for Float32 and Float64 and (long double) if available...
4
__all__ = ['finfo','iinfo']
6
from machar import MachAr
8
import numerictypes as ntypes
9
from numeric import array
12
"""fix rank-0 --> rank-1"""
13
if a.ndim == 0: a.shape = (1,)
17
ntypes.csingle: ntypes.single,
18
ntypes.complex_: ntypes.float_,
19
ntypes.clongfloat: ntypes.longfloat
26
Machine limits for floating point types.
30
eps : floating point number of the appropriate type
31
The smallest representable number such that ``1.0 + eps != 1.0``.
32
epsneg : floating point number of the appropriate type
33
The smallest representable number such that ``1.0 - epsneg != 1.0``.
35
The number of bits in the exponent portion of the floating point
38
The object which calculated these parameters and holds more detailed
41
The exponent that yields ``eps``.
42
max : floating point number of the appropriate type
43
The largest representable number.
45
The smallest positive power of the base (2) that causes overflow.
46
min : floating point number of the appropriate type
47
The smallest representable number, typically ``-max``.
49
The most negative power of the base (2) consistent with there being
50
no leading 0's in the mantissa.
52
The exponent that yields ``epsneg``.
54
The number of bits in the exponent including its sign and bias.
56
The number of bits in the mantissa.
58
The approximate number of decimal digits to which this kind of float
60
resolution : floating point number of the appropriate type
61
The approximate decimal resolution of this type, i.e.
63
tiny : floating point number of the appropriate type
64
The smallest-magnitude usable number.
68
dtype : floating point type, dtype, or instance
69
The kind of floating point data type to get information about.
73
MachAr : The implementation of the tests that produce this information.
74
iinfo : The equivalent for integer data types.
78
For developers of NumPy: do not instantiate this at the module level. The
79
initial calculation of these parameters is expensive and negatively impacts
80
import times. These objects are cached, so calling ``finfo()`` repeatedly
81
inside your functions is not a problem.
87
def __new__(cls, dtype):
89
dtype = numeric.dtype(dtype)
91
# In case a float instance was given
92
dtype = numeric.dtype(type(dtype))
94
obj = cls._finfo_cache.get(dtype,None)
98
newdtype = numeric.obj2sctype(dtype)
99
if newdtype is not dtype:
100
dtypes.append(newdtype)
102
if not issubclass(dtype, numeric.inexact):
103
raise ValueError, "data type %r not inexact" % (dtype)
104
obj = cls._finfo_cache.get(dtype,None)
107
if not issubclass(dtype, numeric.floating):
108
newdtype = _convert_to_float[dtype]
109
if newdtype is not dtype:
110
dtypes.append(newdtype)
112
obj = cls._finfo_cache.get(dtype,None)
115
obj = object.__new__(cls)._init(dtype)
117
cls._finfo_cache[dt] = obj
120
def _init(self, dtype):
121
self.dtype = numeric.dtype(dtype)
122
if dtype is ntypes.double:
126
elif dtype is ntypes.single:
130
elif dtype is ntypes.longdouble:
131
itype = ntypes.longlong
133
precname = 'long double'
135
raise ValueError, repr(dtype)
137
machar = MachAr(lambda v:array([v], dtype),
138
lambda v:_frz(v.astype(itype))[0],
139
lambda v:array(_frz(v)[0], dtype),
140
lambda v: fmt % array(_frz(v)[0], dtype),
141
'numpy %s precision floating point number' % precname)
143
for word in ['precision', 'iexp',
144
'maxexp','minexp','negep',
146
setattr(self,word,getattr(machar, word))
147
for word in ['tiny','resolution','epsneg']:
148
setattr(self,word,getattr(machar, word).squeeze())
149
self.max = machar.huge.flat[0]
151
self.eps = machar.eps.flat[0]
152
self.nexp = machar.iexp
153
self.nmant = machar.it
155
self._str_tiny = machar._str_xmin.strip()
156
self._str_max = machar._str_xmax.strip()
157
self._str_epsneg = machar._str_epsneg.strip()
158
self._str_eps = machar._str_eps.strip()
159
self._str_resolution = machar._str_resolution.strip()
164
Machine parameters for %(dtype)s
165
---------------------------------------------------------------------
166
precision=%(precision)3s resolution= %(_str_resolution)s
167
machep=%(machep)6s eps= %(_str_eps)s
168
negep =%(negep)6s epsneg= %(_str_epsneg)s
169
minexp=%(minexp)6s tiny= %(_str_tiny)s
170
maxexp=%(maxexp)6s max= %(_str_max)s
171
nexp =%(nexp)6s min= -max
172
---------------------------------------------------------------------
180
Machine limits for integer types.
185
The smallest integer expressible by the type.
187
The largest integer expressible by the type.
191
type : integer type, dtype, or instance
192
The kind of integer data type to get information about.
196
finfo : The equivalent for floating point data types.
202
>>> ii16 = np.iinfo(np.int16)
207
>>> ii32 = np.iinfo(np.int32)
215
>>> ii32 = np.iinfo(np.int32(10))
226
def __init__(self, int_type):
228
self.dtype = numeric.dtype(int_type)
230
self.dtype = numeric.dtype(type(int_type))
231
self.kind = self.dtype.kind
232
self.bits = self.dtype.itemsize * 8
233
self.key = "%s%d" % (self.kind, self.bits)
234
if not self.kind in 'iu':
235
raise ValueError("Invalid integer data type.")
238
"""Minimum value of given dtype."""
243
val = iinfo._min_vals[self.key]
245
val = int(-(1L << (self.bits-1)))
246
iinfo._min_vals[self.key] = val
252
"""Maximum value of given dtype."""
254
val = iinfo._max_vals[self.key]
257
val = int((1L << self.bits) - 1)
259
val = int((1L << (self.bits-1)) - 1)
260
iinfo._max_vals[self.key] = val
266
"""String representation."""
268
Machine parameters for %(dtype)s
269
---------------------------------------------------------------------
272
---------------------------------------------------------------------
273
''' % {'dtype': self.dtype, 'min': self.min, 'max': self.max}
276
if __name__ == '__main__':
277
f = finfo(ntypes.single)
278
print 'single epsilon:',f.eps
279
print 'single tiny:',f.tiny
280
f = finfo(ntypes.float)
281
print 'float epsilon:',f.eps
282
print 'float tiny:',f.tiny
283
f = finfo(ntypes.longfloat)
284
print 'longfloat epsilon:',f.eps
285
print 'longfloat tiny:',f.tiny