Skip to content

Commit

Permalink
electronics: Add electrical impedance
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Aug 13, 2023
1 parent 4b922c6 commit 449b755
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
44 changes: 44 additions & 0 deletions electronics/src/electrical_impedance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

//! Electrical impedance is the measure of the opposition that a
//! circuit presents to a current when a voltage is applied.
//!
//! Impedance extends the concept of resistance to alternating current (AC) circuits.
//! [Electrical impedance](https://en.wikipedia.org/wiki/Electrical_impedance)

#[must_use]
pub fn get_resistance(reactance: f64, impedance: f64) -> f64 {
impedance.mul_add(impedance, -reactance.powi(2)).sqrt()
}

#[must_use]
pub fn get_reactance(resistance: f64, impedance: f64) -> f64 {
impedance.mul_add(impedance, -resistance.powi(2)).sqrt()
}

#[must_use]
pub fn get_impedance(resistance: f64, reactance: f64) -> f64 {
resistance.hypot(reactance)
}

#[cfg(test)]
mod tests {
use super::{get_impedance, get_reactance, get_resistance};

#[test]
fn test_get_resistance() {
assert_eq!(get_resistance(4.0, 5.0), 3.0);
}

#[test]
fn test_get_reactance() {
assert_eq!(get_reactance(4.0, 5.0), 3.0);
}

#[test]
fn test_get_impedance() {
assert_eq!(get_impedance(3.0, 4.0), 5.0);
}
}
1 change: 1 addition & 0 deletions electronics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
//pub mod apparent_power;
pub mod builtin_voltage;
pub mod electric_power;
pub mod electrical_impedance;
pub mod ohms_law;

0 comments on commit 449b755

Please sign in to comment.