logo
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Conversion between unit systems
//!
//! Wherever it makes sense, we implement conversion between the unit systems that ship with
//! dimensioned using `core::convert::From`.
//!
//! Note that it does not always make sense to do so. For example, while one can convert from a
//! subset of the `SI` system to `CGS`, it makes no sense to convert from `CGS` to `SI`.
//!
//! If are interested in implementing conversion for your own unit system, [here is an
//! example](https://github.com/paholg/dimensioned-examples/blob/master/src/conversion.md)
//! demonstrating how to do so.
//!
//! Conversions between unit systems are implemented as follows:
//!
//! * `SI` to `UCUM`: As `UCUM` does not have a unit for amount of substance, this is defined only
//! for `SI` units that don't contain `Mole`s. In addition, as `UCUM` defines radians as a unit, it
//! is not correct to perform this conversion if the quantity expresses an angle.
//!
//! * `SI` to `CGS` and `MKS`: These conversions are only defined for `SI` units that are a
//! combination of `Meter`, `Kilogram`, `Second`, and `Ampere`.
//!
//! * `UCUM` to `SI`: As `SI` does not have a unit for angle, this is only defined for `UCUM`
//! units that don't contain `Radian`s.
//!
//! * `CGS` to `MKS`
//! * `MKS` to `CGS`

mod ucum_to_si {
    // From UCUM
    use crate::f64prefixes::*;
    use crate::si::SI;
    use crate::ucum;
    use core::convert::From;
    use core::ops::{Add, Mul};
    use num_traits::float::FloatCore;
    use typenum::{Integer, Prod, Sum, Z0};

    impl<V, Meter, Second, Gram, Kelvin, Coulomb, Candela>
        From<ucum::UCUM<V, tarr![Meter, Second, Gram, Z0, Kelvin, Coulomb, Candela]>>
        for SI<Prod<V, f64>, tarr![Meter, Gram, Sum<Second, Coulomb>, Coulomb, Kelvin, Candela, Z0]>
    where
        Meter: Integer,
        Second: Integer + Add<Coulomb>,
        Gram: Integer,
        Kelvin: Integer,
        Coulomb: Integer,
        Candela: Integer,
        V: Mul<f64>,
    {
        fn from(
            other: ucum::UCUM<V, tarr![Meter, Second, Gram, Z0, Kelvin, Coulomb, Candela]>,
        ) -> Self {
            let gfac = FloatCore::powi(MILLI, Gram::to_i32());

            let fac = gfac;

            SI::new(other.value_unsafe * fac)
        }
    }
}

mod si_to_ucum {
    // From SI
    use crate::f64prefixes::*;
    use crate::si;
    use crate::ucum::UCUM;
    use core::convert::From;
    use core::ops::{Mul, Sub};
    use num_traits::float::FloatCore;
    use typenum::{Diff, Integer, Prod, Z0};

    impl<V, Meter, Kilogram, Second, Ampere, Kelvin, Candela>
        From<si::SI<V, tarr![Meter, Kilogram, Second, Ampere, Kelvin, Candela, Z0]>>
        for UCUM<
            Prod<V, f64>,
            tarr![Meter, Diff<Second, Ampere>, Kilogram, Z0, Kelvin, Ampere, Candela],
        >
    where
        Meter: Integer,
        Kilogram: Integer,
        Second: Integer + Sub<Ampere>,
        Ampere: Integer,
        Kelvin: Integer,
        Candela: Integer,
        V: Mul<f64>,
    {
        fn from(
            other: si::SI<V, tarr![Meter, Kilogram, Second, Ampere, Kelvin, Candela, Z0]>,
        ) -> Self {
            let kgfac = FloatCore::powi(KILO, Kilogram::to_i32());

            let fac = kgfac;

            UCUM::new(other.value_unsafe * fac)
        }
    }
}

#[cfg(any(feature = "std", feature = "nightly"))]
mod mks_to_cgs {
    use crate::cgs::CGS;

    // From MKS
    use crate::f64prefixes::*;
    use crate::mks;
    use crate::traits::Sqrt;
    use core::convert::From;
    use core::ops::Mul;
    use num_traits::float::FloatCore;
    use typenum::{Integer, Prod};

    impl<V, SqrtMeter, SqrtKilogram, Second>
        From<mks::MKS<V, tarr![SqrtMeter, SqrtKilogram, Second]>>
        for CGS<Prod<V, f64>, tarr![SqrtMeter, SqrtKilogram, Second]>
    where
        SqrtMeter: Integer,
        SqrtKilogram: Integer,
        Second: Integer,
        V: Mul<f64>,
    {
        fn from(other: mks::MKS<V, tarr![SqrtMeter, SqrtKilogram, Second]>) -> Self {
            let mfac = match SqrtMeter::to_i32() {
                e if e % 2 == 0 => FloatCore::powi(HECTO, e / 2),
                e => FloatCore::powi(Sqrt::sqrt(HECTO), e),
            };
            let kgfac = match SqrtKilogram::to_i32() {
                e if e % 2 == 0 => FloatCore::powi(KILO, e / 2),
                e => FloatCore::powi(Sqrt::sqrt(KILO), e),
            };

            let fac = mfac * kgfac;

            CGS::new(other.value_unsafe * fac)
        }
    }
}

#[cfg(any(feature = "std", feature = "nightly"))]
mod si_to_cgs {
    use crate::cgs::CGS;

    use crate::mks;
    use core::convert::From;
    use core::ops::{Add, Mul};
    use typenum::{Integer, Prod, Sum};

    // From SI
    use crate::si;
    use typenum::{P2, P3, Z0};

    impl<V, Meter, Kilogram, Second, Ampere>
        From<si::SI<V, tarr![Meter, Kilogram, Second, Ampere, Z0, Z0, Z0]>>
        for CGS<
            Prod<Prod<V, f64>, f64>,
            tarr![
                Sum<Prod<Meter, P2>, Prod<Ampere, P3>>,
                Sum<Prod<Kilogram, P2>, Ampere>,
                Sum<Second, Prod<Ampere, P2>>
            ],
        >
    where
        V: Mul<f64>,
        Meter: Integer + Mul<P2>,
        Kilogram: Integer + Mul<P2>,
        Second: Integer + Add<Prod<Ampere, P2>>,
        Ampere: Integer + Mul<P2> + Mul<P3>,
        Prod<Meter, P2>: Add<Prod<Ampere, P3>>,
        Prod<Kilogram, P2>: Add<Ampere>,
        Sum<Prod<Meter, P2>, Prod<Ampere, P3>>: Integer,
        Sum<Prod<Kilogram, P2>, Ampere>: Integer,
        Sum<Second, Prod<Ampere, P2>>: Integer,
        Prod<V, f64>: Mul<f64>,
    {
        fn from(other: si::SI<V, tarr![Meter, Kilogram, Second, Ampere, Z0, Z0, Z0]>) -> Self {
            CGS::from(mks::MKS::from(other))
        }
    }
}

#[cfg(any(feature = "std", feature = "nightly"))]
mod cgs_to_mks {
    use crate::f64prefixes::*;
    use crate::mks::MKS;
    use crate::traits::Sqrt;
    use core::convert::From;
    use core::ops::Mul;
    use num_traits::float::FloatCore;
    use typenum::{Integer, Prod};

    // From CGS
    use crate::cgs;

    impl<V, SqrtCentimeter, SqrtGram, Second>
        From<cgs::CGS<V, tarr![SqrtCentimeter, SqrtGram, Second]>>
        for MKS<Prod<V, f64>, tarr![SqrtCentimeter, SqrtGram, Second]>
    where
        SqrtCentimeter: Integer,
        SqrtGram: Integer,
        Second: Integer,
        V: Mul<f64>,
    {
        fn from(other: cgs::CGS<V, tarr![SqrtCentimeter, SqrtGram, Second]>) -> Self {
            let cmfac = match SqrtCentimeter::to_i32() {
                e if e % 2 == 0 => FloatCore::powi(CENTI, e / 2),
                e => FloatCore::powi(Sqrt::sqrt(CENTI), e),
            };
            let gfac = match SqrtGram::to_i32() {
                e if e % 2 == 0 => FloatCore::powi(MILLI, e / 2),
                e => FloatCore::powi(Sqrt::sqrt(MILLI), e),
            };

            let fac = cmfac * gfac;

            MKS::new(other.value_unsafe * fac)
        }
    }
}

mod si_to_mks {
    use crate::mks::MKS;
    use core::convert::From;
    use core::ops::{Add, Mul};
    use typenum::{Integer, Prod, Sum};

    // From SI
    use crate::si;
    use typenum::{P2, P3, Z0};

    impl<V, Meter, Kilogram, Second, Ampere>
        From<si::SI<V, tarr![Meter, Kilogram, Second, Ampere, Z0, Z0, Z0]>>
        for MKS<
            Prod<V, f64>,
            tarr![
                Sum<Prod<Meter, P2>, Prod<Ampere, P3>>,
                Sum<Prod<Kilogram, P2>, Ampere>,
                Sum<Second, Prod<Ampere, P2>>
            ],
        >
    where
        V: Mul<f64>,
        Meter: Integer + Mul<P2>,
        Kilogram: Integer + Mul<P2>,
        Second: Integer + Add<Prod<Ampere, P2>>,
        Ampere: Integer + Mul<P2> + Mul<P3>,
        Prod<Meter, P2>: Add<Prod<Ampere, P3>>,
        Prod<Kilogram, P2>: Add<Ampere>,
    {
        fn from(other: si::SI<V, tarr![Meter, Kilogram, Second, Ampere, Z0, Z0, Z0]>) -> Self {
            MKS::new(other.value_unsafe * 1.0)
        }
    }
}