Numeric helpers that keep values within explicit bounds
Project description
boundednumbers
Boundednumbers provides lightweight numeric types that automatically respect the limits you set. Use them to keep values in range across math operations without littering your code with manual clamping or wraparound logic.
Installation
pip install boundednumbers
Quick start
from numbers import ClampedInt, CyclicInt, BouncedInt, ModuloInt, UnitFloat, modulo_range
health = ClampedInt(120, 0, 100) # -> 100, and stays within [0, 100]
angle = CyclicInt(-15, 0, 360) # -> 345, wraps on every operation
paddle = BouncedInt(130, 0, 100) # -> 70, reflects back into range
counter = ModuloInt(7, 5) # -> 2, supports modular arithmetic
opacity = UnitFloat(1.25) # -> 1.0, always between 0 and 1
# Iterate around a circle without repeating the stop value
points = list(modulo_range(start=350, stop=170, step=40, modulus=360))
Features
- Bounded integers:
ClampedInt,CyclicInt,BouncedInt, andModuloBoundedIntautomatically enforce their strategy after arithmetic (addition, subtraction, multiplication, division, and floor division). - Bounded floats:
ClampedFloat,CyclicFloat, andBouncedFloatmirror the integer behavior for floating-point values. - Unit interval helpers:
UnitFloatcorrects values into[0, 1]on construction;EnforcedUnitFloatreapplies the bounds after arithmetic. - Modular arithmetic:
ModuloIntexposes additive and multiplicative inverses along with safe modular iteration viamodulo_range. - Utility functions: Reusable helpers like
clamp,clamp01,bounce,cyclic_wrap, andcyclic_wrap_floatare available when you prefer to keep native numeric types.
API overview
Bounded factories
Use the factory helpers when you need custom class names or number types:
from numbers import BoundType, bounce, make_default_bounded_int, make_default_bounded_float, make_bounded_int
# Create a custom bounded integer that bounces inside [-5, 5]
BouncyFive = make_bounded_int(lambda v, mn, mx: bounce(v, mn, mx), class_name="BouncyFive")
value = BouncyFive(17, -5, 5) # -> -3
# Or rely on the built-in enum to pick a strategy
WrappedFloat = make_default_bounded_float(BoundType.CYCLIC)
angle = WrappedFloat(725.0, 0.0, 360.0) # -> 5.0
Modular utilities
ModuloInt and modulo_range simplify common modular workflows:
from numbers import Direction, ModuloRangeMode, modulo_range, ModuloInt
# Compute a multiplicative inverse
inverse = ModuloInt(7, 26).inverse() # -> ModuloInt(15 mod 26)
# Visit points around a circle without repeating stop (one full modular cycle)
for value in modulo_range(start=0, stop=0, step=3, modulus=10, direction=Direction.DECREASING, max_range_amount=ModuloRangeMode.DETECT):
...
Unit floats
UnitFloat corrects values into the unit interval on construction, while EnforcedUnitFloat applies
bounds after every operation:
from numbers import EnforcedUnitFloat
u = EnforcedUnitFloat(1.4)
print(u - 3.0) # -> 0.0
Further reading
See numbers/README.md for a deeper, module-by-module breakdown and usage tips.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file boundednumbers-0.1.1.tar.gz.
File metadata
- Download URL: boundednumbers-0.1.1.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80f0ab47180a7d2bf5c6e9e8e4fff822266cc3140e52a76bc790531f3770f335
|
|
| MD5 |
b1b19f15dc07ed90ea64a9e001600f7d
|
|
| BLAKE2b-256 |
b5a51bbe1819ab37c831d9c8367da505ad380f4df3f60ba0c512098a1abf0791
|
File details
Details for the file boundednumbers-0.1.1-py3-none-any.whl.
File metadata
- Download URL: boundednumbers-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a95529c1cb6bd11121818cf29c039b15e516eaf56b49072aa87c638e8eba2e5
|
|
| MD5 |
663958b52f2412d664c882303149b7c7
|
|
| BLAKE2b-256 |
79a963d602b4808cc7843bd8384b27309da2160d333df78ed1d9005cb758542f
|