{"record":{"id":"5bf275dc2efb41ae","repo":"TheAlgorithms/Python","slug":"mass-density-area-and-the-drag-coefficient-all-n","errorCode":null,"errorMessage":"mass, density, area and the drag coefficient all need to be positive","messagePattern":"mass, density, area and the drag coefficient all need to be positive","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/terminal_velocity.py","lineNumber":51,"sourceCode":"    >>> terminal_velocity(2, 100, 0.45, 0.23)\n    1.9467947148674276\n    >>> terminal_velocity(5, 50, 0.2, 0.5)\n    4.428690551393267\n    >>> terminal_velocity(-5, 50, -0.2, -2)\n    Traceback (most recent call last):\n        ...\n    ValueError: mass, density, area and the drag coefficient all need to be positive\n    >>> terminal_velocity(3, -20, -1, 2)\n    Traceback (most recent call last):\n        ...\n    ValueError: mass, density, area and the drag coefficient all need to be positive\n    >>> terminal_velocity(-2, -1, -0.44, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: mass, density, area and the drag coefficient all need to be positive\n    \"\"\"\n    if mass <= 0 or density <= 0 or area <= 0 or drag_coefficient <= 0:\n        raise ValueError(\n            \"mass, density, area and the drag coefficient all need to be positive\"\n        )\n    return ((2 * mass * g) / (density * area * drag_coefficient)) ** 0.5\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":33,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/terminal_velocity.py#L33-L61","documentation":"Raised by physics/terminal_velocity.py:terminal_velocity when any of mass, density, area, or drag_coefficient is <= 0. The formula sqrt(2*m*g/(rho*A*Cd)) requires all four to be strictly positive; the check is a single combined guard, so the message does not tell you which parameter failed. Inputs are positional, which makes silent argument swaps the most common cause.","triggerScenarios":"Calling terminal_velocity with any non-positive argument, e.g. terminal_velocity(3, -20, 0.44, 1) (negative density), terminal_velocity(0, 1.2, 0.44, 1) (zero mass), or several at once as in the doctest terminal_velocity(-2, -1, -0.44, -1).","commonSituations":"Positional-argument swaps - the signature is (mass, density, area, drag_coefficient) and passing 1.2 (air density) as mass or 70 (mass) as density are both classic mistakes; zero drag coefficient from an unconfigured aero model; density defaults of 0 when the medium is unspecified; negative values from upstream subtraction of sensor offsets.","solutions":["Check each of the four values individually before the call to find which one is non-positive (the error message does not identify it).","Call with keyword arguments - terminal_velocity(mass=70, density=1.2, area=0.7, drag_coefficient=1.0) - to eliminate positional swaps.","Fix defaults: use realistic fallbacks like density=1.225 (sea-level air) instead of 0 or -1 sentinels.","If invalid parameters occur in batch simulations, catch ValueError and skip/quarantine that record."],"exampleFix":"# before (1.2 meant as density lands in mass)\nterminal_velocity(1.2, 70, 0.44, 1.0)\n\n# after\nterminal_velocity(mass=70, density=1.2, area=0.44, drag_coefficient=1.0)","handlingStrategy":"validation","validationCode":"params = {'mass': m, 'density': rho, 'area': A, 'drag_coefficient': cd}\nbad = [k for k, v in params.items() if v <= 0]\nif bad:\n    raise ValueError(f'non-positive parameters: {bad}')\nvt = terminal_velocity(m, rho, A, cd)","typeGuard":"def is_valid_terminal_velocity_args(m, rho, a, cd) -> bool:\n    return all(isinstance(x, (int, float)) and x > 0 for x in (m, rho, a, cd))","tryCatchPattern":"try:\n    terminal_velocity(m, rho, A, cd)\nexcept ValueError:\n    # message does not say which param failed; re-check all four here\n    ...","preventionTips":["Call with keywords: terminal_velocity(mass=..., density=..., area=..., drag_coefficient=...).","Default density to 1.225 (air), never 0 or -1.","Check all four values before the call - the combined message does not identify the culprit."],"tags":["physics","drag","validation","valueerror","positional-args"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}