{"record":{"id":"398f877bca38604d","repo":"TheAlgorithms/Python","slug":"digit-position-must-be-a-positive-integer","errorCode":null,"errorMessage":"Digit position must be a positive integer","messagePattern":"Digit position must be a positive integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/bailey_borwein_plouffe.py","lineNumber":41,"sourceCode":"    >>> bailey_borwein_plouffe(0)\n    Traceback (most recent call last):\n      ...\n    ValueError: Digit position must be a positive integer\n    >>> bailey_borwein_plouffe(1.7)\n    Traceback (most recent call last):\n      ...\n    ValueError: Digit position must be a positive integer\n    >>> bailey_borwein_plouffe(2, -10)\n    Traceback (most recent call last):\n      ...\n    ValueError: Precision must be a nonnegative integer\n    >>> bailey_borwein_plouffe(2, 1.6)\n    Traceback (most recent call last):\n      ...\n    ValueError: Precision must be a nonnegative integer\n    \"\"\"\n    if (not isinstance(digit_position, int)) or (digit_position <= 0):\n        raise ValueError(\"Digit position must be a positive integer\")\n    elif (not isinstance(precision, int)) or (precision < 0):\n        raise ValueError(\"Precision must be a nonnegative integer\")\n\n    # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly\n    # accurate\n    sum_result = (\n        4 * _subsum(digit_position, 1, precision)\n        - 2 * _subsum(digit_position, 4, precision)\n        - _subsum(digit_position, 5, precision)\n        - _subsum(digit_position, 6, precision)\n    )\n\n    # return the first hex digit of the fractional part of the result\n    return hex(int((sum_result % 1) * 16))[2:]\n\n\ndef _subsum(\n    digit_pos_to_extract: int, denominator_addend: int, precision: int","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/bailey_borwein_plouffe.py#L23-L59","documentation":"bailey_borwein_plouffe(digit_position, precision) extracts hex digits of pi. It raises ValueError('Digit position must be a positive integer') when digit_position is not an int or is <= 0, because position 0 is not a valid digit index in the BBP formula as implemented.","triggerScenarios":"bailey_borwein_plouffe(0, 10); bailey_borwein_plouffe(-3, 10); bailey_borwein_plouffe(2.5, 10) (non-int types are also rejected by the isinstance check).","commonSituations":"Off-by-one from treating the first digit as index 0 instead of 1; passing a float position computed as n/16 or similar; config/CLI input parsed as string or float.","solutions":["Use 1-based positions: the first hex digit after the point is position 1.","Coerce to int explicitly, e.g. int(digit_position), when the value is a whole number in float form.","Validate CLI/user input is a positive integer before calling."],"exampleFix":"# before\nhex_digit = bailey_borwein_plouffe(pos, 12)  # pos = 0\n\n# after\npos = max(1, int(pos))\nhex_digit = bailey_borwein_plouffe(pos, 12)","handlingStrategy":"type-guard","validationCode":"if not isinstance(digit_position, int) or digit_position <= 0:\n    raise ValueError(\"digit_position must be a positive int (1-based)\")","typeGuard":"def is_valid_position(p: object) -> bool:\n    return isinstance(p, int) and not isinstance(p, bool) and p >= 1","tryCatchPattern":null,"preventionTips":["BBP positions are 1-based — the first hex digit after the point is position 1","Coerce float positions with int() only after confirming they are whole"],"tags":["math","bbp","pi","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}