Crate unit_interval

Source
Expand description

A small crate for working with numbers in the unit interval.

This crate currently only provides the UnitInterval type, which represents a number constrained to the closed interval between 0 and 1, inclusive. It offers operations, bounds checking, and utilities for working with values in this range.

§Quick Start

To get started, create a UnitInterval using one of the many constructor methods:

use unit_interval::UnitInterval;

// Create a UnitInterval, panics if out of bounds
let a = UnitInterval::new(0.5);

// Create a UnitInterval, returns a Result
let b = UnitInterval::new_checked(0.75).unwrap();

// Create a UnitInterval, clamping the value to [0, 1]
let c = UnitInterval::new_clamped(1.5);
assert_eq!(c.into_inner(), 1.0);

Perform operations on UnitInterval values:

use unit_interval::UnitInterval;

let a = UnitInterval::new(0.3);
let b = UnitInterval::new(0.5);

// Multiplication always stays within [0, 1]
let product = a * b;
assert_eq!(product.into_inner(), 0.15);

// Other operations may need checking or clamping
let sum = a.checked_add(b).unwrap();
let difference = a.clamped_sub(b);

Structs§

BoundError
An error type representing a value that falls outside the closed unit interval [0, 1]
UnitInterval
A number in the closed unit interval [0, 1]

Enums§

BoundErrorKind
Describes how a value falls outside the closed unit interval [0, 1]