{"record":{"id":"3aab933be9aeb6ac","repo":"TheAlgorithms/Python","slug":"the-length-should-be-non-negative","errorCode":null,"errorMessage":"The length should be non-negative","messagePattern":"The length should be non-negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/period_of_pendulum.py","lineNumber":46,"sourceCode":"\n\ndef period_of_pendulum(length: float) -> float:\n    \"\"\"\n    >>> period_of_pendulum(1.23)\n    2.2252155506257845\n    >>> period_of_pendulum(2.37)\n    3.0888278441908574\n    >>> period_of_pendulum(5.63)\n    4.76073193364765\n    >>> period_of_pendulum(-12)\n    Traceback (most recent call last):\n        ...\n    ValueError: The length should be non-negative\n    >>> period_of_pendulum(0)\n    0.0\n    \"\"\"\n    if length < 0:\n        raise ValueError(\"The length should be non-negative\")\n    return 2 * pi * (length / g) ** 0.5\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":28,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/period_of_pendulum.py#L28-L54","documentation":"Raised by period_of_pendulum(length) in physics/period_of_pendulum.py when length < 0. The small-angle pendulum period T = 2*pi*sqrt(length/g) requires a non-negative pendulum length; length 0 is allowed and returns 0.0. The check runs before the square root, preventing a complex result from a negative radicand.","triggerScenarios":"period_of_pendulum(-12); any call where the length argument is a negative float, e.g. length = anchor_y - bob_y computed with the operands swapped.","commonSituations":"Subtraction-order bugs when deriving length from coordinates; feeding signed positions instead of magnitudes; fixtures that use negative numbers to mean 'invalid' without sanitizing them.","solutions":["Fix the length computation to produce a magnitude: length = abs(bob_y - anchor_y)","Validate inputs at the boundary: reject or clamp length < 0 before calling","In simulations, guard decayed/destroyed pendulum states instead of passing negative lengths"],"exampleFix":"# before\nperiod = period_of_pendulum(anchor_y - bob_y)  # negative if bob is above anchor\n\n# after\nperiod = period_of_pendulum(abs(bob_y - anchor_y))","handlingStrategy":"validation","validationCode":"if length < 0:\n    raise ValueError(f\"pendulum length must be >= 0, got {length}\")\nperiod = period_of_pendulum(length)","typeGuard":null,"tryCatchPattern":"try:\n    period = period_of_pendulum(length)\nexcept ValueError:\n    length = abs(length)  # only if the sign was a coordinate artifact\n    period = period_of_pendulum(length)","preventionTips":["Derive length as abs(delta) between anchor and bob positions","Length 0 is valid and returns 0.0; only negatives are rejected"],"tags":["physics","pendulum","validation","negative-value","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}