Skip to main content

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, 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

boundednumbers-0.1.3.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boundednumbers-0.1.3-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file boundednumbers-0.1.3.tar.gz.

File metadata

  • Download URL: boundednumbers-0.1.3.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for boundednumbers-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f9909c1ba14c5b320b2889d49db88d55115ee8268874b26ceee74d45ca22603a
MD5 ba6c60d1f0c339f87d1cabdf0d087293
BLAKE2b-256 c0b79f42d15bf4c9094cbdd757105ff66eb25301fd973c2ae0985e524dccbe78

See more details on using hashes here.

File details

Details for the file boundednumbers-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: boundednumbers-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for boundednumbers-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9660f515e47957bfcc796d390cd79fcb2bdbdd69030cdde6b43b668352bd493b
MD5 b07b612d673cedf29da11bef65d3b49b
BLAKE2b-256 de2a71fb5b2dd5e2b33c4690b10a76d4a2fed5fd6a24560ca5bce4aec0c74fa7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page