TheAlgorithms/Python · error · ValueError
power_factor must be a valid float value between -1 and 1.
Error message
power_factor must be a valid float value between -1 and 1.
What it means
Thrown by real_power() in electronics/real_and_reactive_power.py when power_factor is not an int/float or falls outside [-1, 1]. Real power P = S * pf is only defined for a cosine-like power factor in the closed range [-1, 1]; the isinstance check also means booleans pass (bool is an int subclass) but strings/None fail even if they look numeric.
Source
Thrown at electronics/real_and_reactive_power.py:21
def real_power(apparent_power: float, power_factor: float) -> float:
"""
Calculate real power from apparent power and power factor.
Examples:
>>> real_power(100, 0.9)
90.0
>>> real_power(0, 0.8)
0.0
>>> real_power(100, -0.9)
-90.0
"""
if (
not isinstance(power_factor, (int, float))
or power_factor < -1
or power_factor > 1
):
raise ValueError("power_factor must be a valid float value between -1 and 1.")
return apparent_power * power_factor
def reactive_power(apparent_power: float, power_factor: float) -> float:
"""
Calculate reactive power from apparent power and power factor.
Examples:
>>> reactive_power(100, 0.9)
43.58898943540673
>>> reactive_power(0, 0.8)
0.0
>>> reactive_power(100, -0.9)
43.58898943540673
"""
if (
not isinstance(power_factor, (int, float))
or power_factor < -1View on GitHub (pinned to f5988cc097)
Solutions
- Pass power_factor as a float in [-1, 1] — e.g. real_power(100, 0.9) -> 90.0.
- Convert numeric strings first: float(value); divide percentages by 100.
- Clamp floating-point drift: max(-1.0, min(1.0, pf)) when the value is computed, not user-entered.
Example fix
# before
real_power(100, '0.9') # ValueError
real_power(100, 90) # percentage passed raw -> ValueError
# after
real_power(100, float('0.9')) # 90.0
real_power(100, 90 / 100) # 90.0 Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(power_factor, (int, float)) or not -1 <= power_factor <= 1:
raise ValueError('power_factor must be a number in [-1, 1]') Type guard
def valid_power_factor(pf: object) -> bool:
return isinstance(pf, (int, float)) and not isinstance(pf, bool) and -1 <= pf <= 1 Try / catch
try:
p = real_power(s, pf)
except ValueError as exc:
if 'power_factor' in str(exc):
pf = float(pf) if isinstance(pf, str) else max(-1.0, min(1.0, pf))
p = real_power(s, pf)
else:
raise Prevention
- Convert numeric strings with float() at the boundary.
- Divide percentage pf by 100 before calling.
- Clamp computed pf to [-1, 1] to absorb float drift.
When it happens
Trigger: real_power(100, 1.5); real_power(100, '0.9') (string not isinstance of (int, float)); real_power(100, None); any pf where not isinstance(power_factor, (int,float)) or pf < -1 or pf > 1.
Common situations: Reading power factor from config/JSON as a string and passing it unconverted; forgetting that pf can be expressed as a percentage (90 instead of 0.9); calculations that drift slightly outside the range due to floating-point rounding (e.g. 1.0000000000000002).
Related errors
- Power cannot be negative in any electrical/electronics syste
- One and only one argument must be 0
- One and only one argument must be 0
- Inductance cannot be negative
- Frequency cannot be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/ec13d6dfa2f3288e.
Report an issue: GitHub.