const_utils/i128_func.rs
1/// Returns 1 if `n` is zero and 0 if `n` is greater than zero
2///
3/// ```rust
4/// use const_utils::i128::is_zero;
5/// assert!(is_zero(0) == 1);
6/// assert!(is_zero(1) == 0);
7/// assert!(is_zero(2) == 0);
8/// assert!(is_zero(3) == 0);
9/// assert!(is_zero(5) == 0);
10/// ```
11pub const fn is_zero(n: i128) -> i128 {
12 (n == 0) as i128
13}
14
15/// Returns 1 if `n` is zero and 0 if `n` is greater than zero
16///
17/// ```rust
18/// use const_utils::i128::not_zero;
19/// assert!(not_zero(0) == 0);
20/// assert!(not_zero(1) == 1);
21/// assert!(not_zero(2) == 1);
22/// assert!(not_zero(3) == 1);
23/// assert!(not_zero(5) == 1);
24/// ```
25pub const fn not_zero(n: i128) -> i128 {
26 is_zero(is_zero(n))
27}
28
29/// Returns `if_true` if cond is true, and otherwise returns `if_false`
30///
31/// ```rust
32/// use const_utils::i128::cond;
33/// assert!(cond(true, 33, 121) == 33);
34/// assert!(cond(false, 33, 121) == 121);
35/// ```
36pub const fn cond(cond: bool, if_true: i128, if_false: i128) -> i128 {
37 (cond as i128) * if_true + (!cond as i128) * if_false
38}
39
40/// Returns `dividend - divisor` if divisor isn't zero, and `core::i128::MAX`
41/// otherwise.
42///
43/// ```rust
44/// use const_utils::i128::safe_div;
45/// assert!(safe_div(100, 10) == 10);
46/// assert!(safe_div(100, 0) == core::i128::MAX);
47/// ```
48pub const fn safe_div(dividend: i128, divisor: i128) -> i128 {
49 let val = dividend / (divisor + is_zero(divisor));
50 cond(divisor == 0, core::i128::MAX, val)
51}
52
53///Returns the maximum of `a` and `b`
54///
55/// ```rust
56/// use const_utils::i128::max;
57/// assert!(max(100, 10) == 100);
58/// assert!(max(0, 100) == 100);
59/// ```
60pub const fn max(a: i128, b: i128) -> i128 {
61 cond(a > b, a, b)
62}
63
64///Returns the minimum of `a` and `b`
65///
66/// ```rust
67/// use const_utils::i128::min;
68/// assert!(min(100, 10) == 10);
69/// assert!(min(0, 100) == 0);
70/// ```
71pub const fn min(a: i128, b: i128) -> i128 {
72 cond(a > b, b, a)
73}