{"record":{"id":"eaa947079a5c5d67","repo":"TheAlgorithms/Python","slug":"precision-must-be-a-nonnegative-integer","errorCode":null,"errorMessage":"Precision must be a nonnegative integer","messagePattern":"Precision must be a nonnegative integer","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/bailey_borwein_plouffe.py","lineNumber":43,"sourceCode":"      ...\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\n) -> float:\n    # only care about first digit of fractional part; don't need decimal","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/bailey_borwein_plouffe.py#L25-L61","documentation":"The second parameter of bailey_borwein_plouffe() controls how many extra hex digits are computed to stabilize the requested digit. It must be a nonnegative int; anything negative or non-int raises ValueError('Precision must be a nonnegative integer').","triggerScenarios":"bailey_borwein_plouffe(2, -10); bailey_borwein_plouffe(2, 1.6); passing bool is fine (bool is int), but floats like 8.0 are rejected by isinstance(precision, int).","commonSituations":"Defaulting precision from a float config value; computing precision as a division result (e.g. n / 2); swapping argument order so a position lands in the precision slot.","solutions":["Pass a plain nonnegative int, e.g. bailey_borwein_plouffe(2, 10).","Wrap computed precisions with int(...) and clamp to >= 0.","Check argument order if you pass position and precision positionally."],"exampleFix":"# before\nbailey_borwein_plouffe(2, 1.6)\n\n# after\nbailey_borwein_plouffe(2, int(1.6))  # -> precision 1","handlingStrategy":"validation","validationCode":"precision = int(max(0, precision)) if precision == int(precision) else None\nif precision is None:\n    raise ValueError(\"precision must be a nonnegative integer\")","typeGuard":"def is_valid_precision(p: object) -> bool:\n    return isinstance(p, int) and not isinstance(p, bool) and p >= 0","tryCatchPattern":null,"preventionTips":["Keep precision as int — avoid float config defaults","Pass positional args in order (digit_position, precision) to avoid swaps"],"tags":["math","bbp","pi","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}