toleranced module#
Toleranced values and interval arithmetic#
This module provides the Toleranced class for representing values with tolerances and performing interval arithmetic operations.
- class Toleranced(typ: float, plusminus: float | None, /)[source]#
- class Toleranced(typ: float, plus: float | None, minus: float | None)
Bases:
IntervalInterval Arithmetic Type for values with tolerances.
- Parameters:
>>> # Create a resistor with ±5% tolerance >>> r1 = Toleranced.percent(10_000, 5) # 10kΩ ± 5% >>> print(r1) 10000 ± 5%
>>> # Create a voltage with asymmetric bounds >>> v1 = Toleranced.min_typ_max(2.7, 3.3, 3.6) # 2.7V to 3.6V, typical 3.3V >>> print(v1) Toleranced(2.7 <= 3.3 <= 3.6)
>>> # Arithmetic with tolerances >>> r2 = Toleranced.percent(1_000, 1) # 1kΩ ± 1% >>> total = r1 + r2 >>> print(f"Total: {total.min_value:.0f}Ω to {total.max_value:.0f}Ω") Total: 10490Ω to 11510Ω
>>> # Voltage divider calculation >>> v_out = v1 * r2 / (r1 + r2) >>> print(v_out) Toleranced(0.3, 0.0466158, 0.0677672)
-
plus:
float|None# Positive tolerance from typical to maximum value. None indicates unbounded maximum.
-
minus:
float|None# Negative tolerance from typical to minimum value. None indicates unbounded minimum.
- center_value()[source]#
Calculate the geometric center of the range (midpoint between min and max).
- Return type:
- plus_percent()[source]#
Calculate the positive tolerance as a percentage of the typical value.
- Return type:
- Returns:
Percentage value (e.g., 5.0 for 5%).
- Raises:
ValueError – If plus is None or typ is zero.
- minus_percent()[source]#
Calculate the negative tolerance as a percentage of the typical value.
- Return type:
- Returns:
Percentage value (e.g., 5.0 for 5%).
- Raises:
ValueError – If minus is None or typ is zero.
- in_range(value)[source]#
Check if a value or range is contained within this tolerance range.
- Parameters:
value (
float|Toleranced) – A float or another Toleranced value to check.- Return type:
- Returns:
True if the value/range is completely within this range.
>>> tol = Toleranced(10, 1, 1) # 9 to 11 >>> tol.in_range(10.5) True >>> tol.in_range(Toleranced(10, 0.5, 0.5)) # 9.5 to 10.5 True
- apply(f)[source]#
Apply a function to the toleranced value.
Applies the given function to min, typ, and max values and creates a new Toleranced from the results. Useful for non-linear transformations.
- Parameters:
f (
Callable[[float],float]) – A function that takes a float and returns a float.- Return type:
Self- Returns:
New Toleranced value with the function applied.
>>> import math >>> v = Toleranced(4.0, 0.5, 0.5) # 3.5 to 4.5 >>> sqrt_v = v.apply(math.sqrt) # sqrt applied to range >>> print(f"{sqrt_v.min_value:.2f} to {sqrt_v.max_value:.2f}") 1.87 to 2.12
- classmethod min_typ_max(min_val, typ_val, max_val)[source]#
Create a Toleranced value from min, typ, and max values.
At least two of the three values must be specified. If typ is not provided, it will be calculated as the midpoint of min and max.
- Parameters:
- Return type:
Self- Returns:
New Toleranced value.
- Raises:
ValueError – If fewer than two values are specified, or if min > typ, typ > max, or min > max.
>>> # Supply voltage specification >>> vdd = Toleranced.min_typ_max(2.7, 3.3, 3.6) >>> print(vdd) Toleranced(2.7 <= 3.3 <= 3.6)
>>> # Temperature range without typical >>> temp = Toleranced.min_typ_max(-40, None, 85) >>> print(f"Center temp: {temp.typ}°C") Center temp: 22.5°C
- classmethod min_max(min_val, max_val)[source]#
Create a Toleranced defined by absolute min and max values.
The typical value will be set to the midpoint of min and max.
- Parameters:
- Return type:
Self- Returns:
New Toleranced value.
>>> # Operating frequency range >>> freq = Toleranced.min_max(10e6, 20e6) # 10-20 MHz >>> print(f"Center: {freq.typ/1e6} MHz") Center: 15.0 MHz
- classmethod min_typ(min_val, typ_val)[source]#
Create a Toleranced defined by an absolute minimum and typical value.
The maximum is unbounded (None).
- Parameters:
- Return type:
Self- Returns:
New Toleranced value with unbounded maximum.
>>> # Minimum load current >>> i_load = Toleranced.min_typ(0.1, 0.5) # At least 100mA, typ 500mA
- classmethod typ_max(typ_val, max_val)[source]#
Create a Toleranced defined by a typical and absolute maximum value.
The minimum is unbounded (None).
- Parameters:
- Return type:
Self- Returns:
New Toleranced value with unbounded minimum.
>>> # Maximum current consumption >>> i_max = Toleranced.typ_max(50e-6, 100e-6) # Typ 50µA, max 100µA
- classmethod percent(typ, plus, minus=Unspecified)[source]#
Create a Toleranced based on symmetric or asymmetric percentages of the typical value.
If the minus argument is unspecified, the range will be symmetric.
- Parameters:
- Return type:
Self- Returns:
New Toleranced value.
- Raises:
ValueError – If plus or minus is not in the range 0-100.
>>> # 10kΩ resistor with ±5% tolerance >>> r1 = Toleranced.percent(10_000, 5) >>> print(r1) 10000 ± 5%
>>> # Voltage with asymmetric tolerance >>> v = Toleranced.percent(3.3, 10, 5) # +10%, -5% >>> print(f"{v.min_value}V to {v.max_value}V") 3.135V to 3.63V
- classmethod sym(typ, plusminus)[source]#
Create a Toleranced with symmetric bounds.
Effectively an alias of the two-argument constructor.
- Parameters:
- Return type:
Self- Returns:
New Toleranced value with symmetric tolerance.
>>> # Component with ±0.1mm tolerance >>> dim = Toleranced.sym(5.0, 0.1) # 4.9 to 5.1 mm
- classmethod exact(typ)[source]#
Create a Toleranced with zero tolerance (exact value).
If given a Toleranced value, returns it unchanged. If given a float, creates a Toleranced with plus=0 and minus=0.
- Parameters:
typ (
float|Toleranced) – Value to wrap as exact, or existing Toleranced.- Return type:
Union[Self,Toleranced]- Returns:
Toleranced value with zero tolerance, or the input if already Toleranced.
>>> # Ideal reference voltage (no tolerance) >>> v_ref = Toleranced.exact(2.5) >>> print(v_ref) 2.5 ± 0