TheAlgorithms/Python · error · ValueError
Impossible object volume
Error message
Impossible object volume
What it means
Raised by archimedes_principle when the volume argument is <= 0. The displaced-fluid volume must be strictly positive for the buoyant-force formula to be meaningful; zero or negative submerged volume is rejected. It is the second check, so a bad density raises first.
Source
Thrown at physics/archimedes_principle_of_buoyant_force.py:52
Traceback (most recent call last):
...
ValueError: Impossible object volume
>>> archimedes_principle(fluid_density=0, volume=0.7)
Traceback (most recent call last):
...
ValueError: Impossible fluid density
>>> archimedes_principle(fluid_density=997, volume=0.7, gravity=0)
0.0
>>> archimedes_principle(fluid_density=997, volume=0.7, gravity=-9.8)
Traceback (most recent call last):
...
ValueError: Impossible gravity
"""
if fluid_density <= 0:
raise ValueError("Impossible fluid density")
if volume <= 0:
raise ValueError("Impossible object volume")
if gravity < 0:
raise ValueError("Impossible gravity")
return fluid_density * gravity * volume
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a positive submerged volume in m^3; double-check the submerged fraction if only part of the object is immersed
- Fix upstream geometry: use abs() or reorder operands so length/height computations cannot go negative
- Guard degenerate shapes before the call if a zero-volume object is possible in your domain
Example fix
# before archimedes_principle(fluid_density=997, volume=top - bottom, gravity=9.8) # swapped -> negative # after archimedes_principle(fluid_density=997, volume=abs(top - bottom), gravity=9.8)
Defensive patterns
Strategy: validation
Validate before calling
def valid_volume(volume) -> bool:
return volume > 0 Prevention
- Compute volumes from dimensions with abs() or ordered operands to avoid sign flips
- Reject degenerate (zero-dimension) shapes upstream
When it happens
Trigger: Calling archimedes_principle(volume=0, ...) or archimedes_principle(volume=-2.5, ...), for example when an object is fully outside the fluid or a geometry computation returns 0.
Common situations: Volume computed from dimensions that are zero (degenerate shape) or negative due to an ordering bug (e.g. height computed as top - bottom with swapped inputs).
Related errors
- Impossible fluid density
- Impossible gravity
- Invalid value for min_val or max_val (min_value < max_value)
- argument value for lower and higher must be(lower > higher)
- guess value must be within the range of lower and higher val
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/c23eec50b86fe676.
Report an issue: GitHub.