TheAlgorithms/Python · error · ValueError

Input number is too large

Error message

Input number is too large

What it means

convert_number raises this ValueError when the (absolute) input exceeds the largest value expressible in the chosen numbering system: NumberingSystem.max_value(system). The limit is 10^18-1 style bounds computed from the largest scale word — short scale tops out after decillion, long scale one power-of-1000 higher, and the Indian system at 10^19-1 (shankh). Beyond that there are no scale words, so output would be wrong.

Source

Thrown at conversions/convert_number_to_words.py:184

    ...
    ValueError: Input number is too large
    >>> convert_number(10**21, "long")
    Traceback (most recent call last):
    ...
    ValueError: Input number is too large
    >>> convert_number(10**19, "indian")
    Traceback (most recent call last):
    ...
    ValueError: Input number is too large
    """
    word_groups = []

    if num < 0:
        word_groups.append("negative")
        num *= -1

    if num > NumberingSystem.max_value(system):
        raise ValueError("Input number is too large")

    for power, unit in NumberingSystem[system.upper()].value:
        digit_group, num = divmod(num, 10**power)
        if digit_group > 0:
            word_group = (
                convert_number(digit_group, system)
                if digit_group >= 100
                else convert_small_number(digit_group)
            )
            word_groups.append(f"{word_group} {unit}")
    if num > 0 or not word_groups:  # word_groups is only empty if input num was 0
        word_groups.append(convert_small_number(num))
    return " ".join(word_groups)


if __name__ == "__main__":
    import doctest

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check abs(num) <= NumberingSystem.max_value(system) before calling
  2. Switch to a system with a higher cap (long > short for equal word counts) if semantics allow
  3. Reject or clamp such inputs at your API boundary and report them to the user

Example fix

# before
convert_number(10**19, 'indian')
# ValueError: Input number is too large

# after
from conversions.convert_number_to_words import NumberingSystem
if abs(n) > NumberingSystem.max_value('indian'):
    raise OverflowError('value unrepresentable in Indian system words')
convert_number(n, 'indian')
Defensive patterns

Strategy: validation

Validate before calling

from conversions.convert_number_to_words import NumberingSystem
if abs(num) > NumberingSystem.max_value(system):
    raise OverflowError(f'{num} exceeds {system} system range')

Type guard

def is_representable(num: int, system: str) -> bool:
    from conversions.convert_number_to_words import NumberingSystem
    return abs(num) <= NumberingSystem.max_value(system)

Try / catch

try:
    convert_number(num, system)
except ValueError as e:
    if 'too large' in str(e):
        raise OverflowError('number out of range for word conversion') from e
    raise

Prevention

When it happens

Trigger: convert_number(10**19, 'indian') (exactly one past the max), convert_number(10**63, 'short'), convert_number(10**63, 'long') depending on table bounds; feeding 64-bit unsigned maxima or financial aggregates in obscure units.

Common situations: Formatting random/big test integers without bounds; assuming all systems share the same limit; unit tests that use round power-of-ten boundaries which are exactly one above max_value.

Related errors


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