{"record":{"id":"0819af42bff9d5a9","repo":"TheAlgorithms/Python","slug":"impossible-fluid-density-0819af","errorCode":null,"errorMessage":"Impossible fluid density","messagePattern":"Impossible fluid density","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/speed_of_sound.py","lineNumber":38,"sourceCode":"\"\"\"\n\n\ndef speed_of_sound_in_a_fluid(density: float, bulk_modulus: float) -> float:\n    \"\"\"\n    Calculates the speed of sound in a fluid from its density and bulk modulus\n\n    Examples:\n    Example 1 --> Water 20°C: bulk_modulus= 2.15MPa, density=998kg/m³\n    Example 2 --> Mercury 20°C: bulk_modulus= 28.5MPa, density=13600kg/m³\n\n    >>> speed_of_sound_in_a_fluid(bulk_modulus=2.15e9, density=998)\n    1467.7563207952705\n    >>> speed_of_sound_in_a_fluid(bulk_modulus=28.5e9, density=13600)\n    1447.614670861731\n    \"\"\"\n\n    if density <= 0:\n        raise ValueError(\"Impossible fluid density\")\n    if bulk_modulus <= 0:\n        raise ValueError(\"Impossible bulk modulus\")\n\n    return (bulk_modulus / density) ** 0.5\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":20,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/speed_of_sound.py#L20-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a strictly positive density in kg/m^3 (e.g. 998 for water, 13600 for mercury).","If density is computed, guard the expression that produces it before calling the function.","Validate both inputs together; remember density is checked before bulk_modulus so fixing only bulk_modulus will still raise this error.","Catch ValueError at the caller if near-zero densities are expected in your data and you want to skip those samples."],"exampleFix":"# before\nspeed_of_sound_in_a_fluid(bulk_modulus=2.15e9, density=0)\n\n# after\nif density > 0 and bulk_modulus > 0:\n    c = speed_of_sound_in_a_fluid(bulk_modulus, density)","handlingStrategy":"validation","validationCode":"if density <= 0:\n    raise ValueError(f'density must be > 0 kg/m^3, got {density}')\nc = speed_of_sound_in_a_fluid(bulk_modulus=b, density=density)","typeGuard":"def is_valid_fluid(density: float, bulk_modulus: float) -> bool:\n    return density > 0 and bulk_modulus > 0","tryCatchPattern":"try:\n    speed_of_sound_in_a_fluid(b, d)\nexcept ValueError as e:\n    if 'density' in str(e):\n        ...","preventionTips":["Validate both density and bulk_modulus together; density is checked first.","Use kg/m^3 consistently; document units at the call site.","Reject missing/zero table values before they reach the function."],"tags":["physics","acoustics","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}