Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ordering functions for IFP #262

Merged
merged 11 commits into from
Jul 24, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Description of the upcoming release here.
### Fixed

- [#258](https://github.com/FuelLabs/sway-libs/pull/258) Fixes incorrect intructions on how to run tests in README and docs hub.
- [#262](https://github.com/FuelLabs/sway-libs/pull/262) Fixes incorrect ordering comparison for IFP64, IFP128 and IFP256.

#### Breaking

Expand Down
75 changes: 69 additions & 6 deletions libs/src/fixed_point/ifp128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,23 @@ impl core::ops::Eq for IFP128 {

impl core::ops::Ord for IFP128 {
fn gt(self, other: Self) -> bool {
if self.non_negative && !self.non_negative {
if self.non_negative && !other.non_negative {
true
} else if !self.non_negative && self.non_negative {
} else if !self.non_negative && other.non_negative {
false
} else if self.non_negative && self.non_negative {
} else if self.non_negative && other.non_negative {
self.underlying > other.underlying
} else {
self.underlying < other.underlying
}
}

fn lt(self, other: Self) -> bool {
if self.non_negative && !self.non_negative {
if self.non_negative && !other.non_negative {
false
} else if !self.non_negative && self.non_negative {
} else if !self.non_negative && other.non_negative {
true
} else if self.non_negative && self.non_negative {
} else if self.non_negative && other.non_negative {
self.underlying < other.underlying
} else {
self.underlying > other.underlying
Expand Down Expand Up @@ -568,3 +568,66 @@ impl Power for IFP128 {
}
}
}

#[test]
fn test_ord() {
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
let num = IFP128::min();
let num2 = IFP128::min();

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));

let num = IFP128::min();
let num2 = IFP128::from_uint(42_u64);

assert(num < num2);
assert(num2 > num);

let num = IFP128::min();
let num2 = IFP128::max();

assert(num < num2);
assert(num2 > num);

let num = IFP128::from_uint(42_u64);
let num2 = IFP128::min();

assert(num > num2);
assert(num2 < num);

let num = IFP128::from_uint(42_u64);
let num2 = IFP128::from_uint(42_u64);

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));

let num = IFP128::from_uint(42_u64);
let num2 = IFP128::max();

assert(num < num2);
assert(num2 > num);

let num = IFP128::max();
let num2 = IFP128::min();

assert(num > num2);
assert(num2 < num);

let num = IFP128::max();
let num2 = IFP128::from_uint(42_u64);

assert(num > num2);
assert(num2 < num);

let num = IFP128::max();
let num2 = IFP128::max();

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));
}
75 changes: 69 additions & 6 deletions libs/src/fixed_point/ifp256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,23 @@ impl core::ops::Eq for IFP256 {

impl core::ops::Ord for IFP256 {
fn gt(self, other: Self) -> bool {
if self.non_negative && !self.non_negative {
if self.non_negative && !other.non_negative {
true
} else if !self.non_negative && self.non_negative {
} else if !self.non_negative && other.non_negative {
false
} else if self.non_negative && self.non_negative {
} else if self.non_negative && other.non_negative {
self.underlying > other.underlying
} else {
self.underlying < other.underlying
}
}

fn lt(self, other: Self) -> bool {
if self.non_negative && !self.non_negative {
if self.non_negative && !other.non_negative {
false
} else if !self.non_negative && self.non_negative {
} else if !self.non_negative && other.non_negative {
true
} else if self.non_negative && self.non_negative {
} else if self.non_negative && other.non_negative {
self.underlying < other.underlying
} else {
self.underlying > other.underlying
Expand Down Expand Up @@ -568,3 +568,66 @@ impl Power for IFP256 {
}
}
}

#[test]
fn test_ord() {
let num = IFP256::min();
let num2 = IFP256::min();

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));

let num = IFP256::min();
let num2 = IFP256::from_uint(42_u64);

assert(num < num2);
assert(num2 > num);

let num = IFP256::min();
let num2 = IFP256::max();

assert(num < num2);
assert(num2 > num);

let num = IFP256::from_uint(42_u64);
let num2 = IFP256::min();

assert(num > num2);
assert(num2 < num);

let num = IFP256::from_uint(42_u64);
let num2 = IFP256::from_uint(42_u64);

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));

let num = IFP256::from_uint(42_u64);
let num2 = IFP256::max();

assert(num < num2);
assert(num2 > num);

let num = IFP256::max();
let num2 = IFP256::min();

assert(num > num2);
assert(num2 < num);

let num = IFP256::max();
let num2 = IFP256::from_uint(42_u64);

assert(num > num2);
assert(num2 < num);

let num = IFP256::max();
let num2 = IFP256::max();

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));
}
75 changes: 69 additions & 6 deletions libs/src/fixed_point/ifp64.sw
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,23 @@ impl core::ops::Eq for IFP64 {

impl core::ops::Ord for IFP64 {
fn gt(self, other: Self) -> bool {
if self.non_negative && !self.non_negative {
if self.non_negative && !other.non_negative {
true
} else if !self.non_negative && self.non_negative {
} else if !self.non_negative && other.non_negative {
false
} else if self.non_negative && self.non_negative {
} else if self.non_negative && other.non_negative {
self.underlying > other.underlying
} else {
self.underlying < other.underlying
}
}

fn lt(self, other: Self) -> bool {
if self.non_negative && !self.non_negative {
if self.non_negative && !other.non_negative {
false
} else if !self.non_negative && self.non_negative {
} else if !self.non_negative && other.non_negative {
true
} else if self.non_negative && self.non_negative {
} else if self.non_negative && other.non_negative {
self.underlying < other.underlying
} else {
self.underlying > other.underlying
Expand Down Expand Up @@ -568,3 +568,66 @@ impl Power for IFP64 {
}
}
}

#[test]
fn test_ord() {
let num = IFP64::min();
let num2 = IFP64::min();

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));

let num = IFP64::min();
let num2 = IFP64::from_uint(42_u32);

assert(num < num2);
assert(num2 > num);

let num = IFP64::min();
let num2 = IFP64::max();

assert(num < num2);
assert(num2 > num);

let num = IFP64::from_uint(42_u32);
let num2 = IFP64::min();

assert(num > num2);
assert(num2 < num);

let num = IFP64::from_uint(42_u32);
let num2 = IFP64::from_uint(42_u32);

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));

let num = IFP64::from_uint(42_u32);
let num2 = IFP64::max();

assert(num < num2);
assert(num2 > num);

let num = IFP64::max();
let num2 = IFP64::min();

assert(num > num2);
assert(num2 < num);

let num = IFP64::max();
let num2 = IFP64::from_uint(42_u32);

assert(num > num2);
assert(num2 < num);

let num = IFP64::max();
let num2 = IFP64::max();

assert(!(num > num2));
assert(!(num < num2));
assert(!(num2 < num));
assert(!(num2 > num));
}
Loading