-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStaticInterfaceMembers.cs
More file actions
62 lines (39 loc) · 2.15 KB
/
StaticInterfaceMembers.cs
File metadata and controls
62 lines (39 loc) · 2.15 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
53
54
55
56
57
58
59
60
61
62
public interface INumber<T> where T : INumber<T>
{
static abstract T operator ++(T other);
static virtual T operator --(T other) => other;
static abstract T operator +(T left, T right);
static virtual T operator -(T left, T right) => left;
static abstract explicit operator int(T n);
static abstract explicit operator short(T n);
static abstract T Inc(T other);
static virtual T Dec(T other) => other;
static abstract T Add(T left, T right);
static virtual T Subtract(T left, T right) => left;
static T Zero() => default(T);
}
public class Complex : INumber<Complex>
{
public double Real { get; private set; } = 0.0;
public double Imaginary { get; private set; } = 0.0;
public Complex() { }
public static Complex Zero() => new Complex();
public static Complex operator ++(Complex other) =>
new Complex { Real = other.Real + 1.0, Imaginary = other.Imaginary };
public static Complex operator --(Complex other) =>
new Complex { Real = other.Real - 1.0, Imaginary = other.Imaginary };
static Complex INumber<Complex>.operator +(Complex left, Complex right) =>
new Complex { Real = left.Real + right.Real, Imaginary = left.Imaginary + right.Imaginary };
static Complex INumber<Complex>.operator -(Complex left, Complex right) =>
new Complex { Real = left.Real - right.Real, Imaginary = left.Imaginary - right.Imaginary };
public static explicit operator int(Complex n) => (int)n.Real;
static explicit INumber<Complex>.operator short(Complex n) => (short)n.Real;
static Complex INumber<Complex>.Inc(Complex other) =>
new Complex { Real = other.Real + 1.0, Imaginary = other.Imaginary };
static Complex INumber<Complex>.Dec(Complex other) =>
new Complex { Real = other.Real - 1.0, Imaginary = other.Imaginary };
public static Complex Add(Complex left, Complex right) =>
new Complex { Real = left.Real + right.Real, Imaginary = left.Imaginary + right.Imaginary };
public static Complex Subtract(Complex left, Complex right) =>
new Complex { Real = left.Real - right.Real, Imaginary = left.Imaginary - right.Imaginary };
}