Skip to content

Commit

Permalink
Fix negative zero for i257 eq gt (#278)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

## Pull Request type

<!-- Please try to limit your pull request to one type; submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:

- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no API changes)
- [ ] Build-related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

Issue Number: N/A

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

-
-
-

## Does this introduce a breaking change?

- [ ] Yes
- [ ] No

<!-- If this does introduce a breaking change, please describe the
impact and migration path for existing applications below. -->

## Other information

<!-- Any other information that is important to this PR, such as
screenshots of how the component looks before and after the change. -->
  • Loading branch information
Uniblake authored Feb 21, 2024
1 parent e14c6b0 commit 9e3565d
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/math/src/i257.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ fn i257_div_rem(lhs: i257, rhs: i257) -> (i257, i257) {
}

// Implements the PartialEq trait for i257.
// WARNING: If either `lhs` or `rhs` is negative zero, functions will revert.
// Ensure that neither `lhs` nor `rhs` is negative zero before calling.
impl i257PartialEq of PartialEq<i257> {
fn eq(lhs: @i257, rhs: @i257) -> bool {
i257_assert_no_negative_zero(*lhs);
i257_assert_no_negative_zero(*rhs);
lhs.is_negative == rhs.is_negative && lhs.abs == rhs.abs
}

Expand All @@ -206,6 +210,8 @@ impl i257PartialEq of PartialEq<i257> {
}

// Implements the PartialOrd trait for i257.
// WARNING: If either `lhs` or `rhs` is negative zero, functions will revert.
// Ensure that neither `lhs` nor `rhs` is negative zero before calling.
impl i257PartialOrd of PartialOrd<i257> {
fn le(lhs: i257, rhs: i257) -> bool {
!i257PartialOrd::gt(lhs, rhs)
Expand All @@ -219,6 +225,8 @@ impl i257PartialOrd of PartialOrd<i257> {
}

fn gt(lhs: i257, rhs: i257) -> bool {
i257_assert_no_negative_zero(lhs);
i257_assert_no_negative_zero(rhs);
// Check if `lhs` is negative and `rhs` is positive.
if lhs.is_negative & !rhs.is_negative {
return false;
Expand Down Expand Up @@ -262,7 +270,7 @@ impl i257Zeroable of Zeroable<i257> {
// # Arguments
// * `x` - The i257 integer to check.
// # Panics
// Panics if `x` is zero and is not negative
// Panics if `x` is zero and is negative
fn i257_assert_no_negative_zero(x: i257) {
if x.abs == 0 {
assert(!x.is_negative, 'negative zero');
Expand Down

0 comments on commit 9e3565d

Please sign in to comment.