github.com/sandwich-go/boost@v1.3.29/xmath/internal/template2/math_template.go (about) 1 package template2 2 3 import "sort" 4 5 type MathConfig struct { 6 Key string 7 HasNegative bool 8 } 9 10 func GetMathTPLArgs() interface{} { 11 var mathConfigs = make([]MathConfig, 0) 12 var types = []string{ 13 "int", "int8", "int16", "int32", "int64", 14 "uint", "uint8", "uint16", "uint32", "uint64", 15 "float32", "float64", 16 } 17 var hasNegatives = []string{ 18 "int", "int8", "int16", "int32", "int64", 19 "float32", "float64", 20 } 21 var hasNegativeMapping = make(map[string]bool) 22 for _, v := range hasNegatives { 23 hasNegativeMapping[v] = true 24 } 25 for _, tk := range types { 26 mathConfigs = append(mathConfigs, MathConfig{Key: tk, HasNegative: hasNegativeMapping[tk]}) 27 } 28 sort.Slice(mathConfigs, func(i, j int) bool { 29 return mathConfigs[i].Key < mathConfigs[j].Key 30 }) 31 return map[string]interface{}{"MathConfigs": mathConfigs} 32 } 33 34 const MathTestTPL = `// Code generated by tools. DO NOT EDIT. 35 package xmath 36 37 import ( 38 . "github.com/smartystreets/goconvey/convey" 39 "testing" 40 ) 41 42 func TestMath(t *testing.T) { 43 Convey("Float64Equals", t, func() { 44 So(Float64Equals(0.1, 0.2), ShouldBeFalse) 45 So(Float64Equals(0.1, 0.1+EPSILON64), ShouldBeTrue) 46 }) 47 48 Convey("Float32Equals", t, func() { 49 So(Float32Equals(0.1, 0.2), ShouldBeFalse) 50 So(Float32Equals(0.1, 0.1+EPSILON32), ShouldBeTrue) 51 }) 52 53 Convey("IsZeroFloat64", t, func() { 54 So(IsZeroFloat64(0.1), ShouldBeFalse) 55 So(IsZeroFloat64(0), ShouldBeTrue) 56 }) 57 58 Convey("IsZeroFloat32", t, func() { 59 So(IsZeroFloat32(0.1), ShouldBeFalse) 60 So(IsZeroFloat32(0), ShouldBeTrue) 61 }) 62 63 Convey("IsBelowZeroFloat64", t, func() { 64 So(IsBelowZeroFloat64(0.1), ShouldBeFalse) 65 So(IsBelowZeroFloat64(0), ShouldBeTrue) 66 So(IsBelowZeroFloat64(-0.1), ShouldBeTrue) 67 }) 68 69 Convey("IsBelowZeroFloat32", t, func() { 70 So(IsBelowZeroFloat32(0.1), ShouldBeFalse) 71 So(IsBelowZeroFloat32(0), ShouldBeTrue) 72 So(IsBelowZeroFloat32(-0.1), ShouldBeTrue) 73 }) 74 {{ range $mathConfig := .MathConfigs }} 75 {{- $camelCaseKey := $mathConfig.Key | CamelCase }} 76 Convey("{{ $camelCaseKey }}", t, func() { 77 {{- if or (eq $mathConfig.Key "float32") (eq $mathConfig.Key "float64") }} 78 So(Max{{ $camelCaseKey }}(3.000000001, 3.000000002), ShouldEqual, 3.000000002) 79 So(Min{{ $camelCaseKey }}(3.000000001, 3.000000002), ShouldEqual, 3.000000001) 80 {{- if $mathConfig.HasNegative }} 81 So(Abs{{ $camelCaseKey }}(3.000000001), ShouldEqual, 3.000000001) 82 So(Abs{{ $camelCaseKey }}(-3.000000001), ShouldEqual, 3.000000001) 83 So(EffectZeroLimit{{ $camelCaseKey }}(3.000000001, 0), ShouldEqual, 3.000000001) 84 So(EffectZeroLimit{{ $camelCaseKey }}(3.000000001, -3.000000002), ShouldEqual, 0) 85 {{- end }} 86 {{- else }} 87 So(Max{{ $camelCaseKey }}(3, 2), ShouldEqual, 3) 88 So(Min{{ $camelCaseKey }}(3, 2), ShouldEqual, 2) 89 {{- if $mathConfig.HasNegative }} 90 So(Abs{{ $camelCaseKey }}(3), ShouldEqual, 3) 91 So(Abs{{ $camelCaseKey }}(-3), ShouldEqual, 3) 92 So(EffectZeroLimit{{ $camelCaseKey }}(3, 0), ShouldEqual, 3) 93 So(EffectZeroLimit{{ $camelCaseKey }}(3, -4), ShouldEqual, 0) 94 {{- end }} 95 {{- end }} 96 }) 97 {{ end }}} 98 ` 99 100 const MathTPL = `// Code generated by tools. DO NOT EDIT. 101 package xmath 102 103 import "math" 104 105 const ( 106 EPSILON64 float64 = 0.00000001 107 EPSILON32 float32 = 0.00000001 108 ) 109 110 // Integer limit values. 111 const ( 112 ConstMaxInt = math.MaxInt 113 ConstMinInt = math.MinInt 114 ConstMaxUint = math.MaxUint 115 ConstMaxInt8 = math.MaxInt8 116 ConstMinInt8 = math.MinInt8 117 ConstMaxInt16 = math.MaxInt16 118 ConstMinInt16 = math.MinInt16 119 ConstMaxInt32 = math.MaxInt32 120 ConstMinInt32 = math.MinInt32 121 ConstMaxInt64 = math.MaxInt64 122 ConstMinInt64 = math.MinInt64 123 ConstMaxUint8 = math.MaxUint8 124 ConstMaxUint16 = math.MaxUint16 125 ConstMaxUint32 = math.MaxUint32 126 ConstMaxUint64 = math.MaxUint64 127 ) 128 129 // Float64Equals 判断 float64 是否相等 130 func Float64Equals(a, b float64) bool { 131 return (a-b) {{ "<" | Unescaped }} EPSILON64 && (b-a) {{ "<" | Unescaped }} EPSILON64 132 } 133 134 // Float32Equals 判断 float32 是否相等 135 func Float32Equals(a, b float32) bool { 136 return (a-b) {{ "<" | Unescaped }} EPSILON32 && (b-a) {{ "<" | Unescaped }} EPSILON32 137 } 138 139 // IsZeroFloat64 判断 float64 是否是零值 140 func IsZeroFloat64(v float64) bool { 141 return Float64Equals(v, 0) 142 } 143 144 // IsZeroFloat32 判断 float32 是否是零值 145 func IsZeroFloat32(v float32) bool { 146 return Float32Equals(v, 0) 147 } 148 149 // IsBelowZeroFloat64 v == 0 时也返回true 150 func IsBelowZeroFloat64(v float64) bool { 151 return (v - 0) {{ "<" | Unescaped }} EPSILON64 152 } 153 154 // IsBelowZeroFloat32 v == 0 时也返回true 155 func IsBelowZeroFloat32(v float32) bool { 156 return (v - 0) {{ "<" | Unescaped }} EPSILON32 157 } 158 {{ range $mathConfig := .MathConfigs }} 159 {{- $camelCaseKey := $mathConfig.Key | CamelCase }} 160 // Max{{ $camelCaseKey }} 返回 {{ $mathConfig.Key }} 类型大值 161 func Max{{ $camelCaseKey }}(a, b {{ $mathConfig.Key }}) {{ $mathConfig.Key }} { 162 {{- if eq $mathConfig.Key "float32" }} 163 return float32(math.Max(float64(a), float64(b))) 164 {{- else if eq $mathConfig.Key "float64" }} 165 return math.Max(a, b) 166 {{- else }} 167 if a > b { 168 return a 169 } 170 return b 171 {{- end }} 172 } 173 174 // Min{{ $camelCaseKey }} 返回 {{ $mathConfig.Key }} 类型小值 175 func Min{{ $camelCaseKey }}(a, b {{ $mathConfig.Key }}) {{ $mathConfig.Key }} { 176 {{- if eq $mathConfig.Key "float32" }} 177 return float32(math.Min(float64(a), float64(b))) 178 {{- else if eq $mathConfig.Key "float64" }} 179 return math.Min(a, b) 180 {{- else }} 181 if a {{ "<" | Unescaped }} b { 182 return a 183 } 184 return b 185 {{- end }} 186 } 187 {{ if $mathConfig.HasNegative }} 188 // Abs{{ $camelCaseKey }} 返回 {{ $mathConfig.Key }} 类型绝对值 189 func Abs{{ $camelCaseKey }}(v {{ $mathConfig.Key }}) {{ $mathConfig.Key }} { 190 {{- if eq $mathConfig.Key "float32" }} 191 if IsBelowZeroFloat32(v) { 192 {{- else if eq $mathConfig.Key "float64" }} 193 if IsBelowZeroFloat64(v) { 194 {{- else }} 195 if v {{ "<" | Unescaped }} 0 { 196 {{- end }} 197 return -v 198 } 199 return v 200 } 201 202 // EffectZeroLimit{{ $camelCaseKey }} 加 change 值,返回 {{ $mathConfig.Key }} 类型值,该值不会小于0 203 func EffectZeroLimit{{ $camelCaseKey }}(v, change {{ $mathConfig.Key }}) {{ $mathConfig.Key }} { 204 v += change 205 {{- if eq $mathConfig.Key "float32" }} 206 if IsBelowZeroFloat32(v) { 207 {{- else if eq $mathConfig.Key "float64" }} 208 if IsBelowZeroFloat64(v) { 209 {{- else }} 210 if v {{ "<" | Unescaped }} 0 { 211 {{- end }} 212 v = 0 213 } 214 return v 215 } 216 {{- end }} 217 {{ end }} 218 `