Expand description
Primitive integer division with rounding kind and cumulative error.
As we all know, integer division can lead to precision loss and errors. What is less obvious is that in some scenarios, the dividend is split into several parts, turning a single division into multiple divisions, which can sometimes result in even greater errors.
For example, 60 / 3 = 20, and this division itself is error-free. However, if we split the dividend 60 into three parts of 20 and perform the division three times: 20 / 3 = 6.67, rounding it to 7 (using the Rounded method as an example; other methods are similar). Adding up the three 7 gives us 21, which is 1 more than the original 20.
For such consecutive divisions, if we can record the error caused by rounding in each division and apply it to the next division, we can reduce or even avoid the final error.
Let’s use cum_err
to denote the cumulative error.
- The initial value is cum_err = 0;
- (20 + 0) / 3 = 7, cum_err = -1;
- (20 - 1) / 3 = 6, cum_err = 1;
- (20 + 1) / 3 = 7, cum_err = 0.
The final result is 7 + 6 + 7 = 20, which is equal to the result of the single division.
use int_div_cum_error::{DivCumErr, CumErr, Rounding};
let mut cum_err = CumErr::new();
let q = 20.checked_div_with_cum_err(3, Rounding::Round, &mut cum_err).unwrap();
assert_eq!(q, 7);
let q = 20.checked_div_with_cum_err(3, Rounding::Round, &mut cum_err).unwrap();
assert_eq!(q, 6);
let q = 20.checked_div_with_cum_err(3, Rounding::Round, &mut cum_err).unwrap();
assert_eq!(q, 7);
Structs§
- CumErr
- Cumulative error.
Enums§
- Rounding
- Rounding kinds.
Traits§
- DivCum
Err - Trait for division with rounding and cumulative error.