{"record":{"id":"16e710abcdd396b2","repo":"TheAlgorithms/Python","slug":"additive-persistence-does-not-accept-negative-va","errorCode":null,"errorMessage":"additive_persistence() does not accept negative values","messagePattern":"additive_persistence\\(\\) does not accept negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/persistence.py","lineNumber":61,"sourceCode":"\n    https://en.wikipedia.org/wiki/Persistence_of_a_number\n\n    >>> additive_persistence(199)\n    3\n    >>> additive_persistence(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: additive_persistence() does not accept negative values\n    >>> additive_persistence(\"long number\")\n    Traceback (most recent call last):\n        ...\n    ValueError: additive_persistence() only accepts integral values\n    \"\"\"\n\n    if not isinstance(num, int):\n        raise ValueError(\"additive_persistence() only accepts integral values\")\n    if num < 0:\n        raise ValueError(\"additive_persistence() does not accept negative values\")\n\n    steps = 0\n    num_string = str(num)\n\n    while len(num_string) != 1:\n        numbers = [int(i) for i in num_string]\n\n        total = 0\n        for i in range(len(numbers)):\n            total += numbers[i]\n\n        num_string = str(total)\n\n        steps += 1\n    return steps\n\n\nif __name__ == \"__main__\":","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/persistence.py#L43-L79","documentation":"additive_persistence() in maths/persistence.py raises ValueError('additive_persistence() does not accept negative values') when an int argument is < 0. Digit-sum persistence is defined only for non-negative integers; the implementation does str(num) then int(i) over characters, and a leading '-' would crash int('-') with a less informative error, so negatives are rejected up front.","triggerScenarios":"Calling additive_persistence(-1) or any negative int such as additive_persistence(-123). Reached only for int inputs; negative non-ints raise the integral-values error because the type guard runs first.","commonSituations":"Passing signed computed values (differences, offsets) without abs(); user-supplied negative numbers from a CLI argument not range-checked.","solutions":["Use abs() when the sign carries no meaning: additive_persistence(abs(num)).","Range-validate (num >= 0) upstream and surface a domain-specific message.","Wrap in try/except ValueError if negatives are a legitimate runtime case to skip."],"exampleFix":"# before\nadditive_persistence(delta)  # ValueError when delta < 0\n\n# after\nadditive_persistence(abs(delta))","handlingStrategy":"validation","validationCode":"if num < 0:\n    num = abs(num)  # or raise your own domain error\nadditive_persistence(num)","typeGuard":null,"tryCatchPattern":"try:\n    additive_persistence(num)\nexcept ValueError as exc:\n    if 'negative' in str(exc):\n        num = abs(num)\n    else:\n        raise","preventionTips":["Check sign before digit-manipulation functions.","Document that persistence is defined on non-negative ints only.","Use abs() defensively on deltas and differences."],"tags":["python","value-error","input-validation","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}