Skip to content

Commit

Permalink
Merge pull request #31 from ben-jay-amin/zero-one-trait
Browse files Browse the repository at this point in the history
Added zeroable and oneable traits issue #26
  • Loading branch information
clexmond authored Apr 5, 2024
2 parents ecbe5ed + 48db363 commit adf7008
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
35 changes: 33 additions & 2 deletions src/f128/types/fixed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ trait FixedTrait {

impl FixedImpl of FixedTrait {
fn ZERO() -> Fixed {
return Fixed { mag: 0, sign: false };
return core::num::traits::Zero::zero();
}

fn ONE() -> Fixed {
return Fixed { mag: ONE_u128, sign: false };
return core::num::traits::One::one();
}

fn new(mag: u128, sign: bool) -> Fixed {
Expand Down Expand Up @@ -529,6 +529,37 @@ impl PackFixed of StorePacking<Fixed, felt252> {
}
}


impl FixedZero of core::num::traits::Zero<Fixed> {
fn zero() -> Fixed {
Fixed { mag: 0, sign: false }
}
#[inline(always)]
fn is_zero(self: @Fixed) -> bool {
*self.mag == 0
}
#[inline(always)]
fn is_non_zero(self: @Fixed) -> bool {
!self.is_zero()
}
}

// One trait implementations
impl FixedOne of core::num::traits::One<Fixed> {
fn one() -> Fixed {
Fixed { mag: ONE_u128, sign: false }
}
#[inline(always)]
fn is_one(self: @Fixed) -> bool {
*self == FixedOne::one()
}
#[inline(always)]
fn is_non_one(self: @Fixed) -> bool {
!self.is_one()
}
}


// Tests --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
Expand Down
32 changes: 30 additions & 2 deletions src/f64/types/fixed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ trait FixedTrait {

impl FixedImpl of FixedTrait {
fn ZERO() -> Fixed {
return Fixed { mag: 0, sign: false };
return core::num::traits::Zero::zero();
}

fn ONE() -> Fixed {
return Fixed { mag: ONE, sign: false };
return core::num::traits::One::one();
}

fn new(mag: u64, sign: bool) -> Fixed {
Expand Down Expand Up @@ -527,6 +527,34 @@ impl PackFixed of StorePacking<Fixed, felt252> {
}
}

impl FixedZero of core::num::traits::Zero<Fixed> {
fn zero() -> Fixed {
Fixed { mag: 0, sign: false }
}
#[inline(always)]
fn is_zero(self: @Fixed) -> bool {
*self.mag == 0
}
#[inline(always)]
fn is_non_zero(self: @Fixed) -> bool {
!self.is_zero()
}
}

// One trait implementations
impl FixedOne of core::num::traits::One<Fixed> {
fn one() -> Fixed {
Fixed { mag: ONE, sign: false }
}
#[inline(always)]
fn is_one(self: @Fixed) -> bool {
*self == FixedOne::one()
}
#[inline(always)]
fn is_non_one(self: @Fixed) -> bool {
!self.is_one()
}
}

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit adf7008

Please sign in to comment.