Skip to content

v0.5.0 — Exact scaled units and custom unit definitions

Choose a tag to compare

@DaitTHU DaitTHU released this 17 Oct 04:12
· 25 commits to main since this release

unitx 0.5.0 introduces exact scaled units and lets you define new units directly from existing units or quantities.

  • Custom unit definitions: call .define() on Unit, ScaledUnit, or Quantity, with a required name, optional aliases, and configurable SI prefix support.
  • Exact scaling: ScaledUnit preserves rational and symbolic scales through arithmetic, powers, conversion, copying, and pickling.
  • Conflict detection: validate symbols, names, aliases, and generated prefixed forms before updating the registry.
  • Improved display and typing: show explicit scales in quantity output, reconstruct escaped symbols correctly, and infer ScaledUnit for mixed unit arithmetic.
  • Parser expressions: use Unit.expression() to obtain the formal unit product in parser syntax.
from unitx.factor import PI
from unitx.quantity import Quantity
from unitx.unit import Unit

# Define a derived unit.
newton = Unit('kg*m/s2').define('N', name='newton')

# Define a unit from a quantity.
minute = (60 * Unit('s')).define(
    'min', name='minute', prefixable=False
)

# Define a unit with an exact symbolic scale.
degree = ((PI / 180) * Unit()).define(
    'deg', name='degree', prefixable=False
)

assert Quantity(2, minute).to('s').value == 120
assert Quantity(1, 'kN').to(newton).value == 1000
assert degree.factor == PI / 180

Definitions are local to the current process and leave their source objects unchanged. Prefer Fraction or Decimal for exact numeric definitions; floats follow Factor’s rational approximation rules and may warn or round to zero and fail. Restoring custom units in another process requires their definitions to be registered there.