Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions primitives/safe-math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,23 @@ pub trait FixedExt: Fixed {
/// Safe sqrt with good precision
fn checked_sqrt(&self, epsilon: Self) -> Option<Self> {
let zero = Self::saturating_from_num(0);
let one = Self::saturating_from_num(1);
let two = Self::saturating_from_num(2);

if *self < zero {
return None;
}

let mut high = *self;
let mut low = zero;
let mut high;
let mut low;
if *self > one {
high = *self;
low = zero;
} else {
high = one;
low = *self;
}

let mut middle = high.saturating_add(low).safe_div(two);

let mut iteration: i32 = 0;
Expand Down Expand Up @@ -268,7 +277,8 @@ mod tests {

let result: Option<U110F18> = value.checked_sqrt(epsilon);
assert!(result.is_some());
assert_eq!(result.unwrap(), U110F18::from_num(0.0));
let sqrt_result: U110F18 = result.unwrap();
assert!(sqrt_result.abs_diff(U110F18::from_num(0)) <= epsilon);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions primitives/swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = { workspace = true }

[dependencies]
alloy-primitives = { workspace = true }
approx = { workspace = true }
safe-math = { workspace = true }
sp-arithmetic = { workspace = true }
sp-std = { workspace = true }
Expand Down
Loading
Loading