Skip to content

Commit

Permalink
Merge pull request #1087 from hannobraun/scalar
Browse files Browse the repository at this point in the history
Expand `Scalar` API
  • Loading branch information
hannobraun authored Sep 15, 2022
2 parents 0bfb906 + 8b267ac commit bc1e83f
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions crates/fj-math/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ impl Scalar {
self.0.max(other.into().0).into()
}

/// Compute the largest integer smaller than or equal to this scalar
pub fn floor(self) -> Self {
self.0.floor().into()
}

/// Compute the smallest integer larger than or equal to this scalar
pub fn ceil(self) -> Self {
self.0.ceil().into()
Expand Down Expand Up @@ -180,66 +185,78 @@ impl ops::Neg for Scalar {
}
}

impl ops::Add<Self> for Scalar {
impl<T: Into<Scalar>> ops::Add<T> for Scalar {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
self.0.add(rhs.0).into()
fn add(self, rhs: T) -> Self::Output {
self.0.add(rhs.into().0).into()
}
}

impl ops::AddAssign<Self> for Scalar {
fn add_assign(&mut self, rhs: Self) {
self.0.add_assign(rhs.0);
*self = Self::from_f64(self.0);
impl<T: Into<Scalar>> ops::Sub<T> for Scalar {
type Output = Self;

fn sub(self, rhs: T) -> Self::Output {
self.0.sub(rhs.into().0).into()
}
}

impl ops::Sub<Self> for Scalar {
impl<T: Into<Scalar>> ops::Mul<T> for Scalar {
type Output = Self;

fn sub(self, rhs: Self) -> Self::Output {
self.0.sub(rhs.0).into()
fn mul(self, rhs: T) -> Self::Output {
self.0.mul(rhs.into().0).into()
}
}

impl ops::Mul<Self> for Scalar {
impl<T: Into<Scalar>> ops::Div<T> for Scalar {
type Output = Self;

fn mul(self, rhs: Self) -> Self::Output {
self.0.mul(rhs.0).into()
fn div(self, rhs: T) -> Self::Output {
self.0.div(rhs.into().0).into()
}
}

impl ops::Mul<f64> for Scalar {
impl<T: Into<Scalar>> ops::Rem<T> for Scalar {
type Output = Self;

fn mul(self, rhs: f64) -> Self::Output {
self.0.mul(rhs).into()
fn rem(self, rhs: T) -> Self::Output {
self.0.rem(rhs.into().0).into()
}
}

impl ops::Div<Self> for Scalar {
type Output = Self;

fn div(self, rhs: Self) -> Self::Output {
self.0.div(rhs.0).into()
impl<T: Into<Scalar>> ops::AddAssign<T> for Scalar {
fn add_assign(&mut self, rhs: T) {
self.0.add_assign(rhs.into().0);
*self = self.0.into();
}
}

impl ops::Div<f64> for Scalar {
type Output = Self;
impl<T: Into<Scalar>> ops::SubAssign<T> for Scalar {
fn sub_assign(&mut self, rhs: T) {
self.0.sub_assign(rhs.into().0);
*self = self.0.into();
}
}

fn div(self, rhs: f64) -> Self::Output {
self.0.div(rhs).into()
impl<T: Into<Scalar>> ops::MulAssign<T> for Scalar {
fn mul_assign(&mut self, rhs: T) {
self.0.mul_assign(rhs.into().0);
*self = self.0.into();
}
}

impl ops::Rem<Self> for Scalar {
type Output = Self;
impl<T: Into<Scalar>> ops::DivAssign<T> for Scalar {
fn div_assign(&mut self, rhs: T) {
self.0.div_assign(rhs.into().0);
*self = self.0.into();
}
}

fn rem(self, rhs: Self) -> Self::Output {
self.0.rem(rhs.0).into()
impl<T: Into<Scalar>> ops::RemAssign<T> for Scalar {
fn rem_assign(&mut self, rhs: T) {
self.0.rem_assign(rhs.into().0);
*self = self.0.into();
}
}

Expand Down

0 comments on commit bc1e83f

Please sign in to comment.