Function fraction::division::divide_integral

source ·
pub fn divide_integral<I, Consumer>(
    dividend: I,
    divisor: I,
    consumer: Consumer,
) -> Result<DivisionState<I>, DivisionError>
where Consumer: FnMut(u8) -> Result<bool, DivisionError>, I: Clone + GenericInteger,
Expand description

Divide two numbers and produce every single digit of the whole part of the resulting number

WARNING: Negative numbers as arguments are not supported.

Returns remainder of the division If the consumer returns Ok(true) keeps on calculation If the consumer returns Ok(false) stops calculation and returns the remainder If the consumer returns Err(_) the calculation will be stopped and the error will be passed as the result value

§Examples

use fraction::division::divide_integral;

let mut result: [u8; 2] = [0; 2];
let mut ptr: usize = 0;

let state = divide_integral(30, 2, |d| {
    result[ptr] = d;
    ptr += 1;
    Ok(true)
}).unwrap();

assert_eq!([1, 5], result);
assert_eq!(state.remainder, 0);
assert_eq!(state.divisor, 2);
use fraction::division::divide_integral;

let mut result: u8 = 0;

let state = divide_integral(30, 2, |d| {
    result = d;
    Ok(false)
}).unwrap();

assert_eq!(result, 1);
assert_eq!(state.remainder, 10);
assert_eq!(state.divisor, 2);