Added PSI unit

This commit is contained in:
Zachary Sunforge
2025-01-15 15:49:41 -08:00
parent 812678b34b
commit 41b767d8b5
2 changed files with 30 additions and 3 deletions

View File

@ -16,7 +16,7 @@ members = [
] ]
[workspace.package] [workspace.package]
version = "0.4.4" version = "0.4.5"
edition = "2021" edition = "2021"
repository = "https://git.bfpower.io/BFPOWER/physical" repository = "https://git.bfpower.io/BFPOWER/physical"
readme = "README.md" readme = "README.md"

View File

@ -2,6 +2,7 @@ use crate::quantity::{KILO, Quantity, Value};
use generate_quantity::quantity_type; use generate_quantity::quantity_type;
const PASCALS_PER_TORR: f64 = 101325.0 / 760.0; const PASCALS_PER_TORR: f64 = 101325.0 / 760.0;
const KILO_PASCALS_PER_PSI: f64 = 6.894757293168364;
//----- Pascals ---------------------------------- //----- Pascals ----------------------------------
quantity_type! {Pascals, "Pa"} quantity_type! {Pascals, "Pa"}
@ -30,6 +31,12 @@ impl<V: Value> KiloPascals<V> {
let multiplier = V::from_u16(KILO).unwrap(); let multiplier = V::from_u16(KILO).unwrap();
Pascals(self.0 * multiplier) Pascals(self.0 * multiplier)
} }
#[inline]
pub fn to_psi(self) -> Psi<V> {
let divisor = V::from_f64(KILO_PASCALS_PER_PSI).unwrap();
Psi(self.0 / divisor)
}
} }
//----- Torr ---------------------------------- //----- Torr ----------------------------------
@ -44,6 +51,17 @@ impl<V: Value> Torr<V> {
} }
} }
//----- PSI ----------------------------------
quantity_type! {Psi, "PSI"}
impl<V: Value> Psi<V> {
#[inline]
pub fn to_kilo_pascals(self) -> KiloPascals<V> {
let multiplier = V::from_f64(KILO_PASCALS_PER_PSI).unwrap();
KiloPascals(self.0 * multiplier)
}
}
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
// ----- Tests ------------------------ // ----- Tests ------------------------
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
@ -53,7 +71,7 @@ mod tests {
use float_cmp::assert_approx_eq; use float_cmp::assert_approx_eq;
#[test] #[test]
fn convert_u32() { fn pascals_kilo_pascals() {
let pascals: Pascals<u32> = 1_000.pascals(); let pascals: Pascals<u32> = 1_000.pascals();
let kilo_pascals: KiloPascals<u32> = 1.kilo_pascals(); let kilo_pascals: KiloPascals<u32> = 1.kilo_pascals();
@ -62,11 +80,20 @@ mod tests {
} }
#[test] #[test]
fn torr_pascal() { fn torr_pascals() {
let torr: Torr<f32> = 7.5.torr(); let torr: Torr<f32> = 7.5.torr();
let pascals: Pascals<f32> = 999.9177631578947.pascals(); let pascals: Pascals<f32> = 999.9177631578947.pascals();
assert_approx_eq!(f32, pascals.to_torr().0, torr.0); assert_approx_eq!(f32, pascals.to_torr().0, torr.0);
assert_approx_eq!(f32, torr.to_pascals().0, pascals.0); assert_approx_eq!(f32, torr.to_pascals().0, pascals.0);
} }
#[test]
fn psi_kilo_pascals() {
let psi: Psi<f32> = 2.5.psi();
let kilo_pascals: KiloPascals<f32> = 17.23689323292091.kilo_pascals();
assert_approx_eq!(f32, psi.to_kilo_pascals().0, kilo_pascals.0);
assert_approx_eq!(f32, kilo_pascals.to_psi().0, psi.0);
}
} }