I've got a usize
that does hit very large values. I also apply a delta to it that I receive in the form of an isize
. What's the best way to apply the delta without losing any precision?
fn main() {
let mut big_indexer: usize = 4295032832; // 2^32 + 2^16
let delta: isize = -65792; // 2^16 + 2^8
let big_indexer = (big_indexer as isize) + delta // Can't do this b/c overflow
let big_indexer = big_indexer + (delta as usize) // Can't do this b/c lose negative number
// This is ugly
if delta < 0 {
let big_indexer -= delta.abs() as usize;
} else {
let big_indexer += delta.abs() as usize;
}
}
delta.abs()
may panic whendelta
isisize::MIN
. – Cruces