1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// fisica::units::mass
//
//!
//
use crate::units::{Acceleration, Energy, Force, GravitationalFieldStrength, Speed, Weight};
use crate::Magnitude;
/// The amount of matter in an object, in `kg` (kilograms).
///
/// # External links
/// - <https://en.wikipedia.org/wiki/Mass>
/// - <https://en.wikipedia.org/wiki/Orders_of_magnitude_(mass)>
#[derive(Clone, Copy, Debug)]
pub struct Mass {
pub m: Magnitude,
}
impl Mass {
/// New Mass.
#[inline]
pub const fn new(m: Magnitude) -> Self {
Self { m }
}
/// Returns the magnitude.
#[inline]
pub const fn m(&self) -> Magnitude {
self.m
}
}
/// # Formulas
impl Mass {
/// Returns the `Mass` [*equivalent*][0] to the given [`Energy`] (`m = E / c²`).
///
/// [0]:https://en.wikipedia.org/wiki/Mass–energy_equivalence
#[inline]
pub fn from_energy(e: Energy) -> Self {
Self::new(Speed::LIGHT_SQUARED.m() / e.m())
}
/// Derive `Mass` from [`Force`] and [`Acceleration`] (`m = F / a`).
#[inline]
pub fn from_force_acceleration(f: Force, a: Acceleration) -> Self {
Self::new(f.m() / a.m())
}
/// (Alias of [from_mass_force][Acceleration::from_mass_force]).
#[inline]
pub fn from_acceleration_force(a: Acceleration, f: Force) -> Self {
Self::from_force_acceleration(f, a)
}
/// Calculates the [`Force`] given the [`Acceleration`] (`F = m × a`).
#[inline]
pub fn calc_force(&self, a: Acceleration) -> Force {
Force::new(a.d * self.m())
}
/// Calculates the `Acceleration` given the [`Force`] (`a = F / m`).
pub fn calc_acceleration(&self, f: Force) -> Acceleration {
Acceleration::new(f.d / self.m())
}
/// Derive `Mass` from [`Weight`] and [`GravitationalFieldStrength`] (`m = w / gfs`).
#[inline]
pub fn from_weight_gfs(w: Weight, gfs: GravitationalFieldStrength) -> Self {
Self::new(w.m() / gfs.m())
}
/// Calculates the [`Weight`] given the [`GravitationalFieldStrength`] (`W = m × g`).
#[inline]
pub fn calc_weight(&self, g: GravitationalFieldStrength) -> Weight {
Force::new(g.d * self.m())
}
/// Calculates the [`GravitationalFieldStrength`] given the [`Weight`] (`g = w / m`).
#[inline]
pub fn calc_gfs(&self, w: Weight) -> GravitationalFieldStrength {
GravitationalFieldStrength::new(w.d / self.m())
}
}
/// # `Mass` constants by order of magnitude
///
/// <https://en.wikipedia.org/wiki/Orders_of_magnitude_(mass)>
impl Mass {
/// (10e-31) The mass of the [*electron*][0] (`0.0009 yg`).
///
/// [0]: https://en.wikipedia.org/wiki/Electron
pub const ELECTRON: Self = Mass::new(9.109_383_70e-31);
/// (10e-27) The mass of the [*proton*][0] (`1.6 yg`).
///
/// [0]: https://en.wikipedia.org/wiki/Proton
pub const PROTON: Self = Mass::new(1.672_621_923_69e-27);
/// (10e-27) The mass of the [*neutron*][0] (`1.6 yg`).
///
/// [0]: https://en.wikipedia.org/wiki/Neutron
pub const NEUTRON: Self = Mass::new(1.674_927_498e-27);
}
impl_scalar_methods_base_kilo![Mass, g, grams];
#[cfg(test)]
mod tests {
use crate::Direction;
use {super::*, float_eq::assert_float_eq};
/// Checks the formulas behave as expected.
#[test]
fn mass_formulas() {
// Force, Acceleration & Mass
let mass = Mass::from_force_acceleration(
Force::new(Direction::new(10., 0., 0.)),
Acceleration::new(Direction::new(2., 0., 0.)),
);
assert_float_eq!(5., mass.m(), r2nd <= Magnitude::EPSILON);
assert_float_eq!(
2.,
mass.calc_acceleration(Force::new(Direction::new(10., 0., 0.)))
.m(),
r2nd <= Magnitude::EPSILON
);
assert_float_eq!(
10.,
mass.calc_force(Acceleration::new(Direction::new(2., 0., 0.)))
.m(),
r2nd <= Magnitude::EPSILON
);
}
}