dims/us/
area.rs

1//! Units of area that aren't necessarily used in surveying.
2//! The ACRE is re-exported from surveying
3
4// Re-export ACRE (is commonly used outside of surveying)
5pub use super::area_survey::ACRE;
6use crate::systems::AreaSystem;
7
8pub type AreaUnit<'t> = super::UnitType<'t, AreaSystem>;
9
10pub const SQUARE_INCH: AreaUnit = AreaUnit {
11    offset: 0.0,
12    ratio: SQUARE_FOOT.ratio / 144.0,
13    #[cfg(feature = "str")]
14    abbr: "in²",
15    #[cfg(feature = "str")]
16    singular: "square inch",
17    #[cfg(feature = "str")]
18    plural: "square inches",
19};
20
21pub const SQUARE_FOOT: AreaUnit = AreaUnit {
22    offset: 0.0,
23    ratio: 0.09290304,
24    #[cfg(feature = "str")]
25    abbr: "ft²",
26    #[cfg(feature = "str")]
27    singular: "square foot",
28    #[cfg(feature = "str")]
29    plural: "square feet",
30};
31
32pub const SQUARE_YARD: AreaUnit = AreaUnit {
33    offset: 0.0,
34    ratio: SQUARE_FOOT.ratio * 9.0,
35    #[cfg(feature = "str")]
36    abbr: "yd²",
37    #[cfg(feature = "str")]
38    singular: "square yard",
39    #[cfg(feature = "str")]
40    plural: "square yards",
41};
42
43pub const SQUARE_MILE: AreaUnit = AreaUnit {
44    offset: 0.0,
45    ratio: 2589988.110336,
46    #[cfg(feature = "str")]
47    abbr: "mi²",
48    #[cfg(feature = "str")]
49    singular: "square mile",
50    #[cfg(feature = "str")]
51    plural: "square miles",
52};
53#[cfg(test)]
54mod test {
55    use super::*;
56    use dims_core::unit_creation::*;
57    #[test]
58    fn test_area() {
59        assert_eq!(SQUARE_INCH.from(1.0).as_base(), 0.0254_f32.powi(2).into());
60        assert_eq!(SQUARE_INCH.from(432.0), SQUARE_FOOT.from(3.0));
61        assert_eq!(SQUARE_FOOT.from(18.0), SQUARE_YARD.from(2.0));
62        assert_eq!(SQUARE_YARD.from(6_195_200.0), SQUARE_MILE.from(2.0));
63    }
64}