From 0135909cc8ddf102e34a37517c843080a7fcf061 Mon Sep 17 00:00:00 2001 From: DrunkRandomWalker Date: Thu, 7 Sep 2023 16:27:32 -0400 Subject: [PATCH] added neg operator, iter::sum bumped version --- Cargo.lock | 6 +-- packages/injective-math/Cargo.toml | 2 +- .../src/fp_decimal/arithmetic.rs | 20 +++++++++ packages/injective-math/src/fp_decimal/mod.rs | 42 ++++++++++++++++--- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c49e58b6..3fa23b90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -763,7 +763,7 @@ dependencies = [ "cw-storage-plus 0.15.1", "ethereum-types", "hex", - "injective-math 0.1.18", + "injective-math 0.1.19", "schemars", "serde 1.0.164", "serde-json-wasm 0.4.1", @@ -790,7 +790,7 @@ dependencies = [ [[package]] name = "injective-math" -version = "0.1.18" +version = "0.1.19" dependencies = [ "bigint", "cosmwasm-schema", @@ -840,7 +840,7 @@ dependencies = [ "cosmwasm-std", "cw-multi-test", "injective-cosmwasm 0.2.12", - "injective-math 0.1.18", + "injective-math 0.1.19", "rand 0.4.6", "secp256k1", "serde 1.0.164", diff --git a/packages/injective-math/Cargo.toml b/packages/injective-math/Cargo.toml index a154f474..87da1bbc 100644 --- a/packages/injective-math/Cargo.toml +++ b/packages/injective-math/Cargo.toml @@ -6,7 +6,7 @@ license = "Apache-2.0" name = "injective-math" readme = "README.md" repository = "https://github.com/InjectiveLabs/cw-injective/tree/master/packages/injective-math" -version = "0.1.18" +version = "0.1.19" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/injective-math/src/fp_decimal/arithmetic.rs b/packages/injective-math/src/fp_decimal/arithmetic.rs index 03ad548a..3f058aba 100644 --- a/packages/injective-math/src/fp_decimal/arithmetic.rs +++ b/packages/injective-math/src/fp_decimal/arithmetic.rs @@ -1,5 +1,6 @@ /// Arithmetic operators for FPDecimal use crate::fp_decimal::{FPDecimal, U256}; +use std::iter; use std::ops; impl FPDecimal { @@ -162,6 +163,15 @@ impl ops::Div for FPDecimal { } } +impl<'a> iter::Sum<&'a Self> for FPDecimal { + fn sum(iter: I) -> Self + where + I: Iterator, + { + iter.fold(FPDecimal::ZERO, |a, b| a + *b) + } +} + #[cfg(test)] mod tests { use std::str::FromStr; @@ -578,4 +588,14 @@ mod tests { let ans = lhs.abs_diff(&rhs); assert_eq!(FPDecimal::from(3u128), ans); } + #[test] + fn test_chain_sum() { + let vector = vec![FPDecimal::ZERO, FPDecimal::ONE, FPDecimal::TWO, FPDecimal::THREE]; + assert_eq!(FPDecimal::SIX, vector.iter().sum()); + } + #[test] + fn test_chain_sum_equal_zero() { + let vector = vec![FPDecimal::ZERO, FPDecimal::ONE, FPDecimal::TWO, -FPDecimal::THREE]; + assert_eq!(FPDecimal::ZERO, vector.iter().sum()); + } } diff --git a/packages/injective-math/src/fp_decimal/mod.rs b/packages/injective-math/src/fp_decimal/mod.rs index 4fb87917..30ca6ebe 100644 --- a/packages/injective-math/src/fp_decimal/mod.rs +++ b/packages/injective-math/src/fp_decimal/mod.rs @@ -1,3 +1,4 @@ +use std::ops::Neg; use std::str::FromStr; use bigint::U256; @@ -77,12 +78,23 @@ impl From for FPDecimal { } } -// #[cfg(not(target_arch = "wasm32"))] -// impl convert::From for f32 { -// fn from(x: FPDecimal) -> f32 { -// f32::from_str(&x.to_string()).unwrap() -// } -// } +impl Neg for FPDecimal { + type Output = FPDecimal; + fn neg(mut self) -> Self::Output { + if self.is_zero() { + return self; + } + match self.sign { + 0 => { + self.sign = 1; + } + _ => { + self.sign = 0; + } + } + self + } +} impl FPDecimal { pub const MAX: FPDecimal = FPDecimal { num: U256::MAX, sign: 1 }; @@ -247,3 +259,21 @@ mod log; pub mod scale; mod serde; mod trigonometry; + +#[cfg(test)] +mod tests { + use crate::FPDecimal; + #[test] + fn test_neg_sign() { + let lhs = FPDecimal::ZERO - FPDecimal::ONE; + let rhs = -FPDecimal::ONE; + assert_eq!(lhs, rhs); + } + + #[test] + fn test_neg_zero() { + let lhs = FPDecimal::ZERO; + let rhs = -FPDecimal::ZERO; + assert_eq!(lhs, rhs); + } +}