TheAlgorithms/Python · error · ValueError
Undefined for non-natural numbers
Error message
Undefined for non-natural numbers
What it means
Raised by pi() in maths/chudnovsky_algorithm.py when precision is an int but less than 1. The Chudnovsky series is summed for ceil(precision/14) iterations to produce exactly `precision` significant digits; zero or negative precision has no mathematical meaning, so it is rejected with ValueError (undefined for non-natural numbers).
Source
Thrown at maths/chudnovsky_algorithm.py:42
>>> pi(10)
'3.14159265'
>>> pi(100)
'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706'
>>> pi('hello')
Traceback (most recent call last):
...
TypeError: Undefined for non-integers
>>> pi(-1)
Traceback (most recent call last):
...
ValueError: Undefined for non-natural numbers
"""
if not isinstance(precision, int):
raise TypeError("Undefined for non-integers")
elif precision < 1:
raise ValueError("Undefined for non-natural numbers")
getcontext().prec = precision
num_iterations = ceil(precision / 14)
constant_term = 426880 * Decimal(10005).sqrt()
exponential_term = 1
linear_term = 13591409
partial_sum = Decimal(linear_term)
for k in range(1, num_iterations):
multinomial_term = factorial(6 * k) // (factorial(3 * k) * factorial(k) ** 3)
linear_term += 545140134
exponential_term *= -262537412640768000
partial_sum += Decimal(multinomial_term * linear_term) / exponential_term
return str(constant_term / partial_sum)[:-1]
if __name__ == "__main__":
n = 50
print(f"The first {n} digits of pi is: {pi(n)}")View on GitHub (pinned to f5988cc097)
Solutions
- Clamp or validate precision >= 1 before calling: precision = max(precision, 1) if a fallback is acceptable, else raise your own error.
- Fix the upstream computation that produced a non-positive precision.
- Treat 0/negative precision as invalid input at your API boundary with a clear message.
Example fix
# before
pi(-1) # ValueError: Undefined for non-natural numbers
# after
precision = 50
if precision < 1:
raise ValueError('precision must be >= 1')
result = pi(precision) Defensive patterns
Strategy: validation
Validate before calling
if precision < 1:
raise ValueError(f'precision must be >= 1, got {precision}')
digits = pi(precision) Try / catch
try:
digits = pi(precision)
except ValueError as e:
if 'non-natural' in str(e):
digits = pi(1) # deliberate fallback to minimum precision
else:
raise Prevention
- Clamp user-supplied precision: precision = max(1, int(precision)).
- Watch formulas like len(s) - 20 that go negative for short inputs.
When it happens
Trigger: Calling pi(0) or pi(-1) — any integer precision < 1. Note pi(-1.5) hits the earlier TypeError instead, since the isinstance check runs first.
Common situations: Precision derived from a formula that can reach 0 (e.g. len(str) - 20 for short inputs); inverted conditionals that pass -1 as an error sentinel; user-facing parameters left at 0 by default.
Related errors
- Undefined for non-integers
- Both points must have the same dimension.
- Monogons and Digons are not polygons in the Euclidean space
- All values must be greater than 0
- Please enter positive integers for n and k where n >= k
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/bd1a3b25a79ed960.
Report an issue: GitHub.