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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::types::{Balance, Fraction};
use num_traits::One;
use sp_arithmetic::helpers_128bit::multiply_by_rational_with_rounding;
use sp_arithmetic::per_things::Rounding;
use sp_arithmetic::{FixedPointNumber, FixedU128, Rational128};
pub const SMALLEST_NON_ZERO: Fraction = Fraction::from_bits(1);
pub const DIV: u128 = 1u128 << 127;
pub fn frac(n: u128, d: u128) -> Fraction {
debug_assert!(
d >= n,
"fraction should be less than or equal to 1 -> denominator should be greater equal the numerator"
);
Fraction::from_bits(multiply_by_rational_with_rounding(n, DIV, d, Rounding::NearestPrefDown).unwrap_or(DIV))
}
pub fn to_fixed(f: Fraction) -> FixedU128 {
debug_assert!(f <= Fraction::ONE);
FixedU128::from_inner(
multiply_by_rational_with_rounding(FixedU128::DIV, f.to_bits(), DIV, Rounding::NearestPrefDown).unwrap_or(DIV),
)
}
pub fn from_fixed(f: FixedU128) -> Fraction {
debug_assert!(f <= FixedU128::one(), "fraction should be less than or equal to 1");
Fraction::from_bits(
multiply_by_rational_with_rounding(f.into_inner(), DIV, FixedU128::DIV, Rounding::NearestPrefDown)
.unwrap_or(DIV),
)
}
pub fn to_rational(f: Fraction) -> Rational128 {
Rational128::from(f.to_bits(), DIV)
}
pub fn multiply_by_balance(f: Fraction, b: Balance) -> Balance {
debug_assert!(f <= Fraction::ONE);
multiply_by_rational_with_rounding(b, f.to_bits(), DIV, Rounding::NearestPrefDown).unwrap_or(DIV)
}
pub fn multiply_by_fixed(fraction: Fraction, fixed: FixedU128) -> FixedU128 {
debug_assert!(fraction <= Fraction::ONE);
FixedU128::from_inner(
multiply_by_rational_with_rounding(fixed.into_inner(), fraction.to_bits(), DIV, Rounding::NearestPrefDown)
.unwrap_or(DIV),
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{any_fixed, fixed_to_high_precision, fraction_to_high_precision};
use num_traits::One;
use proptest::prelude::*;
use sp_arithmetic::FixedPointNumber;
#[test]
fn fraction_representation() {
assert_eq!(Fraction::from_num(0.25), Fraction::ONE / 4);
let expected_smallest_non_zero = Fraction::ONE / (u128::MAX / 2);
assert_eq!(SMALLEST_NON_ZERO, expected_smallest_non_zero);
assert_eq!(Fraction::from_num(0.5), Fraction::from_bits(DIV / 2));
assert_eq!(Fraction::from_num(1), Fraction::from_bits(DIV));
}
#[test]
fn fraction_works() {
let f = frac(1, 2);
let expected = Fraction::from_bits(DIV / 2);
assert_eq!(f, expected);
let f = frac(1e16 as u128, 2e16 as u128);
let expected = Fraction::from_bits(DIV / 2);
assert_eq!(f, expected);
}
#[test]
fn to_fixed_works() {
let fraction = Fraction::one() / 100;
let expected = FixedU128::from((1, 100));
assert_eq!(to_fixed(fraction), expected);
}
#[test]
fn from_fixed_works() {
let fixed = FixedU128::from((1, 100));
let expected = Fraction::one() / 100;
assert_eq!(from_fixed(fixed), expected);
}
#[test]
fn multiply_by_balance_works() {
let frac = Fraction::from_num(0.25);
let balance = 1e16 as Balance;
let expected = balance / 4;
assert_eq!(multiply_by_balance(frac, balance), expected);
}
#[test]
fn multiply_by_fixed_works() {
let frac = Fraction::from_num(0.25);
let fixed = FixedU128::saturating_from_integer(1e16 as u64);
let expected = fixed / FixedU128::from(4);
assert_eq!(multiply_by_fixed(frac, fixed), expected);
let fixed = FixedU128::from((1, 100));
let expected = FixedU128::from((1, 400));
assert_eq!(multiply_by_fixed(frac, fixed), expected);
}
fn typical_fraction() -> impl Strategy<Value = Fraction> {
(1u128..110_000).prop_map(|n| frac(2, n.max(1).saturating_add(1)))
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(1_000))]
#[test]
fn fraction_times_fixed_precision(
fraction in typical_fraction(),
fixed in any_fixed(),
) {
let rational = fixed_to_high_precision(fixed) * fraction_to_high_precision(fraction);
let conversion = fixed * to_fixed(fraction);
let conversion_distance = (rational.clone() - fixed_to_high_precision(conversion)).abs();
let multiply = multiply_by_fixed(fraction, fixed);
let multiply_distance = (rational - fixed_to_high_precision(multiply)).abs();
prop_assert!(multiply_distance <= conversion_distance);
}
}
}