TheAlgorithms/Python · error · ValueError

Digit position must be a positive integer

Error message

Digit position must be a positive integer

What it means

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.

Source

Thrown at maths/bailey_borwein_plouffe.py:41

    >>> bailey_borwein_plouffe(0)
    Traceback (most recent call last):
      ...
    ValueError: Digit position must be a positive integer
    >>> bailey_borwein_plouffe(1.7)
    Traceback (most recent call last):
      ...
    ValueError: Digit position must be a positive integer
    >>> bailey_borwein_plouffe(2, -10)
    Traceback (most recent call last):
      ...
    ValueError: Precision must be a nonnegative integer
    >>> bailey_borwein_plouffe(2, 1.6)
    Traceback (most recent call last):
      ...
    ValueError: Precision must be a nonnegative integer
    """
    if (not isinstance(digit_position, int)) or (digit_position <= 0):
        raise ValueError("Digit position must be a positive integer")
    elif (not isinstance(precision, int)) or (precision < 0):
        raise ValueError("Precision must be a nonnegative integer")

    # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
    # accurate
    sum_result = (
        4 * _subsum(digit_position, 1, precision)
        - 2 * _subsum(digit_position, 4, precision)
        - _subsum(digit_position, 5, precision)
        - _subsum(digit_position, 6, precision)
    )

    # return the first hex digit of the fractional part of the result
    return hex(int((sum_result % 1) * 16))[2:]


def _subsum(
    digit_pos_to_extract: int, denominator_addend: int, precision: int

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use 1-based positions: the first hex digit after the point is position 1.
  2. Coerce to int explicitly, e.g. int(digit_position), when the value is a whole number in float form.
  3. Validate CLI/user input is a positive integer before calling.

Example fix

# before
hex_digit = bailey_borwein_plouffe(pos, 12)  # pos = 0

# after
pos = max(1, int(pos))
hex_digit = bailey_borwein_plouffe(pos, 12)
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(digit_position, int) or digit_position <= 0:
    raise ValueError("digit_position must be a positive int (1-based)")

Type guard

def is_valid_position(p: object) -> bool:
    return isinstance(p, int) and not isinstance(p, bool) and p >= 1

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/398f877bca38604d. Report an issue: GitHub.