This works, but it looks clunky. I'd like to be able to just write "T:MAX" to get the max bound of a generic type. but that's not implemented. There's a FIXME - Make these constants in the "num" crate where that ought to go. Is there a better way to get these bounds? Having to unwrap a compile time value is silly.
/// Rescale integer value into specified float range
fn scaletof32<T: num::Bounded + num::PrimInt>(val: T, minval: f32, maxval: f32) -> f32 {
let inmin = T::min_value().to_f32().unwrap(); // range of input
let inmax = T::max_value().to_f32().unwrap(); // cannot fail at runtime; these are constants
let fval = val.to_f32().unwrap(); // all ints can conver to floats
let range = maxval - minval; // numerical range to output
let scale = range / (inmax-inmin); // scale factor to convert to float
(fval - inmin) * scale + minval // scaled output
}