mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 12:17:58 +00:00
Enable fixed point u128 (#6214)
* Add fixed u128. * remove move * Change sat_from_integer impl. * checked_pow is always positive * Revert. * rename fixed file * Rename to FixedI * rename fixed file * Add newline. * Use Multiplier in impls. * Renames negate() to saturating_negate(). * Uncomment test. * Add Signed to macro. * Add some tests for Saturating trait.
This commit is contained in:
+18
-18
@@ -16,19 +16,19 @@
|
||||
// limitations under the License.
|
||||
|
||||
//! # Running
|
||||
//! Running this fuzzer can be done with `cargo hfuzz run fixed`. `honggfuzz` CLI options can
|
||||
//! Running this fuzzer can be done with `cargo hfuzz run fixed_point`. `honggfuzz` CLI options can
|
||||
//! be used by setting `HFUZZ_RUN_ARGS`, such as `-n 4` to use 4 threads.
|
||||
//!
|
||||
//! # Debugging a panic
|
||||
//! Once a panic is found, it can be debugged with
|
||||
//! `cargo hfuzz run-debug fixed hfuzz_workspace/fixed/*.fuzz`.
|
||||
//! `cargo hfuzz run-debug fixed_point hfuzz_workspace/fixed_point/*.fuzz`.
|
||||
//!
|
||||
//! # More information
|
||||
//! More information about `honggfuzz` can be found
|
||||
//! [here](https://docs.rs/honggfuzz/).
|
||||
|
||||
use honggfuzz::fuzz;
|
||||
use sp_arithmetic::{FixedPointNumber, Fixed64, traits::Saturating};
|
||||
use sp_arithmetic::{FixedPointNumber, FixedI64, traits::Saturating};
|
||||
|
||||
fn main() {
|
||||
loop {
|
||||
@@ -38,21 +38,21 @@ fn main() {
|
||||
|
||||
// Check `from_rational` and division are consistent.
|
||||
if y != 0 {
|
||||
let f1 = Fixed64::saturating_from_integer(x) / Fixed64::saturating_from_integer(y);
|
||||
let f2 = Fixed64::saturating_from_rational(x, y);
|
||||
let f1 = FixedI64::saturating_from_integer(x) / FixedI64::saturating_from_integer(y);
|
||||
let f2 = FixedI64::saturating_from_rational(x, y);
|
||||
assert_eq!(f1.into_inner(), f2.into_inner());
|
||||
}
|
||||
|
||||
// Check `saturating_mul`.
|
||||
let a = Fixed64::saturating_from_rational(2, 5);
|
||||
let b = a.saturating_mul(Fixed64::saturating_from_integer(x));
|
||||
let a = FixedI64::saturating_from_rational(2, 5);
|
||||
let b = a.saturating_mul(FixedI64::saturating_from_integer(x));
|
||||
let n = b.into_inner() as i128;
|
||||
let m = 2i128 * x * Fixed64::accuracy() as i128 / 5i128;
|
||||
let m = 2i128 * x * FixedI64::accuracy() as i128 / 5i128;
|
||||
assert_eq!(n, m);
|
||||
|
||||
// Check `saturating_mul` and division are inverse.
|
||||
if x != 0 {
|
||||
assert_eq!(a, b / Fixed64::saturating_from_integer(x));
|
||||
assert_eq!(a, b / FixedI64::saturating_from_integer(x));
|
||||
}
|
||||
|
||||
// Check `reciprocal`.
|
||||
@@ -60,22 +60,22 @@ fn main() {
|
||||
assert_eq!(a, r);
|
||||
|
||||
// Check addition.
|
||||
let a = Fixed64::saturating_from_integer(x);
|
||||
let b = Fixed64::saturating_from_integer(y);
|
||||
let c = Fixed64::saturating_from_integer(x.saturating_add(y));
|
||||
let a = FixedI64::saturating_from_integer(x);
|
||||
let b = FixedI64::saturating_from_integer(y);
|
||||
let c = FixedI64::saturating_from_integer(x.saturating_add(y));
|
||||
assert_eq!(a.saturating_add(b), c);
|
||||
|
||||
// Check substraction.
|
||||
let a = Fixed64::saturating_from_integer(x);
|
||||
let b = Fixed64::saturating_from_integer(y);
|
||||
let c = Fixed64::saturating_from_integer(x.saturating_sub(y));
|
||||
let a = FixedI64::saturating_from_integer(x);
|
||||
let b = FixedI64::saturating_from_integer(y);
|
||||
let c = FixedI64::saturating_from_integer(x.saturating_sub(y));
|
||||
assert_eq!(a.saturating_sub(b), c);
|
||||
|
||||
// Check `saturating_mul_acc_int`.
|
||||
let a = Fixed64::saturating_from_rational(2, 5);
|
||||
let a = FixedI64::saturating_from_rational(2, 5);
|
||||
let b = a.saturating_mul_acc_int(x);
|
||||
let xx = Fixed64::saturating_from_integer(x);
|
||||
let d = a.saturating_mul(xx).saturating_add(xx).into_inner() as i128 / Fixed64::accuracy() as i128;
|
||||
let xx = FixedI64::saturating_from_integer(x);
|
||||
let d = a.saturating_mul(xx).saturating_add(xx).into_inner() as i128 / FixedI64::accuracy() as i128;
|
||||
assert_eq!(b, d);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user