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
pub fn round_2(val: f64) -> f64 {
(val * 100.0).round() / 100.0
}
pub fn round_4(val: f64) -> f64 {
(val * 10_000.0).round() / 10_000.0
}
pub fn round_6(val: f64) -> f64 {
(val * 1_000_000.0).round() / 1_000_000.0
}
pub fn round_8(val: f64) -> f64 {
(val * 100_000_000.0).round() / 100_000_000.0
}
#[inline(always)]
pub fn assert_rounded_2(val_1: f64, val_2: f64) {
assert!(val_1.is_finite());
assert!(val_2.is_finite());
assert_eq!(round_2(val_1), round_2(val_2));
}
#[inline(always)]
pub fn assert_rounded_4(val_1: f64, val_2: f64) {
assert!(val_1.is_finite());
assert!(val_2.is_finite());
assert_eq!(round_4(val_1), round_4(val_2));
}
#[inline(always)]
pub fn assert_rounded_6(val_1: f64, val_2: f64) {
assert!(val_1.is_finite());
assert!(val_2.is_finite());
assert_eq!(round_6(val_1), round_6(val_2));
}
#[inline(always)]
pub fn assert_rounded_8(val_1: f64, val_2: f64) {
assert!(val_1.is_finite());
assert!(val_2.is_finite());
assert_eq!(round_8(val_1), round_8(val_2));
}
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
#[test]
fn test_equals_zero() {
assert_eq!(0.0f64, 0.0f64);
assert_eq!(-0.0f64, -0.0f64);
assert_eq!(0.0f64, -0.0f64);
}
#[test]
fn test_round_2() {
assert_eq!(295_489.94, round_2(295_489.941849));
assert_eq!(295_489.94, round_2(295_489.9449));
assert_ne!(295_489.94, round_2(295_489.9451234));
}
#[test]
fn test_round_4() {
assert_eq!(295_489.9418, round_4(295_489.941849));
assert_eq!(295_489.9418, round_4(295_489.94175));
assert_ne!(295_489.9418, round_4(295_489.94185));
}
#[test]
fn test_round_6() {
assert_eq!(295_489.941849, round_6(295_489.9418494449));
assert_eq!(295_489.533367, round_6(295_489.5333669999999));
assert_eq!(295_489.945123, round_6(295_489.9451229999));
assert_ne!(295_489.945123, round_6(295_489.9451249999));
}
#[test]
fn test_round_8() {
assert_eq!(295_489.94184944, round_8(295_489.9418494449));
assert_eq!(295_489.53336700, round_8(295_489.5333669999999));
assert_eq!(295_489.94512333, round_8(295_489.945123329999));
assert_ne!(295_489.94512333, round_8(295_489.9451249999));
}
#[test]
fn test_assert_rounded_2_nominal() {
assert_rounded_2!(53_243.7448, 53_243.7401);
}
#[test]
#[should_panic]
fn test_assert_rounded_2_panic() {
assert_rounded_2!(53_243.7458, 53_243.7401);
}
#[test]
#[should_panic]
fn test_assert_approx_equal_panic() {
assert_rounded_2!(53_243.8123, 53_243.7401);
}
}