-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumeric-ints.go
190 lines (161 loc) · 4.22 KB
/
numeric-ints.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package pgtypes
import "github.com/apaxa-go/helper/mathh"
//replacer:ignore
//go:generate go run $GOPATH/src/github.com/apaxa-go/generator/replacer/main.go -- $GOFILE
// SetInt8 sets z to x and returns z.
func (z *Numeric) SetInt8(x int8) *Numeric {
if x == 0 {
return z.SetZero()
}
if x < 0 {
z.sign = numericNegative
} else {
z.sign = numericPositive
}
z.weight = 0
z.digits = []int16{mathh.AbsInt16(int16(x))} // First update type, second abs!
return z
}
// SetUint8 sets z to x and returns z.
func (z *Numeric) SetUint8(x uint8) *Numeric {
if x == 0 {
return z.SetZero()
}
z.sign = numericPositive
z.weight = 0
z.digits = []int16{int16(x)}
return z
}
// Uint8 returns the uint8 representation of x.
// If x is NaN, the result is 0.
// If x cannot be represented in an uint8, the result is undefined.
func (x *Numeric) Uint8() uint8 {
if x.sign != numericPositive || x.weight != 0 || len(x.digits) == 0 {
return 0
}
return uint8(x.digits[0])
}
// Int8 returns the int8 representation of x.
// If x is NaN, the result is 0.
// If x cannot be represented in an int8, the result is undefined.
func (x *Numeric) Int8() int8 {
if x.sign == numericNaN || x.weight != 0 || len(x.digits) == 0 {
return 0
}
if x.sign == numericNegative {
return int8(-x.digits[0]) // First - negate, only after it type conversion!
}
return int8(x.digits[0])
}
//replacer:replace
//replacer:old int64 Int64
//replacer:new int Int
//replacer:new int16 Int16
//replacer:new int32 Int32
// SetInt64 sets z to x and returns z.
func (z *Numeric) SetInt64(x int64) *Numeric {
if x == 0 {
return z.SetZero()
}
if x < 0 {
z.sign = numericNegative
} else {
z.sign = numericPositive
}
z.weight = -1
z.digits = make([]int16, 0, 1) // as x!=0 there is at least 1 1000-base digit
for x != 0 {
d := mathh.AbsInt16(int16(x % numericBase))
x /= numericBase
if d != 0 || len(z.digits) > 0 { // avoid tailing zero
z.digits = append([]int16{d}, z.digits...)
}
z.weight++
}
return z
}
// SetUint64 sets z to x and returns z.
func (z *Numeric) SetUint64(x uint64) *Numeric {
if x == 0 {
return z.SetZero()
}
z.sign = numericPositive
z.weight = -1
z.digits = make([]int16, 0, 1) // as x!=0 there is at least 1 1000-base digit
for x != 0 {
d := int16(x % numericBase)
x /= numericBase
if d != 0 || len(z.digits) > 0 { // avoid tailing zero
z.digits = append([]int16{d}, z.digits...)
}
z.weight++
}
return z
}
// Uint64 returns the uint64 representation of x.
// If x is NaN, the result is 0.
// If x cannot be represented in an uint64, the result is undefined.
func (x *Numeric) Uint64() uint64 {
const maxWeight = mathh.Uint64Bytes / 2 // Interesting, this should work at least for 1-8 bytes [unsigned] integers
if x.sign != numericPositive || len(x.digits) == 0 {
return 0
}
if x.weight > maxWeight {
return mathh.MaxUint64
}
to := mathh.Min2Int(int(x.weight), len(x.digits)-1)
var r uint64
for i := 0; i <= to; i++ {
r = r*numericBase + uint64(x.digits[i])
}
for i := to + 1; i <= int(x.weight); i++ {
r *= numericBase
}
return r
}
// Int64 returns the int64 representation of x.
// If x is NaN, the result is 0.
// If x cannot be represented in an int64, the result is undefined.
func (x *Numeric) Int64() int64 {
const maxWeight = mathh.Int64Bytes / 2 // Interesting, this should work at least for 1-8 bytes [unsigned] integers
if x.sign == numericNaN || len(x.digits) == 0 {
return 0
}
var sign int64
if x.sign == numericPositive {
if x.weight > maxWeight {
return mathh.MaxInt64
}
sign = 1
} else {
if x.weight > maxWeight {
return mathh.MinInt64
}
sign = -1
}
to := mathh.Min2Int(int(x.weight), len(x.digits)-1)
var r int64
for i := 0; i <= to; i++ {
r = r*numericBase + sign*int64(x.digits[i])
}
for i := to + 1; i <= int(x.weight); i++ {
r *= numericBase
}
return r
}
//replacer:replace
//replacer:old int64 Int64
//replacer:new int Int
//replacer:new int8 Int8
//replacer:new int16 Int16
//replacer:new int32 Int32
//replacer:new uint Uint
//replacer:new uint8 Uint8
//replacer:new uint16 Uint16
//replacer:new uint32 Uint32
//replacer:new uint64 Uint64
// NewInt64 allocates and returns a new Numeric set to x.
func NewInt64(x int64) *Numeric {
var r Numeric
return r.SetInt64(x)
}