Last active
May 14, 2022 21:13
-
-
Save eventhelix/2cf16135c352f4e97ff4ac0c8ac0c5d2 to your computer and use it in GitHub Desktop.
Complex numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::rc::Rc; | |
use std::sync::Arc; | |
pub struct Complex { | |
real: f64, | |
imaginary: f64, | |
} | |
impl Complex { | |
pub fn magnitude_self_ownership(self) -> f64 { | |
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt() | |
} | |
pub fn magnitude_self_reference(&self) -> f64 { | |
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt() | |
} | |
// Passing smart pointers | |
pub fn magnitude_self_box(self: Box<Self>) -> f64 { | |
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt() | |
} | |
pub fn magnitude_self_rc(self: Rc<Self>) -> f64 { | |
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt() | |
} | |
pub fn magnitude_self_arc(self: Arc<Self>) -> f64 { | |
(self.real.powf(2.0) + self.imaginary.powf(2.0)).sqrt() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment