Skip to content

Commit

Permalink
Fixing i257 is_zero() (#282)
Browse files Browse the repository at this point in the history
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):

Fixing is_zero behaviour that was causing is_zero to sometimes return
false when it was indeed zero
  • Loading branch information
gaetbout authored Mar 7, 2024
1 parent 2fbd825 commit 946e6e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/math/src/i257.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::zeroable::Zeroable;
// ====================== INT 257 ======================

// i257 represents a 129-bit integer.
Expand Down Expand Up @@ -259,10 +260,11 @@ impl i257Zeroable of Zeroable<i257> {
i257_new(0, false)
}
fn is_zero(self: i257) -> bool {
self == Zeroable::zero()
assert(!self.is_negative, 'no negative zero');
self.abs == 0
}
fn is_non_zero(self: i257) -> bool {
self != Zeroable::zero()
!self.is_zero()
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/math/src/tests/i257_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ fn i257_test_mul() {
assert!(!result.is_negative, "10 * 0 -> positive");
}

#[test]
fn i257_test_is_zero() {
let a = i257 { abs: 0, is_negative: false };
assert!(a.is_zero(), "should be true");
}

#[test]
#[should_panic(expected: ('no negative zero',))]
fn i257_test_is_zero_panic() {
let a = i257 { abs: 0, is_negative: true };
let _x = a.is_zero();
}

#[test]
fn i257_test_div_no_rem() {
// Test division of positive integers
Expand Down

0 comments on commit 946e6e2

Please sign in to comment.