infinitable

Crates.ioinfinitable
lib.rsinfinitable
version1.6.0
sourcesrc
created_at2017-10-09 07:23:45.431035
updated_at2024-05-10 23:37:41.130034
descriptionInfinity for types without infinite values
homepage
repositoryhttps://github.com/ObjectBoxPC/rust-infinitable
max_upload_size
id34955
size40,904
Philip Chung (ObjectBoxPC)

documentation

README

Infinitable (Rust)

Infinitable is a simple library for introducing the notion of "infinity" and "negative infinity" to numeric types, such as integers, that do not have infinite values.

A representation of infinity is useful for graph algorithms such as Dijkstra's algorithm, as well as for representing a graph with an adjacency matrix.

Usage

Simply install the infinitable crate, available on Crates.io.

use infinitable::Infinitable;

fn main() {
	let finite = Infinitable::Finite(5);
	let infinity = Infinitable::Infinity;
	let negative_infinity = Infinitable::NegativeInfinity;

	assert!(finite < infinity);
	assert!(finite > negative_infinity);
}

Tests

Tests are in the tests directory. Simply run cargo test.

Note About Floating-Point Numbers

The infinite values provided by Infinitable are different from the existing infinite values in floating-point numeric types. Therefore, simply using Finite or Infinitable::from to convert from a floating-point value to an Infinitable may lead to unintuitive results:

use infinitable::Infinitable;

fn main() {
	let infinitable_infinity = Infinitable::Infinity;
	let fp_infinity = Infinitable::Finite(f64::INFINITY);

	assert!(infinitable_infinity > fp_infinity);
}

The library provides the from_f32 and from_f64 functions to convert from floating-point values while taking into account infinite values and NaN:

use infinitable::Infinitable;

fn main() {
	let infinitable_infinity = Infinitable::Infinity;
	let fp_infinity = infinitable::from_f64(f64::INFINITY).unwrap();

	assert!(infinitable_infinity == fp_infinity);
}

License

Infinitable is available under the 2-clause BSD license. Refer to LICENSE.txt for details.

Commit count: 75

cargo fmt