rounding⚓︎
Functions for rounding numbers.
UncertainQtty
⚓︎
Data container associating uncertainty (confidence) to a quantity.
Includes intelligent rounding and printing functionality.
Usually, the precision parameter will be set to the (potentially estimated)
standard deviation of an uncertain quantity.
However, this class in itself does not define the prec
attribute
by anything else than what it does: impact the rounding & printing of val
.
Examples:
>>> for c in [.01, .1, .2, .9, 1]:
... print(UncertainQtty(1.2345, c))
1.23 ±0.01
1.2 ±0.1
1.2 ±0.2
1.2 ±0.9
1 ±1
>>> for c in [.01, 1e-10, 1e-17, 0]:
... print(UncertainQtty(1.2, c))
1.20 ±0.01
1.2000000000 ±1e-10
1.19999999999999996 ±1e-17
1.2000000000 ±0
Note that in the case of a confidence of exactly 0,
it defaults to 10 decimal places.
Meanwhile, a NaN confidence yields printing using rc.sigfig
:
Also note the effect of large uncertainty:
>>> for c in [1, 9, 10, 11, 20, 100, np.inf]:
... print(UncertainQtty(12, c))
12 ±1
12 ±9
10 ±10
10 ±10
10 ±20
0 ±100
0 ±inf
__repr__()
⚓︎
Essentially the same as __str__
.
__str__()
⚓︎
Returns 'val ±prec', using tools.rounding.UncertainQtty.round
and some finesse.
round()
⚓︎
Round intelligently.
prec
to 1 sig.fig.val
toround2(val, prec)
.
is_whole(x, **kwargs)
⚓︎
Check if a number is a whole/natural number to precision given by np.isclose
.
For actual type checking, use isinstance(x, (int, np.integer))
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float or ndarray
|
Values to be checked |
required |
Returns:
Name | Type | Description |
---|---|---|
l |
bool
|
True if rounded x is close to x, otherwise False |
log10int(x)
⚓︎
Compute decimal order, rounded down.
Conversion to int
means that we cannot return nan's or ± infinity,
even though this could be meaningful. Instead, we return integers of magnitude
a little less than IEEE floating point max/min-ima instead.
This avoids a lot of clauses in the parent/callers to this function.
Examples:
np_vectorize(f)
⚓︎
Like np.vectorize
, but with some embellishments.
- Includes
functools.wraps
- Applies
.item()
to output if input was a scalar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f
|
callable
|
Your function. |
required |
Returns:
Name | Type | Description |
---|---|---|
vectorized |
callable
|
Your function, now element-wise applicable to an iterable. |
round2(x, prec=1.0)
⚓︎
Round x to the decimal order appropriate for the precision.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
array_like
|
Value to be rounded. |
required |
prec
|
float
|
Precision, before prettify, which is given by $$ \text{prec} = 10^{\text{floor}(-\log_{10}|\text{prec}|)} $$ |
1.0
|
Returns:
Type | Description |
---|---|
Rounded value (always a float).
|
|
See Also
round2sigfig
Examples:
round2sigfig(x, sigfig=1)
⚓︎
Round to significant figures.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Value to be rounded. |
required | |
sigfig
|
Number of significant figures to include. |
1
|
Returns:
Type | Description |
---|---|
rounded value (always a float).
|
|
See Also
np.round : rounds to a given number of decimals.
round2
: rounds to a given precision.
Examples: