fuzz.values
(Values)¶
Contains the Value class.
-
class
fuzz.values.
Value
(value, error=0)[source]¶ A Value represents a numerical measurement of some kind, with its associated uncertainty/error.
For example a value of 23 ± 0.2 would be
Value(23, 0.2)
.Values mostly support the same operators that numbers do - you can add them, divide them, raise them to powers, compare them etc. There are a few important differences however. Firstly, the error values of the resultant operations will be derived from the standard guidelines for combining uncertainties. Specifically, values will be assumed to be independent, and so errors will be summed etc. in quadrature.
Secondly, comparing two values with
==
,<
etc. will compare the values only - the error values will not be taken into account. I thought it would be too confusing otherwise. However, all values have aconsistent_with()
method which will look at the error values. If two values are consistent, then one should not be considered larger than the other, regardless of what>
says.The intention behind the Value class was that if you wanted to, you could forget that it was anything other than an
int
orfloat
, and only access the error associated with it if you need it.You can create a Value from a value, and the argument will be treated exactly like an
int
orfloat
. That is, unless you also supply an error value, the resultant Value will have an error of 0. I considered having the error of the Value passed in become the new error, but decided it would become too easy to lose track of the errors. So,Value(Value(23, 0.2))
would produce a Value with an error of 0 (and a value of 23).One final note on terminology - I know it is confusing that the Value class has a property called
value()
, but that is the terminology in use as of this version.- Parameters
value – The value.
error – The uncertainty associated with the value. By default this is zero.
- Raises
TypeError – if either the value or its error is not numeric.
ValueError – if the error is negative.
-
static
create
(value, error=0)[source]¶ This is a static method, and serves as an alternate constructor for Values. It tries to convert some value to an actual Value, and if it can’t because it is the wrong type, it just sends the object back unaltered.
- Parameters
value – The value to convert.
error – The error associated with the value.
- Returns
Either the converted
Value
or the original object.
-
value
()[source]¶ Returns the value’s… value. That is, the measurement itself, without its associated error.
- Return type
int
orfloat
-
relative_error
()[source]¶ Returns the value’s associated error as a proportion of the value. If the value is 0, the relative error will be 0 too.
- Return type
float
-
error_range
()[source]¶ Returns the range of possible values implied by the uncertainty.
- Return type
tuple
-
consistent_with
(other)[source]¶ Checks if the value is consistent with another value. Two values are considered consistent if the difference between them is less than or equal to the sum of their uncertainties/errors.
If two values are consistent, there cannot be said to be a difference between them, whereas if they are not consistent, there is a meaningful difference between them.
You can also provide an
int
orfloat
, which will be assumed to have an error of zero.- Parameters
other (Value) – The other value to check against.
- Raises
TypeError – if the other value given is not a
Value
,int
orfloat
.- Return type
bool