-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbig.go
147 lines (135 loc) · 4.08 KB
/
big.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
package xmath
import (
"math"
"math/big"
)
// FloorBigFloat returns the greatest integer value less than or equal to x.
// It returns nil if x is an infinity.
func FloorBigFloat(x *big.Float) *big.Int {
n, acc := x.Int(nil)
if acc == big.Above {
n.Add(n, big.NewInt(-1))
}
return n
}
// CeilBigFloat returns the least integer value greater than or equal to x.
// It returns nil if x is an infinity.
func CeilBigFloat(x *big.Float) *big.Int {
n, acc := x.Int(nil)
if acc == big.Below {
n.Add(n, big.NewInt(+1))
}
return n
}
// RoundBigFloat returns the nearest integer, rounding half away from zero.
// It returns nil if x is an infinity.
func RoundBigFloat(x *big.Float) *big.Int {
z := new(big.Float).Copy(x)
return FloorBigFloat(z.Add(z, big.NewFloat(0.5)))
}
// Int64BigInt returns the integer resulting from truncating x towards zero.
// If math.MinInt64 <= x <= math.MaxInt64, the accuracy is big.Exact.
// The result is (math.MinInt64, big.Above) for x < math.MinInt64, and (math.MaxInt64, big.Below) for x > math.MaxInt64.
func Int64BigInt(x *big.Int) (int64, big.Accuracy) {
if x.IsInt64() {
return x.Int64(), big.Exact
}
switch t := x.Sign(); {
case t < 0:
return math.MinInt64, big.Above
case t > 0:
return math.MaxInt64, big.Below
}
return 0, big.Exact
}
// Uint64BigInt returns the integer resulting from truncating x towards zero.
// If 0 <= x <= math.MaxUint64, the accuracy is big.Exact.
// The result is (0, big.Above) for x < 0, and (math.MaxUint64, big.Below) for x > math.MaxUint64.
func Uint64BigInt(x *big.Int) (uint64, big.Accuracy) {
if x.IsUint64() {
return x.Uint64(), big.Exact
}
switch t := x.Sign(); {
case t < 0:
return 0, big.Above
case t > 0:
return math.MaxUint64, big.Below
}
return 0, big.Exact
}
// IntBigRat returns the result of truncating x towards zero.
// The accuracy is big.Exact if x.IsInt(); otherwise it is big.Below or big.Above.
func IntBigRat(x *big.Rat) (*big.Int, big.Accuracy) {
n, r := new(big.Int).QuoRem(x.Num(), x.Denom(), new(big.Int))
switch t := r.Sign(); {
case t < 0:
return n, big.Above
case t > 0:
return n, big.Below
}
return n, big.Exact
}
// Int64BigRat returns the integer resulting from truncating x towards zero.
// If math.MinInt64 <= x <= math.MaxInt64, the accuracy is like IntBigRat.
// The result is (math.MinInt64, big.Above) for x < math.MinInt64, and (math.MaxInt64, big.Below) for x > math.MaxInt64.
func Int64BigRat(x *big.Rat) (int64, big.Accuracy) {
n, a := IntBigRat(x)
if n.IsInt64() {
return n.Int64(), a
}
switch t := n.Sign(); {
case t < 0:
return math.MinInt64, big.Above
case t > 0:
return math.MaxInt64, big.Below
}
return 0, big.Exact
}
// Uint64BigRat returns the integer resulting from truncating x towards zero.
// If 0 <= x <= math.MaxUint64, the accuracy is like IntBigRat.
// The result is (0, big.Above) for x < 0, and (math.MaxUint64, big.Below) for x > math.MaxUint64.
func Uint64BigRat(x *big.Rat) (uint64, big.Accuracy) {
n, a := IntBigRat(x)
if n.IsUint64() {
return n.Uint64(), a
}
switch t := n.Sign(); {
case t < 0:
return 0, big.Above
case t > 0:
return math.MaxUint64, big.Below
}
return 0, big.Exact
}
// FloorBigRat returns the greatest integer value less than or equal to x.
func FloorBigRat(x *big.Rat) *big.Int {
n, r := new(big.Int).QuoRem(x.Num(), x.Denom(), new(big.Int))
if r.Sign() < 0 {
n.Add(n, big.NewInt(-1))
}
return n
}
// CeilBigRat returns the least integer value greater than or equal to x.
func CeilBigRat(x *big.Rat) *big.Int {
n, r := new(big.Int).QuoRem(x.Num(), x.Denom(), new(big.Int))
if r.Sign() > 0 {
n.Add(n, big.NewInt(+1))
}
return n
}
// RoundBigRat returns the nearest integer, rounding half away from zero.
func RoundBigRat(x *big.Rat) *big.Int {
numHalf, denomHalf := x.Num(), x.Denom()
n, r := new(big.Int).QuoRem(new(big.Int).Mul(big.NewInt(2), numHalf), new(big.Int).Mul(big.NewInt(2), denomHalf), new(big.Int))
switch t := r.Sign(); {
case t < 0:
if r.Cmp(new(big.Int).Sub(big.NewInt(0), denomHalf)) < 0 {
n.Add(n, big.NewInt(-1))
}
case t > 0:
if r.Cmp(denomHalf) >= 0 {
n.Add(n, big.NewInt(+1))
}
}
return n
}