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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use std::fmt::Display;

use num::Float;

/// Determine if two decimals are equal with the specified precision.
///
/// 2つの小数が指定した精度で等しいかどうかを判定する。
///
/// # Examples
///
/// ```
/// use close_to::close_to;
///
/// let (is_close, expected_diff, received_diff) = close_to(1.0, 1.0001, 3);
/// assert!(is_close);
///```
/// ```should_panic
/// use close_to::close_to;
///
/// let (is_close, expected_diff, received_diff) = close_to(1.0, 1.0001, 4);
/// assert!(is_close);
/// ```
/// ```
pub fn close_to<T: Float, U: Into<T>>(left: T, right: U, precision: i32) -> (bool, T, T)
{
    let expected_diff = T::from(10).unwrap().powi(-precision) / T::from(2).unwrap();

    let received_diff = (left - right.into()).abs();

    (received_diff <= expected_diff, expected_diff, received_diff)
}

/// Ensures that two decimals are equal with the specified precision.
/// In case of panic, the function outputs the value of the decimal along with a debug expression.
///
/// 2つの小数が指定した精度で等しいことを保証する。
/// パニックになると、この関数は小数の値をデバッグ表現とともに出力する。
///
/// # Examples
///
/// ```
/// use close_to::assert_close_to;
///
/// assert_close_to(1.0, 1.0001, 3);
/// ```
/// ```should_panic
/// use close_to::assert_close_to;
///
/// assert_close_to(1.0, 1.0001, 4); // panic with the following message
///
/// // assertion 'left ≈ right` failed
/// // left:  1
/// // right: 1.0001
/// // received_diff: 0.00009999999999998899
/// // expected_diff: 0.00005
/// ```
/// ```
pub fn assert_close_to<T: Float + Display + Copy, U: Display + Into<T> + Copy>(left: T, right: U, precision: i32)
{
    let (is_close, expected_diff, received_diff) = close_to(left, right, precision);

    if !is_close
    {
        panic!("assertion 'left ≈ right` failed\n left:  {}\nright: {}\nreceived_diff: {}\nexpected_diff: {}", left, right, received_diff, expected_diff);
    }
}

/// Determine if two decimals are not equal with the specified precision.
///
/// 2つの小数が指定した精度で等しくないかどうかを判定する。
///
/// # Examples
///
/// ```
/// use close_to::far_from;
///
/// let (is_far, expected_diff, received_diff) = far_from(1.0, 1.0001, 4);
/// assert!(is_far);
/// ```
/// ```should_panic
/// use close_to::far_from;
///
/// let (is_far, expected_diff, received_diff) = far_from(1.0, 1.0001, 3);
/// assert!(is_far);
/// ```
pub fn far_from<T: Float, U: Into<T>>(left: T, right: U, precision: i32) -> (bool, T, T)
{
    let (is_close, expected_diff, received_diff) = close_to(left, right, precision);

    (!is_close, expected_diff, received_diff)
}

/// Ensures that two decimals are not equal with the specified precision.
/// In case of panic, the function outputs the value of the decimal along with a debugging expression.
///
/// 2つの小数が指定した精度で等しくないことを保証する。
/// パニックになると、この関数は小数の値をデバッグ表現とともに出力する。
///
/// # Examples
///
/// ```
/// use close_to::assert_far_from;
///
/// assert_far_from(1.0, 1.0001, 4);
/// ```
/// ```should_panic
/// use close_to::assert_far_from;
///
/// assert_far_from(1.0, 1.0001, 3); // panic with the following message
///
/// // assertion 'left != right` failed
/// // left:  1
/// // right: 1.0001
/// // received_diff: 0.00009999999999998899
/// // expected_diff: 0.0005
/// ```
pub fn assert_far_from<T: Float + Display + Copy, U: Display + Into<T> + Copy>(left: T, right: U, precision: i32)
{
    let (is_far, expected_diff, received_diff) = far_from(left, right, precision);

    if !is_far
    {
        panic!("assertion 'left != right` failed\n left:  {}\nright: {}\nreceived_diff: {}\nexpected_diff: {}", left, right, received_diff, expected_diff);
    }
}

/// Trait to determine if two decimals are equal to a specified precision.
/// Implemented in float type by default.
///
/// 2つの小数が指定した精度で等しいかどうかを判定するトレイト。
/// デフォルトでfloat型に実装されている。
///
/// # Examples
///
/// ```
/// use close_to::CloseTo;
///
/// let (is_close, ..) = 1.0.close_to(1.0001, 3);
/// assert!(is_close);
///
/// assert!(1.0.far_from(1.0001, 4).0);
/// ```
pub trait CloseTo<T, U: Into<T>>
{
    /// Determine if two decimals are equal with the specified precision.
    ///
    /// 2つの小数が指定した精度で等しいかどうかを判定する。
    fn close_to(&self, other: U, precision: i32) -> (bool, T, T);

    /// Determine if two decimals are not equal with the specified precision.
    ///
    /// 2つの小数が指定した精度で等しくないかどうかを判定する。
    fn far_from(&self, other: U, precision: i32) -> (bool, T, T);
}

/// A trace that guarantees that two decimals are equal to a specified precision.
/// Implemented in float type by default.
///
/// 2つの小数が指定した精度で等しいことを保証するトレイト。
/// デフォルトでfloat型に実装されている。
///
/// # Examples
///
/// ```
/// use close_to::{assert_far_from, AssertCloseTo};
///
/// 1.0.assert_close_to(1.0001, 3);
/// 1.0.assert_far_from(1.0001, 4);
/// ```
pub trait AssertCloseTo<T>
{
    /// Ensures that two decimals are equal with the specified precision.
    ///
    /// 2つの小数が指定した精度で等しいことを保証する。
    fn assert_close_to(&self, other: T, precision: i32);
    /// Ensures that no two decimals are equal at the specified precision.
    ///
    /// 2つの小数が指定した精度で等しくないことを保証する。
    fn assert_far_from(&self, other: T, precision: i32);
}


impl<T: Float, U: Into<T>> CloseTo<T, U> for T
{
    fn close_to(&self, other: U, precision: i32) -> (bool, T, T)
    {
        close_to(*self, other, precision)
    }

    fn far_from(&self, other: U, precision: i32) -> (bool, T, T)
    {
        far_from(*self, other, precision)
    }
}

impl<T: Float + Display, U: Into<T> + Display + Copy> AssertCloseTo<U> for T
{
    fn assert_close_to(&self, other: U, precision: i32)
    {
        assert_close_to(*self, other, precision)
    }

    fn assert_far_from(&self, other: U, precision: i32)
    {
        assert_far_from(*self, other, precision)
    }
}

#[cfg(test)]
mod test
{
    use super::*;

    #[test]
    fn test_close_to()
    {
        let (is_close, ..) = close_to(1.0, 1.0001, 3);
        assert!(is_close);

        let (is_close, ..) = close_to(1.0, 1.0001, 4);
        assert!(!is_close);
    }

    #[test]
    fn test_assert_close_to()
    {
        assert_close_to(1.0, 1.0001, 3);
        assert_far_from(1.0, 1.0001, 4);
    }

    #[test]
    #[should_panic]
    fn test_assert_close_to_panic()
    {
        assert_close_to(1.0, 1.0001, 4);
    }

    #[test]
    #[should_panic]
    fn test_assert_far_from_panic()
    {
        assert_far_from(1.0, 1.0001, 3);
    }

    #[test]
    fn test_trait()
    {
        let (is_close, ..) = 1.0.close_to(1.0001, 3);
        assert!(is_close);

        let (is_close, ..) = 1.0.close_to(1.0001, 4);
        assert!(!is_close);
    }

    #[test]
    fn test_trait_assert()
    {
        1.0.assert_close_to(1.0001, 3);
        1.0.assert_far_from(1.0001, 4);
    }

    #[test]
    #[should_panic]
    fn test_trait_assert_panic()
    {
        1.0.assert_close_to(1.0001, 4);
    }

    #[test]
    #[should_panic]
    fn test_trait_assert_far_from_panic()
    {
        1.0.assert_far_from(1.0001, 3);
    }
}