Onyx logo

Previous topic

onyx.util.safediv – Utility routine for doing safe division of Numpy arrays

Next topic

Development Modules

This Page

onyx.util.floatutils – Simple routines to exactly serialize floats as strings in a readable

fashion. This wrapper also exposes the C99 fpclassify macro and the manifest constants it returns.

Examples:

>>> pi = 3.14159
>>> s1 = float_to_readable_string(-100*pi)
>>> s1
'-(+0008)0x3a28b43958106'
>>> pi0 = readable_string_to_float(s1)
>>> pi0 == -100*pi
True
>>> fpclassify(pi0) == FP_NORMAL
True
>>> fpclassify(0.0) == FP_NORMAL
False
>>> fpclassify(0.0) == FP_ZERO
True
>>> fpclassify(0) == FP_ZERO
True
>>> # trying to use mathutils.quiet_log() introduces an import circularity,
>>> # so we do the work directly
>>> with np.errstate(divide='ignore'):
...   inf = np.log(0.0)
>>> fpclassify(inf) == FP_INFINITE
True
>>> with np.errstate(invalid='ignore'):
...    nan = np.divide(inf, inf)
>>> fpclassify(nan) == FP_NAN
True
>>> subnorm_readable = '+(-1023)0x0000000000010'
>>> subnorm = readable_string_to_float(subnorm_readable)
>>> fpclassify(subnorm) == FP_SUBNORMAL
True
>>> fpclassify("bad_arg")
Traceback (most recent call last):
...
ValueError: unable to classify object of type <type 'str'>
>>> float_to_readable_string(subnorm)
'+(-1023)0x0000000000010'
>>> float_to_readable_string(inf)    
Traceback (most recent call last):
...
ValueError: unable to generate readable string for infinite value ...
>>> float_to_readable_string(nan)    
Traceback (most recent call last):
...
ValueError: unable to generate readable string for NaN value ...
>>> p0 = readable_string_to_float(s1[1:])
Traceback (most recent call last):
...
ValueError: unable to decode string (+0008)0x3a28b43958106  --  bad string length, expected 23 characters but got 22
>>> zero0 = 0.0
>>> s = float_to_hex_string(pi)
>>> s
'6e861bf0f9210940'
>>> s2 = float_to_hex_string(zero0)
>>> s2
'0000000000000000'
>>> pi1 = hex_string_to_float(s)
>>> float_to_readable_string(pi1)
'+(+0001)0x921f9f01b866e'
>>> pi == pi1
True
>>> zero = hex_string_to_float(s2)
>>> zero
0.0
>>> zero == zero0
True
>>> bad_str = '+(-1000)0xbadbadbadbad0'
>>> bad_score = readable_string_to_float(bad_str)
>>> bad_score < 2.0e-301
True
>>> log_bs = np.log(bad_score)
>>> float_to_readable_string(log_bs)
'-(+0009)0x5a4caf96d63e1'
onyx.util.floatutils.float_to_hex_string(f)
onyx.util.floatutils.float_to_readable_string(f)
onyx.util.floatutils.fpclassify(f)
onyx.util.floatutils.hex_string_to_float(s)
onyx.util.floatutils.pformat(object, indent=1, width=80, depth=None)

Format a Python object into a pretty-printed representation.

>>> test=( ['a', 3.4005000000000001], 1+1e-16, ( 6.1-1e-16, 6.1+1e-16) )
>>> pformat(test, width=1000)
"(['a', 3.4005], 1.0, (6.1, 6.1))"
onyx.util.floatutils.pformat_hammer(object, indent=1, width=80, depth=None)

Format a Python object into a pretty-printed representation.

>>> test=( ['a', 3.4005000000000001], 1+1e-16, ( 6.1-1e-16, 6.1+1e-16) )
>>> pformat(test, width=1000)
"(['a', 3.4005], 1.0, (6.1, 6.1))"
onyx.util.floatutils.pprint(object, stream=None, indent=1, width=80, depth=None)

Pretty-print a Python object to a stream [default is sys.stdout].

>>> test=( ['a', 3.4005000000000001], 1+1e-16, ( 6.1-1e-16, 6.1+1e-16) )
>>> pprint(test, width=1000)
(['a', 3.4005], 1.0, (6.1, 6.1))
onyx.util.floatutils.pprint_hammer(object, stream=None, indent=1, width=80, depth=None)

Pretty-print a Python object to a stream [default is sys.stdout].

>>> test=( ['a', 3.4005000000000001], 1+1e-16, ( 6.1-1e-16, 6.1+1e-16) )
>>> pprint(test, width=1000)
(['a', 3.4005], 1.0, (6.1, 6.1))
onyx.util.floatutils.readable_string_to_float(s)