brainhops._core.numeric
Recovery of the short decimals behind numbers stored in files.
Files often store numbers in single precision, sometimes in a different
unit from the one used in memory, such as milliseconds instead of
seconds or radians instead of degrees. Once widened to double precision,
a stored 0.3 reads as 0.30000001192092896. The functions in this
module recover the short decimal that was originally written.
Functions:
shortest_decimal
Return the shortest decimal that is stored as the same float32.
A writer stores a decimal d as float32(encode(d)), where encode
converts d into the unit of the file. A reader obtains the double
value, which is close to d but rarely equal to it. The returned
decimal is the shortest number that the writer could have written, and
writing it again stores the same bits. With the default identity
encoding, the result is the shortest representation of a float32 that
round-trips, which is what str(numpy.float32(value)) prints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Double decoded from the file. |
required |
encode
|
callable
|
Conversion from the unit of |
None
|
Returns:
| Type | Description |
|---|---|
float
|
The shortest decimal. Zero and non-finite values are returned as they are. |
Examples:
>>> import math
>>> import numpy as np
>>> shortest_decimal(float(np.float32(0.3)))
0.3
>>> stored = np.float32(2000.7) # a repetition time, in milliseconds
>>> float(stored) * 1e-3
2.000699951171875
>>> shortest_decimal(float(stored) * 1e-3, lambda s: s / 1e-3)
2.0007
>>> radians = np.float32(math.radians(9)) # a flip angle
>>> math.degrees(float(radians))
9.000000250447817
>>> shortest_decimal(math.degrees(float(radians)), math.radians)
9.0