TheAlgorithms/Python · error · ValueError
Impossible fluid density
Error message
Impossible fluid density
What it means
Raised by archimedes_principle when fluid_density is <= 0. The buoyant force formula (density * gravity * volume) requires a positive fluid density; zero density would mean no fluid, and negative density is unphysical. This is the first of three ordered validation checks (density, volume, gravity).
Source
Thrown at physics/archimedes_principle_of_buoyant_force.py:50
6844.061035
>>> archimedes_principle(fluid_density=997, volume=-0.7)
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 fluid density in kg/m^3 (e.g. 997 for water, 1.225 for air at sea level)
- Check config defaults: an unset density of 0 will raise
- For vacuum/free-fall scenarios, keep density/volume positive and set gravity=0 instead (that path is supported)
Example fix
# before archimedes_principle(fluid_density=0, volume=0.7, gravity=9.8) # ValueError # after archimedes_principle(fluid_density=997, volume=0.7, gravity=9.8)
Defensive patterns
Strategy: validation
Validate before calling
def valid_fluid_density(fluid_density) -> bool:
return fluid_density > 0 Prevention
- Use realistic densities in kg/m^3 (water 997, air 1.225) and never leave 0 defaults
- For free-fall scenarios set gravity=0 rather than density=0
When it happens
Trigger: Calling archimedes_principle(fluid_density=-997, ...) or archimedes_principle(fluid_density=0, ...). Gravity=0 is allowed (returns 0.0, e.g. orbit); only density and volume must be strictly positive.
Common situations: Passing densities in wrong units or sign conventions, defaulting density to 0 in a config struct, or vacuum scenarios encoded as 0 density — all rejected by design.
Related errors
- Impossible object volume
- 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/c08f079554ffe236.
Report an issue: GitHub.