#range #mapping #another #int #map #target #bounds

map_range_int

map a value from one range to another

2 stable releases

1.1.0 Nov 1, 2024
1.0.0 Oct 31, 2024

#815 in Algorithms

Download history 4/week @ 2024-11-13 9/week @ 2024-11-20 3/week @ 2024-11-27 14/week @ 2024-12-04 20/week @ 2024-12-11 3/week @ 2024-12-18 7/week @ 2024-12-25 112/week @ 2025-01-01 35/week @ 2025-01-08 52/week @ 2025-01-15 75/week @ 2025-01-22 72/week @ 2025-01-29 73/week @ 2025-02-05 44/week @ 2025-02-12 28/week @ 2025-02-19 21/week @ 2025-02-26

176 downloads per month
Used in 6 crates (2 directly)

MIT/Apache

16KB
275 lines

Changes

MapRange

MapRange maps a value from one range to a second range.

For integer-integer mapping it doesn't escalate to floats, but uses the next bigger integer type.

It works better than integer divide + scale up, as it truly fills the full target range. Especially an end-point in the source range maps to an end-point in the target range.

This is implemented for all combinations of source/target type except i128 and u128.

   use map_range_int::MapRange;

//
// in range
//
let r2 = 17u8.map_range((10, 20), (100, 200)).expect("in_range");
assert_eq!(r2, 170u8);

let r2 = 10u8.map_range((10, 20), (100, 200)).expect("in_range");
assert_eq!(r2, 100u8);

let r2 = 20u8.map_range((10, 20), (100, 200)).expect("in_range");
assert_eq!(r2, 200u8);

//
// out of range
//
let r2: Option<u8> = 5u8.map_range((10, 20), (100, 200));
assert_eq!(r2, None);

//
// reversed bounds
//
let r2: Option<u8> = 17u8.map_range(/**/ (20, 10) /**/, (100, 200));
assert_eq!(r2, None);

let r2: Option<u8> = 17u8.map_range((10, 20), /**/ (200, 100) /**/);
assert_eq!(r2, None);

//
// boundaries
//
let r2 = 17u8
.map_range((10, 20), /**/ (100, 100) /**/)
.expect("in_range");
assert_eq!(r2, 100u8);

let r2 = 10u8.map_range((10, 10), (100, 200)).expect("in_range");
assert_eq!(r2, 100u8);

//
// signed
//
let r2 = ( - 10i8).map_range(( - 100, 100), (0, 10)).expect("in_range");
assert_eq!(r2, 4u8);

let r2 = 90u8.map_range((0, 200), ( - 100, 100)).expect("in_range");
assert_eq!(r2, -10i8);

//
// float
//
let r2 = 0.31f64.map_range((0., 1.), (0, 255)).expect("in_range");
assert_eq!(r2, 79u8);

let r2 = 81u8.map_range((0, 255), (0., 1.)).expect("in_range");
assert_eq!(r2, 0.3176470588235294f64);

No runtime deps