Newer
Older
use codec::{Decode, Encode};
use primitives::U256;
use rstd::convert::{Into, TryFrom, TryInto};
traits::{Bounded, Saturating},
Perbill, Percent, Permill, Perquintill,
};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
/// An unsigned fixed point number. Can hold any value in the range [0, 340_282_366_920_938_463_464]
/// with fixed point accuracy of 10 ** 18.
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct FixedU128(u128);
const DIV: u128 = 1_000_000_000_000_000_000;
impl FixedU128 {
/// Create self from a natural number.
/// Note that this might be lossy.
pub fn from_natural(int: u128) -> Self {
Self(int.saturating_mul(DIV))
}
/// Accuracy of `FixedU128`.
pub const fn accuracy() -> u128 {
/// Raw constructor. Equal to `parts / DIV`.
pub fn from_parts(parts: u128) -> Self {
Self(parts)
}
/// Creates self from a rational number. Equal to `n/d`.
/// Note that this might be lossy.
pub fn from_rational(n: u128, d: u128) -> Self {
Self(
(U256::from(n).saturating_mul(U256::from(DIV)) / U256::from(d).max(U256::one()))
.try_into()
.unwrap_or_else(|_| Bounded::max_value()),
)
}
/// Consume self and return the inner raw `u128` value.
/// Note this is a low level function, as the returned value is represented with accuracy.
pub fn deconstruct(self) -> u128 {
/// Takes the reciprocal(inverse) of FixedU128, 1/x
pub fn recip(&self) -> Option<Self> {
Self::from_natural(1u128).checked_div(self)
}
/// Checked add. Same semantic to `num_traits::CheckedAdd`.
pub fn checked_add(&self, rhs: &Self) -> Option<Self> {
self.0.checked_add(rhs.0).map(Self)
}
/// Checked sub. Same semantic to `num_traits::CheckedSub`.
pub fn checked_sub(&self, rhs: &Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
/// Checked mul. Same semantic to `num_traits::CheckedMul`.
pub fn checked_mul(&self, rhs: &Self) -> Option<Self> {
if let Some(r) = U256::from(self.0)
.checked_mul(U256::from(rhs.0))
.and_then(|n| n.checked_div(U256::from(DIV)))
{
if let Ok(r) = TryInto::<u128>::try_into(r) {
return Some(Self(r));
}
}
None
}
/// Checked div. Same semantic to `num_traits::CheckedDiv`.
pub fn checked_div(&self, rhs: &Self) -> Option<Self> {
if let Some(r) = U256::from(self.0)
.checked_mul(U256::from(DIV))
.and_then(|n| n.checked_div(U256::from(rhs.0)))
{
if let Ok(r) = TryInto::<u128>::try_into(r) {
return Some(Self(r));
}
}
None
}
/// Checked mul for int type `N`.
pub fn checked_mul_int<N>(&self, other: &N) -> Option<N>
where
N: Copy + TryFrom<u128> + TryInto<u128>,
{
if let Ok(n) = N::try_into(*other) {
if let Some(n) = U256::from(self.0)
.checked_mul(U256::from(n))
.and_then(|n| n.checked_div(U256::from(DIV)))
{
if let Ok(r) = TryInto::<u128>::try_into(n) {
if let Ok(r) = TryInto::<N>::try_into(r) {
return Some(r);
}
}
}
}
None
}
/// Checked div for int type `N`.
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
pub fn checked_div_int<N>(&self, other: &N) -> Option<N>
where
N: Copy + TryFrom<u128> + TryInto<u128>,
{
if let Ok(n) = N::try_into(*other) {
if let Some(n) = self.0.checked_div(n).and_then(|n| n.checked_div(DIV)) {
if let Ok(r) = TryInto::<N>::try_into(n) {
return Some(r);
}
}
}
None
}
}
impl Saturating for FixedU128 {
fn saturating_add(self, rhs: Self) -> Self {
Self(self.0.saturating_add(rhs.0))
}
fn saturating_mul(self, rhs: Self) -> Self {
Self(
(U256::from(self.0).saturating_mul(U256::from(rhs.0)) / U256::from(DIV))
.try_into()
.unwrap_or_else(|_| Bounded::max_value()),
)
}
fn saturating_sub(self, rhs: Self) -> Self {
Self(self.0.saturating_sub(rhs.0))
}
}
impl Bounded for FixedU128 {
fn max_value() -> Self {
Self(u128::max_value())
}
fn min_value() -> Self {
Self(0u128)
}
}
impl rstd::fmt::Debug for FixedU128 {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut rstd::fmt::Formatter) -> rstd::fmt::Result {
write!(f, "FixedU128({},{})", self.0 / DIV, (self.0 % DIV) / 1000)
}
#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut rstd::fmt::Formatter) -> rstd::fmt::Result {
Ok(())
}
}
macro_rules! impl_perthing_into_fixed_u128 {
($perthing:ty) => {
impl Into<FixedU128> for $perthing {
fn into(self) -> FixedU128 {
FixedU128::from_rational(self.deconstruct().into(), <$perthing>::accuracy().into())
}
}
};
}
impl_perthing_into_fixed_u128!(Percent);
impl_perthing_into_fixed_u128!(Permill);
impl_perthing_into_fixed_u128!(Perbill);
impl_perthing_into_fixed_u128!(Perquintill);
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#[cfg(test)]
mod tests {
use super::*;
fn max() -> FixedU128 {
FixedU128::from_parts(u128::max_value())
}
#[test]
fn fixed128_semantics() {
assert_eq!(FixedU128::from_rational(5, 2).0, 5 * 1_000_000_000_000_000_000 / 2);
assert_eq!(FixedU128::from_rational(5, 2), FixedU128::from_rational(10, 4));
assert_eq!(FixedU128::from_rational(5, 0), FixedU128::from_rational(5, 1));
// biggest value that can be created.
assert_ne!(max(), FixedU128::from_natural(340_282_366_920_938_463_463));
assert_eq!(max(), FixedU128::from_natural(340_282_366_920_938_463_464));
}
#[test]
fn fixed128_operation() {
let a = FixedU128::from_natural(2);
let b = FixedU128::from_natural(1);
assert_eq!(a.checked_add(&b), Some(FixedU128::from_natural(1 + 2)));
assert_eq!(a.checked_sub(&b), Some(FixedU128::from_natural(2 - 1)));
assert_eq!(a.checked_mul(&b), Some(FixedU128::from_natural(1 * 2)));
assert_eq!(a.checked_div(&b), Some(FixedU128::from_rational(2, 1)));
let a = FixedU128::from_rational(5, 2);
let b = FixedU128::from_rational(3, 2);
assert_eq!(a.checked_add(&b), Some(FixedU128::from_rational(8, 2)));
assert_eq!(a.checked_sub(&b), Some(FixedU128::from_rational(2, 2)));
assert_eq!(a.checked_mul(&b), Some(FixedU128::from_rational(15, 4)));
assert_eq!(a.checked_div(&b), Some(FixedU128::from_rational(10, 6)));
let a = FixedU128::from_natural(120);
let b = 2i32;
assert_eq!(a.checked_div_int::<i32>(&b), Some(60));
let a = FixedU128::from_rational(20, 1);
let b = 2i32;
assert_eq!(a.checked_div_int::<i32>(&b), Some(10));
let a = FixedU128::from_natural(120);
let b = 2i32;
assert_eq!(a.checked_mul_int::<i32>(&b), Some(240));
let a = FixedU128::from_rational(1, 2);
let b = 20i32;
assert_eq!(a.checked_mul_int::<i32>(&b), Some(10));
}
#[test]
fn checked_div_with_zero_should_be_none() {
let a = FixedU128::from_natural(1);
let b = FixedU128::from_natural(0);
assert_eq!(a.checked_div(&b), None);
}
#[test]
fn checked_div_int_with_zero_should_be_none() {
let a = FixedU128::from_natural(1);
let b = 0i32;
assert_eq!(a.checked_div_int(&b), None);
}
#[test]
fn under_flow_should_be_none() {
let a = FixedU128::from_natural(2);
let b = FixedU128::from_natural(3);
assert_eq!(a.checked_sub(&b), None);
}
#[test]
fn over_flow_should_be_none() {
let a = FixedU128::from_parts(u128::max_value() - 1);
let b = FixedU128::from_parts(2);
assert_eq!(a.checked_add(&b), None);
let a = FixedU128::max_value();
let b = FixedU128::from_rational(2, 1);
assert_eq!(a.checked_mul(&b), None);
let a = FixedU128::from_natural(255);
let b = 2u8;
assert_eq!(a.checked_mul_int(&b), None);
let a = FixedU128::from_natural(256);
let b = 1u8;
assert_eq!(a.checked_div_int(&b), None);
}
#[test]
fn perthing_into_fixed_u128() {
let ten_percent_percent: FixedU128 = Percent::from_percent(10).into();
assert_eq!(ten_percent_percent.deconstruct(), DIV / 10);
let ten_percent_permill: FixedU128 = Permill::from_percent(10).into();
assert_eq!(ten_percent_permill.deconstruct(), DIV / 10);
let ten_percent_perbill: FixedU128 = Perbill::from_percent(10).into();
assert_eq!(ten_percent_perbill.deconstruct(), DIV / 10);
let ten_percent_perquintill: FixedU128 = Perquintill::from_percent(10).into();
assert_eq!(ten_percent_perquintill.deconstruct(), DIV / 10);
#[test]
fn recip_should_work() {
let a = FixedU128::from_natural(2);
assert_eq!(a.recip(), Some(FixedU128::from_rational(1, 2)));
let a = FixedU128::from_natural(2);
assert_eq!(a.recip().unwrap().checked_mul_int(&4i32), Some(2i32));
let a = FixedU128::from_rational(100, 121);
assert_eq!(a.recip(), Some(FixedU128::from_rational(121, 100)));
let a = FixedU128::from_rational(1, 2);
assert_eq!(a.recip().unwrap().checked_mul(&a), Some(FixedU128::from_natural(1)));
let a = FixedU128::from_natural(0);
assert_eq!(a.recip(), None);
}