Crates.io | magnitude |
lib.rs | magnitude |
version | 0.3.2 |
source | src |
created_at | 2020-10-12 02:35:48.330482 |
updated_at | 2020-11-25 16:47:35.498546 |
description | Magnitude - To infinity and beyond! |
homepage | https://github.com/maminrayej/magnitude |
repository | https://github.com/maminrayej/magnitude |
max_upload_size | |
id | 298499 |
size | 62,055 |
This crate is useful when you need to work with algorithms like
Dijkstra's Shortest Path or
Floyd–Warshall algorithm
that require infinite values in order to be written elegantly.
One simple example can be finding the max value in a vector:
use magnitude::Magnitude;
fn find_max(vec: &Vec<Magnitude<i32>>) -> Magnitude<i32> {
let mut max = Magnitude::NegInfinite;
for val in vec {
if *val > max {
max = *val;
}
}
max
}
let vec: Vec<Magnitude<i32>> = vec![2.into(), 3.into(), 6.into(), (-10).into()];
assert_eq!(find_max(&vec), 6.into());
You can do all valid comparison(==, !=, >, <, >=, <=) and arithmetic(+,-, *, /, +=, -=, *=, /=) operations on magnitudes.
Invalid operations are listed below which means any other operation is valid.
PosInfinite
NegInfinite
PosInfinite
+ NegInfinite
PosInfinite
- PosInfinite
NegInfinite
- NegInfinite
PosInfinite
NegInfinite
PosInfinite
NegInfinite
PosInfinite
/ zeroNegInfinite
/ zeroPosInfinite
/ PosInfinite
PosInfinite
/ NegInfinite
NegInfinite
/ PosInfinite
NegInfinite
/ NegInfinite
f64
and f32
infinitiesMagnitude as of 0.2.0 treat f64::INFINITY
, f64::NEG_INFINITY
, f32::INFINITY
, f32::NEG_INFINITY
as infinites:
use magnitude::Magnitude;
let pos_inf: Magnitude<f64> = f64::INFINITY.into();
let neg_inf: Magnitude<f64> = f64::NEG_INFINITY.into();
assert!(pos_inf.is_pos_infinite());
assert!(neg_inf.is_neg_infinite());
let pos_inf: Magnitude<f32> = f32::INFINITY.into();
let neg_inf: Magnitude<f32> = f32::NEG_INFINITY.into();
assert!(pos_inf.is_pos_infinite());
assert!(neg_inf.is_neg_infinite());
Copy
instead of Clone
.from_vec
to build a vector of Magnitude
from a vector of values:
use magnitude::Magnitude;
let magnitude_vec = Magnitude::from_vec(&vec![1,2,3]);
assert_eq!(magnitude_vec[0], 1.into());
assert_eq!(magnitude_vec[1], 2.into());
assert_eq!(magnitude_vec[2], 3.into());
unwrap
for easier access to value inside Finite
:
use magnitude::Magnitude;
let one: Magnitude<i32> = 1.into();
assert_eq!(one.unwrap(), 1);
f64::INFINITY
, f64::NEG_INFINITY
, f32::INFINITY
, f32::NEG_INFINITY
properly