Skip to content

Commit 946e6e2

Browse files
authored
Fixing i257 is_zero() (#282)
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
1 parent 2fbd825 commit 946e6e2

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/math/src/i257.cairo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::zeroable::Zeroable;
12
// ====================== INT 257 ======================
23

34
// i257 represents a 129-bit integer.
@@ -259,10 +260,11 @@ impl i257Zeroable of Zeroable<i257> {
259260
i257_new(0, false)
260261
}
261262
fn is_zero(self: i257) -> bool {
262-
self == Zeroable::zero()
263+
assert(!self.is_negative, 'no negative zero');
264+
self.abs == 0
263265
}
264266
fn is_non_zero(self: i257) -> bool {
265-
self != Zeroable::zero()
267+
!self.is_zero()
266268
}
267269
}
268270

src/math/src/tests/i257_test.cairo

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ fn i257_test_mul() {
136136
assert!(!result.is_negative, "10 * 0 -> positive");
137137
}
138138

139+
#[test]
140+
fn i257_test_is_zero() {
141+
let a = i257 { abs: 0, is_negative: false };
142+
assert!(a.is_zero(), "should be true");
143+
}
144+
145+
#[test]
146+
#[should_panic(expected: ('no negative zero',))]
147+
fn i257_test_is_zero_panic() {
148+
let a = i257 { abs: 0, is_negative: true };
149+
let _x = a.is_zero();
150+
}
151+
139152
#[test]
140153
fn i257_test_div_no_rem() {
141154
// Test division of positive integers

0 commit comments

Comments
 (0)