-
Notifications
You must be signed in to change notification settings - Fork 199
/
computers.go
128 lines (109 loc) · 2.39 KB
/
computers.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
package core
import (
"math/big"
)
// MaxInt32 returns the maximum of two given numbers
func MaxInt32(a int32, b int32) int32 {
if a > b {
return a
}
return b
}
// MinInt returns the minimum of two given numbers
func MinInt(a int, b int) int {
if a < b {
return a
}
return b
}
// MaxInt returns the maximum of two given numbers
func MaxInt(a int, b int) int {
if a > b {
return a
}
return b
}
// MinInt32 returns the minimum of two given numbers
func MinInt32(a int32, b int32) int32 {
if a < b {
return a
}
return b
}
// MaxUint32 returns the maximum of two given numbers
func MaxUint32(a uint32, b uint32) uint32 {
if a > b {
return a
}
return b
}
// MinUint32 returns the minimum of two given numbers
func MinUint32(a uint32, b uint32) uint32 {
if a < b {
return a
}
return b
}
// MaxUint64 returns the maximum of two given numbers
func MaxUint64(a uint64, b uint64) uint64 {
if a > b {
return a
}
return b
}
// MinUint64 returns the minimum of two given numbers
func MinUint64(a uint64, b uint64) uint64 {
if a < b {
return a
}
return b
}
// MaxInt64 returns the maximum of two given numbers
func MaxInt64(a int64, b int64) int64 {
if a > b {
return a
}
return b
}
// MinInt64 returns the minimum of two given numbers
func MinInt64(a int64, b int64) int64 {
if a < b {
return a
}
return b
}
// MaxFloat64 returns the maximum of two given numbers
func MaxFloat64(a float64, b float64) float64 {
if a > b {
return a
}
return b
}
// GetPercentageOfValue returns the percentage part of the value
func GetPercentageOfValue(value *big.Int, percentage float64) *big.Int {
x := new(big.Float).SetInt(value)
y := big.NewFloat(percentage)
z := new(big.Float).Mul(x, y)
op := big.NewInt(0)
result, _ := z.Int(op)
return result
}
// SafeMul returns multiplication results of two uint64 values into a big int
func SafeMul(a uint64, b uint64) *big.Int {
return big.NewInt(0).Mul(big.NewInt(0).SetUint64(a), big.NewInt(0).SetUint64(b))
}
// SafeSubUint64 performs subtraction on uint64 and returns an error if it overflows
func SafeSubUint64(a, b uint64) (uint64, error) {
if a < b {
return 0, ErrSubtractionOverflow
}
return a - b, nil
}
// SafeAddUint64 performs addition on uint64 and returns an error if the addition overflows
func SafeAddUint64(a, b uint64) (uint64, error) {
s := a + b
if s >= a && s >= b {
return s, nil
}
return 0, ErrAdditionOverflow
}