MCPcopy Create free account
hub / github.com/numpy/numpy / _get_machar

Function _get_machar

numpy/core/getlimits.py:307–361  ·  view source on GitHub ↗

Get MachAr instance or MachAr-like instance Get parameters for floating point type, by first trying signatures of various known floating point types, then, if none match, attempting to identify parameters by analysis. Parameters ---------- ftype : class Numpy float

(ftype)

Source from the content-addressed store, hash-verified

305
306
307def _get_machar(ftype):
308 """ Get MachAr instance or MachAr-like instance
309
310 Get parameters for floating point type, by first trying signatures of
311 various known floating point types, then, if none match, attempting to
312 identify parameters by analysis.
313
314 Parameters
315 ----------
316 ftype : class
317 Numpy floating point type class (e.g. ``np.float64``)
318
319 Returns
320 -------
321 ma_like : instance of :class:`MachAr` or :class:`MachArLike`
322 Object giving floating point parameters for `ftype`.
323
324 Warns
325 -----
326 UserWarning
327 If the binary signature of the float type is not in the dictionary of
328 known float types.
329 """
330 params = _MACHAR_PARAMS.get(ftype)
331 if params is None:
332 raise ValueError(repr(ftype))
333 # Detect known / suspected types
334 # ftype(-1.0) / ftype(10.0) is better than ftype('-0.1') because stold
335 # may be deficient
336 key = (ftype(-1.0) / ftype(10.)).newbyteorder('<').tobytes()
337 ma_like = None
338 if ftype == ntypes.longdouble:
339 # Could be 80 bit == 10 byte extended precision, where last bytes can
340 # be random garbage.
341 # Comparing first 10 bytes to pattern first to avoid branching on the
342 # random garbage.
343 ma_like = _KNOWN_TYPES.get(key[:10])
344 if ma_like is None:
345 # see if the full key is known.
346 ma_like = _KNOWN_TYPES.get(key)
347 if ma_like is None and len(key) == 16:
348 # machine limits could be f80 masquerading as np.float128,
349 # find all keys with length 16 and make new dict, but make the keys
350 # only 10 bytes long, the last bytes can be random garbage
351 _kt = {k[:10]: v for k, v in _KNOWN_TYPES.items() if len(k) == 16}
352 ma_like = _kt.get(key[:10])
353 if ma_like is not None:
354 return ma_like
355 # Fall back to parameter discovery
356 warnings.warn(
357 f'Signature {key} for {ftype} does not match any known type: '
358 'falling back to type probe function.\n'
359 'This warnings indicates broken support for the dtype!',
360 UserWarning, stacklevel=2)
361 return _discovered_machar(ftype)
362
363
364def _discovered_machar(ftype):

Callers 1

_initMethod · 0.85

Calls 4

_discovered_macharFunction · 0.85
warnMethod · 0.80
getMethod · 0.45
tobytesMethod · 0.45

Tested by

no test coverage detected