TheAlgorithms/Python · error · ValueError

Impossible fluid density

Error message

Impossible fluid density

What it means

Raised by physics/speed_of_sound.py:speed_of_sound_in_a_fluid when density <= 0. The function computes sqrt(bulk_modulus / density); a zero density would divide by zero and a negative one would produce a complex result, so non-positive density is rejected up front. Density is the first of the two guards (density, then bulk_modulus), so this error fires even if bulk_modulus is also invalid.

Source

Thrown at physics/speed_of_sound.py:38

"""


def speed_of_sound_in_a_fluid(density: float, bulk_modulus: float) -> float:
    """
    Calculates the speed of sound in a fluid from its density and bulk modulus

    Examples:
    Example 1 --> Water 20°C: bulk_modulus= 2.15MPa, density=998kg/m³
    Example 2 --> Mercury 20°C: bulk_modulus= 28.5MPa, density=13600kg/m³

    >>> speed_of_sound_in_a_fluid(bulk_modulus=2.15e9, density=998)
    1467.7563207952705
    >>> speed_of_sound_in_a_fluid(bulk_modulus=28.5e9, density=13600)
    1447.614670861731
    """

    if density <= 0:
        raise ValueError("Impossible fluid density")
    if bulk_modulus <= 0:
        raise ValueError("Impossible bulk modulus")

    return (bulk_modulus / density) ** 0.5


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass a strictly positive density in kg/m^3 (e.g. 998 for water, 13600 for mercury).
  2. If density is computed, guard the expression that produces it before calling the function.
  3. Validate both inputs together; remember density is checked before bulk_modulus so fixing only bulk_modulus will still raise this error.
  4. Catch ValueError at the caller if near-zero densities are expected in your data and you want to skip those samples.

Example fix

# before
speed_of_sound_in_a_fluid(bulk_modulus=2.15e9, density=0)

# after
if density > 0 and bulk_modulus > 0:
    c = speed_of_sound_in_a_fluid(bulk_modulus, density)
Defensive patterns

Strategy: validation

Validate before calling

if density <= 0:
    raise ValueError(f'density must be > 0 kg/m^3, got {density}')
c = speed_of_sound_in_a_fluid(bulk_modulus=b, density=density)

Type guard

def is_valid_fluid(density: float, bulk_modulus: float) -> bool:
    return density > 0 and bulk_modulus > 0

Try / catch

try:
    speed_of_sound_in_a_fluid(b, d)
except ValueError as e:
    if 'density' in str(e):
        ...

Prevention

When it happens

Trigger: Calling speed_of_sound_in_a_fluid(bulk_modulus=2.15e9, density=0) or with any negative density. Also reached when both arguments are invalid, e.g. speed_of_sound_in_a_fluid(0, 0), because density is checked first.

Common situations: Using kg/m^3 vs g/cm^3 inconsistently and hitting 0 through unit conversion mistakes; reading density from a table/config where the field is missing and defaults to 0; passing a density difference (rho1-rho2) that goes negative; modeling vacuum conditions where density legitimately tends to 0.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/0819af42bff9d5a9. Report an issue: GitHub.