aboutsummaryrefslogtreecommitdiff
path: root/src/complex.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-01-17 23:59:43 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2024-01-17 23:59:43 +0100
commit3cd19a42e1cc66b09f4279625385e00292b8900d (patch)
tree90dcbb339888da4fab50a1ef78a1c6413dc2c6c1 /src/complex.rs
parent95ba627a9c5e4e6e1d79f7aaff1854eb831511b4 (diff)
resolve clippy warnings
Diffstat (limited to 'src/complex.rs')
-rw-r--r--src/complex.rs58
1 files changed, 27 insertions, 31 deletions
diff --git a/src/complex.rs b/src/complex.rs
index e498234..d120c29 100644
--- a/src/complex.rs
+++ b/src/complex.rs
@@ -67,12 +67,10 @@ impl std::fmt::Display for Complex {
} else {
write!(f, "{}i", self.imag)
}
+ } else if self.imag == 1.0 {
+ write!(f, "{} + i", self.real)
} else {
- if self.imag == 1.0 {
- write!(f, "{} + i", self.real)
- } else {
- write!(f, "{} + {}i", self.real, self.imag)
- }
+ write!(f, "{} + {}i", self.real, self.imag)
}
}
}
@@ -101,25 +99,23 @@ impl FromStr for Complex {
c.real = a.parse()?;
c.imag = b[..b.len() - 1].parse()?;
}
- } else {
- if s.contains('i') {
- if s.len() == 1 {
- c.imag = 1.0;
- } else {
- c.imag = s[..s.len() - 1].parse()?;
- }
+ } else if s.contains('i') {
+ if s.len() == 1 {
+ c.imag = 1.0;
} else {
- c.real = s.parse()?;
+ c.imag = s[..s.len() - 1].parse()?;
}
+ } else {
+ c.real = s.parse()?;
}
Ok(c)
}
}
-impl Into<Complex> for f64 {
- fn into(self) -> Complex {
- Complex::new(self, 0.0)
+impl From<f64> for Complex {
+ fn from(val: f64) -> Self {
+ Complex::new(val, 0.0)
}
}
@@ -127,7 +123,7 @@ impl std::ops::Add for &Complex {
type Output = Complex;
fn add(self, rhs: Self) -> Self::Output {
- Complex::new(self.real + rhs.real, self.imag + rhs.imag)
+ *self + *rhs
}
}
@@ -135,7 +131,7 @@ impl std::ops::Add for Complex {
type Output = Complex;
fn add(self, rhs: Self) -> Self::Output {
- &self + &rhs
+ Complex::new(self.real + rhs.real, self.imag + rhs.imag)
}
}
@@ -143,7 +139,7 @@ impl std::ops::Sub for &Complex {
type Output = Complex;
fn sub(self, rhs: Self) -> Self::Output {
- Complex::new(self.real - rhs.real, self.imag - rhs.imag)
+ *self - *rhs
}
}
@@ -151,7 +147,7 @@ impl std::ops::Sub for Complex {
type Output = Complex;
fn sub(self, rhs: Self) -> Self::Output {
- &self - &rhs
+ Complex::new(self.real - rhs.real, self.imag - rhs.imag)
}
}
@@ -159,10 +155,7 @@ impl std::ops::Mul for &Complex {
type Output = Complex;
fn mul(self, rhs: Self) -> Self::Output {
- Complex::new(
- self.real * rhs.real - self.imag * rhs.imag,
- self.real * rhs.imag + self.imag * rhs.real,
- )
+ *self * *rhs
}
}
@@ -170,7 +163,10 @@ impl std::ops::Mul for Complex {
type Output = Complex;
fn mul(self, rhs: Self) -> Self::Output {
- &self * &rhs
+ Complex::new(
+ self.real * rhs.real - self.imag * rhs.imag,
+ self.real * rhs.imag + self.imag * rhs.real,
+ )
}
}
@@ -178,11 +174,7 @@ impl std::ops::Div for &Complex {
type Output = Complex;
fn div(self, rhs: Self) -> Self::Output {
- let z = rhs.real * rhs.real + rhs.imag * rhs.imag;
- Complex::new(
- (self.real * rhs.real + self.imag * rhs.imag) / z,
- (self.imag * rhs.real - self.real * rhs.imag) / z,
- )
+ *self / *rhs
}
}
@@ -190,6 +182,10 @@ impl std::ops::Div for Complex {
type Output = Complex;
fn div(self, rhs: Self) -> Self::Output {
- &self / &rhs
+ let z = rhs.real * rhs.real + rhs.imag * rhs.imag;
+ Complex::new(
+ (self.real * rhs.real + self.imag * rhs.imag) / z,
+ (self.imag * rhs.real - self.real * rhs.imag) / z,
+ )
}
}