-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiv.rs
More file actions
52 lines (43 loc) · 1.23 KB
/
div.rs
File metadata and controls
52 lines (43 loc) · 1.23 KB
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
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
use num::{One, Zero};
use super::finite_body::FiniteBody;
impl<T> Div for FiniteBody<T>
where
T: Add<T>
+ Mul<Output = T>
+ Div<Output = T>
+ Sub<Output = T>
+ Neg<Output = T>
+ Rem<Output = T>
+ PartialOrd
+ Copy
+ Zero
+ One,
{
type Output = Self;
fn div(self, other: Self) -> Self::Output {
if other.value == T::zero() {
panic!("Error division");
}
let inv = other.inverse();
match inv {
Some(inv) => self * inv,
None => panic!("Error division"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_01_div() {
let p = 7;
assert_eq!(FiniteBody::new(p, 2).inverse().unwrap(), 4);
assert_eq!(FiniteBody::new(p, 5).inverse().unwrap(), 3);
assert_eq!(FiniteBody::new(p, 3).inverse().unwrap(), 5);
assert_eq!((FiniteBody::new(p, 5) / FiniteBody::new(p, 2)), 6);
assert_eq!((FiniteBody::new(p, 2) / FiniteBody::new(p, 5)), 6);
assert_eq!((FiniteBody::new(p, 5) / FiniteBody::new(p, 3)), 4);
assert_eq!((FiniteBody::new(p, 0) / FiniteBody::new(p, 5)), 0);
}
}