Skip to main content

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, and ModuloBoundedInt automatically enforce their strategy after arithmetic (addition, subtraction, multiplication, division, and floor division).
  • Bounded floats: ClampedFloat, CyclicFloat, and BouncedFloat mirror the integer behavior for floating-point values.
  • Unit interval helpers: UnitFloat corrects values into [0, 1] on construction; EnforcedUnitFloat reapplies the bounds after arithmetic.
  • Modular arithmetic: ModuloInt exposes additive and multiplicative inverses along with safe modular iteration via modulo_range.
  • Utility functions: Reusable helpers like clamp, clamp01, bounce, cyclic_wrap, and cyclic_wrap_float are 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.

Metadata

Release files for boundednumbers 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for boundednumbers 0.1.3
File Size Uploaded Provenance
boundednumbers-0.1.3.tar.gz 12.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for boundednumbers 0.1.3
File Interpreter ABI Platform
boundednumbers-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 24.1 kB

Release files / boundednumbers-0.1.3.tar.gz

Download URL boundednumbers-0.1.3.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f9909c1ba14c5b320b2889d49db88d55115ee8268874b26ceee74d45ca22603a
BLAKE2b-256 checksum
How to use checksums
c0b79f42d15bf4c9094cbdd757105ff66eb25301fd973c2ae0985e524dccbe78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / boundednumbers-0.1.3-py3-none-any.whl

Download URL boundednumbers-0.1.3-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9660f515e47957bfcc796d390cd79fcb2bdbdd69030cdde6b43b668352bd493b
BLAKE2b-256 checksum
How to use checksums
de2a71fb5b2dd5e2b33c4690b10a76d4a2fed5fd6a24560ca5bce4aec0c74fa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for boundednumbers 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page