Documentation
pub struct Cal {
    base: u64,
    pec: u64,
    step: f64,
}

impl Cal {
    pub fn new(base: u64, pec: u64) -> Self {
        let step = base as f64 / pec as f64;
        Self { base, pec, step }
    }
    /**

    . 计算回传比例

    # Examples

    ```
    use caisin::cal::Cal;
    let mut n = 0;
    let cal = &Cal::new(100, 20);
    for i in 0..1000 {
        if cal.cal(i) {
            n += 1;
        }
    }
    assert_eq!(n, 200);
    ```
    */
    pub fn cal(&self, num: u64) -> bool {
        if self.pec >= self.base {
            return true;
        }
        if self.pec <= 0 {
            return false;
        }
        let md = num % self.base;
        let a = md as f64 / self.step;
        let f = a.round() * self.step;
        let b = f.round();
        md == b as u64
    }
}
//回收是消耗/出价的指定倍数回传计算
pub fn cal_range(
    cost: f64, //消耗
    bid: f64,  //出价
    base: f64, //倍数
    his: f64,  //回收
) -> (bool, f64) {
    //当前回传倍数
    let now = cost / bid;
    //当前消耗是否在下次消耗范围内
    let mut in_range = false;

    if cost > his * bid {
        //已有倍数是基础倍数的几倍向上取整得到下次是第几次回传
        let ceil = (his / base).floor();
        // 下次回传倍数开始值
        let next = (ceil + 1.0) * base;
        // 下次回传最低消耗
        let f2 = bid * next;
        //消耗大于等于下次回传倍数
        if cost >= f2 {
            in_range = true;
        }
    }
    (in_range, now)
}

#[test]
fn test_cal() {
    let ret = cal_range(38.8, 10.0, 1.5, 100.0);
    println!("{ret:?}")
}