1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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:?}")
}