TheAlgorithms/Python · error · ValueError
Invalid velocity. Should be a positive number.
Error message
Invalid velocity. Should be a positive number.
What it means
Raised by the input validator in physics/horizontal_projectile_motion.py before any projectile calculation when init_velocity is negative. The library treats init_velocity as a speed magnitude combined with a launch angle (1-90 degrees), so a negative magnitude is meaningless. Note the guard is `init_velocity < 0`, so 0 is silently accepted (yielding zero distance) even though the message says 'positive'.
Source
Thrown at physics/horizontal_projectile_motion.py:45
def check_args(init_velocity: float, angle: float) -> None:
"""
Check that the arguments are valid
"""
# Ensure valid instance
if not isinstance(init_velocity, (int, float)):
raise TypeError("Invalid velocity. Should be an integer or float.")
if not isinstance(angle, (int, float)):
raise TypeError("Invalid angle. Should be an integer or float.")
# Ensure valid angle
if angle > 90 or angle < 1:
raise ValueError("Invalid angle. Range is 1-90 degrees.")
# Ensure valid velocity
if init_velocity < 0:
raise ValueError("Invalid velocity. Should be a positive number.")
def horizontal_distance(init_velocity: float, angle: float) -> float:
r"""
Returns the horizontal distance that the object cover
Formula:
.. math::
\frac{v_0^2 \cdot \sin(2 \alpha)}{g}
v_0 - \text{initial velocity}
\alpha - \text{angle}
>>> horizontal_distance(30, 45)
91.77
>>> horizontal_distance(100, 78)
414.76View on GitHub (pinned to f5988cc097)
Solutions
- Pass the speed magnitude (>= 0) and express direction through the angle parameter, e.g. horizontal_distance(30, 45) instead of horizontal_distance(-30, 45).
- Pre-validate inputs with abs(init_velocity) or an explicit `if init_velocity < 0` check before calling.
- Wrap the call in try/except ValueError to convert the message into a user-facing error.
Example fix
# before horizontal_distance(-30, 45) # ValueError # after horizontal_distance(abs(-30), 45) # or horizontal_distance(30, 45)
Defensive patterns
Strategy: validation
Validate before calling
def valid_projectile_inputs(v: float, angle: float) -> bool:
return isinstance(v, (int, float)) and v >= 0 and 1 <= angle <= 90
if valid_projectile_inputs(init_velocity, angle):
horizontal_distance(init_velocity, angle) Try / catch
try:
horizontal_distance(v, angle)
except ValueError as e:
raise ValueError(f"Projectile input rejected: {e} (v={v}, angle={angle})") from e Prevention
- Keep speed and direction separate: magnitude to init_velocity, direction to angle.
- Sanitize external data: abs() the velocity before passing it in.
- Remember 0 velocity passes the guard but returns 0 distance.
When it happens
Trigger: Calling horizontal_distance(-30, 45) or any function in the module that runs this validator with a negative init_velocity. Non-numeric inputs raise TypeError first; the angle check (1-90) runs independently of this one.
Common situations: Passing a signed velocity component (e.g. modeling leftward motion as -v) instead of magnitude+angle; piping raw sensor or CSV data that encodes direction in the sign; off-by-one defaults like -1 used as an 'unset' sentinel.
Related errors
- Expected a_coeffs to have {self.order + 1} elements for {sel
- n must not be negative
- Depth cannot be less than 0
- All input parameters must be positive
- Invalid inputs. Enter positive value.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/aa277adc1e5365f3.
Report an issue: GitHub.