-
Notifications
You must be signed in to change notification settings - Fork 1
/
float.go
161 lines (137 loc) · 3.53 KB
/
float.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
package convert
import (
"fmt"
"math"
"reflect"
"strconv"
)
func Float64(in any, debugKeys ...string) float64 {
f64, err := Float64Err(in, debugKeys...)
if err != nil {
return 0
}
return f64
}
func Float32(in any, debugKeys ...string) float32 {
f, err := Float32Err(in, debugKeys...)
if err != nil {
return 0
}
return f
}
func Float64Err(in any, debugKeys ...string) (float64, error) {
f, err := BaseFloat64Err(in, debugKeys...)
if err != nil {
return 0, err
}
if math.IsInf(f, -1) || math.IsInf(f, -1) {
return 0, err
}
return f, nil
}
func BaseFloat64Err(in any, debugKeys ...string) (float64, error) {
if in == nil {
return .0, fmt.Errorf("Float64Err null value for '%+v' [keys: %+v]", in, debugKeys)
}
switch in.(type) {
case reflect.Kind:
return 0, fmt.Errorf("Float64Err wrong value for '%+v' [keys: %+v]", in, debugKeys)
case float64:
return in.(float64), nil
case float32:
return float64(in.(float32)), nil
case bool:
if in.(bool) {
return 1.0, nil
}
return 0, nil
case uint8:
return float64(in.(uint8)), nil
case uint16:
return float64(in.(uint16)), nil
case uint32:
return float64(in.(uint32)), nil
case uint:
return float64(in.(uint)), nil
case uint64:
return float64(in.(uint64)), nil
case int8:
return float64(in.(int8)), nil
case int16:
return float64(in.(int16)), nil
case int32:
return float64(in.(int32)), nil
case int:
return float64(in.(int)), nil
case int64:
return float64(in.(int64)), nil
case []byte:
f, err := strconv.ParseFloat(string(in.([]byte)), 64)
if err != nil {
return 0, fmt.Errorf("Float64Err wrong common value for '%+v' [keys: %+v]", in, debugKeys)
}
return f, nil
case string:
f, err := strconv.ParseFloat(in.(string), 64)
if err != nil {
return 0, fmt.Errorf("Float64Err wrong common value for '%+v' [keys: %+v]", in, debugKeys)
}
return f, nil
}
return 0, fmt.Errorf("Float64Err wrong unreachable value for '%+v' [keys: %+v]", in, debugKeys)
}
func Float32Err(in any, debugKeys ...string) (float32, error) {
if in == nil {
return .0, fmt.Errorf("Float32Err null value for '%+v' [keys: %+v]", in, debugKeys)
}
switch in.(type) {
case reflect.Kind:
return 0, fmt.Errorf("Float32Err wrong value for '%+v' [keys: %+v]", in, debugKeys)
case float64:
f64 := in.(float64)
if f64 > math.MaxFloat32 || f64 < -1.0*math.MaxFloat32 {
return 0, fmt.Errorf("Float32Err Wrong value for '%+v' [keys: %+v]", in, debugKeys)
}
return float32(f64), nil
case float32:
return in.(float32), nil
case bool:
if in.(bool) {
return 1.0, nil
}
return 0, nil
case uint8:
return float32(in.(uint8)), nil
case uint16:
return float32(in.(uint16)), nil
case uint32:
return float32(in.(uint32)), nil
case uint:
return float32(in.(uint)), nil
case uint64:
return float32(in.(uint64)), nil
case int8:
return float32(in.(int8)), nil
case int16:
return float32(in.(int16)), nil
case int32:
return float32(in.(int32)), nil
case int:
return float32(in.(int)), nil
case int64:
return float32(in.(int64)), nil
case []byte:
f, err := strconv.ParseFloat(string(in.([]byte)), 32)
if err != nil {
return 0, fmt.Errorf("Float32Err wrong common value for '%+v' [keys: %+v]", in, debugKeys)
}
return float32(f), nil
case string:
f, err := strconv.ParseFloat(in.(string), 32)
if err != nil {
return 0, fmt.Errorf("Float32Err wrong common value for '%+v' [keys: %+v]", in, debugKeys)
}
return float32(f), nil
}
return 0, fmt.Errorf("Float32Err wrong unreachable value for '%+v' [keys: %+v]", in, debugKeys)
}