{"record":{"id":"32620fd3e24624d4","repo":"TheAlgorithms/Python","slug":"impossible-gravity","errorCode":null,"errorMessage":"Impossible gravity","messagePattern":"Impossible gravity","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/archimedes_principle_of_buoyant_force.py","lineNumber":54,"sourceCode":"    ValueError: Impossible object volume\n    >>> archimedes_principle(fluid_density=0, volume=0.7)\n    Traceback (most recent call last):\n        ...\n    ValueError: Impossible fluid density\n    >>> archimedes_principle(fluid_density=997, volume=0.7, gravity=0)\n    0.0\n    >>> archimedes_principle(fluid_density=997, volume=0.7, gravity=-9.8)\n    Traceback (most recent call last):\n        ...\n    ValueError: Impossible gravity\n    \"\"\"\n\n    if fluid_density <= 0:\n        raise ValueError(\"Impossible fluid density\")\n    if volume <= 0:\n        raise ValueError(\"Impossible object volume\")\n    if gravity < 0:\n        raise ValueError(\"Impossible gravity\")\n\n    return fluid_density * gravity * volume\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":36,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/archimedes_principle_of_buoyant_force.py#L36-L63","documentation":"Raised by archimedes_principle when gravity is negative. Unlike density and volume, gravity of exactly 0 is allowed (free-fall / orbit, returns 0.0) — only strictly negative values raise, since negative gravitational acceleration is unphysical in this model. It is the last of the three ordered checks.","triggerScenarios":"Calling archimedes_principle(gravity=-9.8, ...), e.g. by sign-flipping an acceleration value or using a downward-positive coordinate convention.","commonSituations":"Physics engines or coordinate systems where the gravity vector is expressed as negative-y, and the signed component is passed instead of its magnitude.","solutions":["Pass the magnitude of gravitational acceleration (e.g. 9.8 or 9.81 m/s^2), not a signed vector component","Use gravity=0 deliberately for free-fall/orbital scenarios — it is supported","If your coordinate convention encodes gravity as -9.8, convert with abs()"],"exampleFix":"# before\narchimedes_principle(fluid_density=997, volume=0.7, gravity=g_vec.y)  # -9.8 -> ValueError\n\n# after\narchimedes_principle(fluid_density=997, volume=0.7, gravity=abs(g_vec.y))","handlingStrategy":"validation","validationCode":"def valid_gravity(gravity) -> bool:\n    return gravity >= 0  # 0 is allowed (free fall); only negative raises","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass gravity magnitudes (9.81), not signed vector components from physics engines","Convert axis-aligned gravity like g_vec.y = -9.8 with abs()"],"tags":["physics","buoyancy","argument-validation","coordinate-systems"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}