-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareNumbers.go
165 lines (160 loc) · 3.5 KB
/
CompareNumbers.go
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
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"fmt"
"reflect"
"github.com/pkg/errors"
)
// CompareNumbers compares parameter a and parameter b.
// The parameters may be of type uint8, int, int64, or float64.
// If a > b, then returns 1. If a < b, then returns -1. If a == b, then return 0.
func CompareNumbers(a interface{}, b interface{}) (int, error) {
switch a.(type) {
case int:
switch b.(type) {
case int:
if a.(int) > b.(int) {
return 1, nil
} else if a.(int) < b.(int) {
return -1, nil
} else {
return 0, nil
}
case int64:
if int64(a.(int)) > b.(int64) {
return 1, nil
} else if int64(a.(int)) < b.(int64) {
return -1, nil
} else {
return 0, nil
}
case uint8:
if a.(int) > int(b.(uint8)) {
return 1, nil
} else if a.(int) < int(b.(uint8)) {
return -1, nil
} else {
return 0, nil
}
case float64:
if float64(a.(int)) > b.(float64) {
return 1, nil
} else if float64(a.(int)) < b.(float64) {
return -1, nil
} else {
return 0, nil
}
}
case int64:
switch b.(type) {
case int:
if a.(int64) > int64(b.(int)) {
return 1, nil
} else if a.(int64) < int64(b.(int)) {
return -1, nil
} else {
return 0, nil
}
case int64:
if a.(int64) > b.(int64) {
return 1, nil
} else if a.(int64) < b.(int64) {
return -1, nil
} else {
return 0, nil
}
case uint8:
if a.(int64) > int64(b.(uint8)) {
return 1, nil
} else if a.(int64) < int64(b.(uint8)) {
return -1, nil
} else {
return 0, nil
}
case float64:
if float64(a.(int64)) > b.(float64) {
return 1, nil
} else if float64(a.(int64)) < b.(float64) {
return -1, nil
} else {
return 0, nil
}
}
case uint8:
switch b.(type) {
case int:
if int(a.(uint8)) > int(b.(int)) {
return 1, nil
} else if int(a.(uint8)) < int(b.(int)) {
return -1, nil
} else {
return 0, nil
}
case int64:
if int64(a.(uint8)) > b.(int64) {
return 1, nil
} else if int64(a.(uint8)) < b.(int64) {
return -1, nil
} else {
return 0, nil
}
case uint8:
if a.(uint8) > b.(uint8) {
return 1, nil
} else if a.(uint8) < b.(uint8) {
return -1, nil
} else {
return 0, nil
}
case float64:
if float64(a.(uint8)) > b.(float64) {
return 1, nil
} else if float64(a.(uint8)) < b.(float64) {
return -1, nil
} else {
return 0, nil
}
}
case float64:
switch b.(type) {
case int:
if a.(float64) > float64(b.(int)) {
return 1, nil
} else if a.(float64) < float64(b.(int)) {
return -1, nil
} else {
return 0, nil
}
case int64:
if a.(float64) > float64(b.(int64)) {
return 1, nil
} else if a.(float64) < float64(b.(int64)) {
return -1, nil
} else {
return 0, nil
}
case uint8:
if a.(float64) > float64(b.(uint8)) {
return 1, nil
} else if a.(float64) < float64(b.(uint8)) {
return -1, nil
} else {
return 0, nil
}
case float64:
if a.(float64) > b.(float64) {
return 1, nil
} else if a.(float64) < b.(float64) {
return -1, nil
} else {
return 0, nil
}
}
}
return 0, errors.New(fmt.Sprintf("Error comparing numbers %#v (%v) and %#v (%v)", a, reflect.TypeOf(a).String(), b, reflect.TypeOf(b).String()))
}