├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── bigint.go ├── bigint_test.go ├── bigrat.go ├── bigrat_test.go ├── doc.go ├── extra.go ├── extra_test.go ├── int.go ├── int16.go ├── int16_test.go ├── int32.go ├── int32_test.go ├── int64.go ├── int64_test.go ├── int8.go ├── int8_test.go ├── int_test.go ├── uint.go ├── uint16.go ├── uint16_test.go ├── uint32.go ├── uint32_test.go ├── uint64.go ├── uint64_test.go ├── uint8.go ├── uint8_test.go └── uint_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, pkg 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | * Neither the name of the {organization} nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | GOFILES=int.go int_test.go int8.go int8_test.go int16.go int16_test.go int32.go int32_test.go int64.go int64_test.go extra.go extra_test.go 2 | GOFILES+=uint.go uint_test.go uint8.go uint8_test.go uint16.go uint16_test.go uint32.go uint32_test.go uint64.go uint64_test.go 3 | GOFILES+=bigint.go bigint_test.go bigrat.go bigrat_test.go 4 | 5 | all: gen test cov README.md 6 | 7 | test: gen 8 | go test . 9 | 10 | cov: test 11 | $(eval TEMP:=$(shell mktemp cover.XXXXXXX)) 12 | go test -coverprofile=$(TEMP) . 13 | go tool cover -func=$(TEMP) 14 | rm $(TEMP) 15 | 16 | README.md: gen 17 | godoc2md github.com/pkg/math > $@ 18 | 19 | gen: $(GOFILES) 20 | go fmt $^ 21 | 22 | int8.go: int.go 23 | sed -e 's/Int/Int8/g' -e 's/int/int8/g' $^ > $@ 24 | 25 | int16.go: int.go 26 | sed -e 's/Int/Int16/g' -e 's/int/int16/g' $^ > $@ 27 | 28 | int32.go: int.go 29 | sed -e 's/Int/Int32/g' -e 's/int/int32/g' $^ > $@ 30 | 31 | int64.go: int.go 32 | sed -e 's/Int/Int64/g' -e 's/int/int64/g' $^ > $@ 33 | 34 | extra.go: int.go 35 | sed -e 's/Int//g' $^ > $@ 36 | 37 | extra_test.go: int_test.go 38 | sed -e 's/Int//g' $^ > $@ 39 | 40 | int8_test.go: int_test.go 41 | sed -e 's/Int/Int8/g' -e 's/int/int8/g' $^ > $@ 42 | 43 | int16_test.go: int_test.go 44 | sed -e 's/Int/Int16/g' -e 's/int/int16/g' $^ > $@ 45 | 46 | int32_test.go: int_test.go 47 | sed -e 's/Int/Int32/g' -e 's/int/int32/g' $^ > $@ 48 | 49 | int64_test.go: int_test.go 50 | sed -e 's/Int/Int64/g' -e 's/int/int64/g' $^ > $@ 51 | 52 | uint.go: int.go 53 | sed -e 's/Int/Uint/g' -e 's/ int/ uint/g' -e 's/\.int/\.uint/g' -e 's/\]int/\]uint/g' $^ > $@ 54 | 55 | uint8.go: uint.go 56 | sed -e 's/Uint/Uint8/g' -e 's/ uint/ uint8/g' -e 's/\.uint/\.uint8/g' -e 's/\]uint/\]uint8/g' $^ > $@ 57 | 58 | uint16.go: uint.go 59 | sed -e 's/Uint/Uint16/g' -e 's/ uint/ uint16/g' -e 's/\.uint/\.uint16/g' -e 's/\]uint/\]uint16/g' $^ > $@ 60 | 61 | uint32.go: uint.go 62 | sed -e 's/Uint/Uint32/g' -e 's/ uint/ uint32/g' -e 's/\.uint/\.uint32/g' -e 's/\]uint/\]uint32/g' $^ > $@ 63 | 64 | uint64.go: uint.go 65 | sed -e 's/Uint/Uint64/g' -e 's/ uint/ uint64/g' -e 's/\.uint/\.uint64/g' -e 's/\]uint/\]uint64/g' $^ > $@ 66 | 67 | uint8_test.go: uint_test.go 68 | sed -e 's/Uint/Uint8/g' -e 's/ uint/ uint8/g' -e 's/\.uint/\.uint8/g' -e 's/\]uint/\]uint8/g' $^ > $@ 69 | 70 | uint16_test.go: uint_test.go 71 | sed -e 's/Uint/Uint16/g' -e 's/ uint/ uint16/g' -e 's/\.uint/\.uint16/g' -e 's/\]uint/\]uint16/g' $^ > $@ 72 | 73 | uint32_test.go: uint_test.go 74 | sed -e 's/Uint/Uint32/g' -e 's/ uint/ uint32/g' -e 's/\.uint/\.uint32/g' -e 's/\]uint/\]uint32/g' $^ > $@ 75 | 76 | uint64_test.go: uint_test.go 77 | sed -e 's/Uint/Uint64/g' -e 's/ uint/ uint64/g' -e 's/\.uint/\.uint64/g' -e 's/\]uint/\]uint64/g' $^ > $@ 78 | 79 | bigrat.go: bigint.go 80 | sed -e 's/BigInt/BigRat/g' -e 's/big\.Int/big\.Rat/g' $^ > $@ 81 | 82 | .PHONEY: gen test cov 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # math 3 | import "github.com/pkg/math" 4 | 5 | Package math provides helper functions for mathematical operations 6 | over all integer Go types. 7 | 8 | Almost all files in this package are automatically generated. 9 | 10 | To regenerate this package 11 | 12 | 13 | make -B 14 | 15 | This package relies on github.com/davecheney/godoc2md. 16 | 17 | 18 | 19 | 20 | 21 | 22 | ## func EqualBigInt 23 | ``` go 24 | func EqualBigInt(a, b *big.Int) bool 25 | ``` 26 | EqualBigInt returns true if both *big.Ints are equal 27 | 28 | 29 | ## func EqualBigRat 30 | ``` go 31 | func EqualBigRat(a, b *big.Rat) bool 32 | ``` 33 | EqualBigRat returns true if both *big.Rats are equal 34 | 35 | 36 | ## func Max 37 | ``` go 38 | func Max(a, b int) int 39 | ``` 40 | Max returns the larger of two ints. 41 | 42 | 43 | ## func MaxBigInt 44 | ``` go 45 | func MaxBigInt(a, b *big.Int) *big.Int 46 | ``` 47 | MaxBigInt returns the larger of the two *big.Ints 48 | 49 | 50 | ## func MaxBigRat 51 | ``` go 52 | func MaxBigRat(a, b *big.Rat) *big.Rat 53 | ``` 54 | MaxBigRat returns the larger of the two *big.Rats 55 | 56 | 57 | ## func MaxInt 58 | ``` go 59 | func MaxInt(a, b int) int 60 | ``` 61 | MaxInt returns the larger of two ints. 62 | 63 | 64 | ## func MaxInt16 65 | ``` go 66 | func MaxInt16(a, b int16) int16 67 | ``` 68 | MaxInt16 returns the larger of two int16s. 69 | 70 | 71 | ## func MaxInt16N 72 | ``` go 73 | func MaxInt16N(v ...int16) int16 74 | ``` 75 | MaxInt16N returns the largest int16 in the set provided. 76 | If no values are provided, MaxInt16 returns 0. 77 | 78 | 79 | ## func MaxInt32 80 | ``` go 81 | func MaxInt32(a, b int32) int32 82 | ``` 83 | MaxInt32 returns the larger of two int32s. 84 | 85 | 86 | ## func MaxInt32N 87 | ``` go 88 | func MaxInt32N(v ...int32) int32 89 | ``` 90 | MaxInt32N returns the largest int32 in the set provided. 91 | If no values are provided, MaxInt32 returns 0. 92 | 93 | 94 | ## func MaxInt64 95 | ``` go 96 | func MaxInt64(a, b int64) int64 97 | ``` 98 | MaxInt64 returns the larger of two int64s. 99 | 100 | 101 | ## func MaxInt64N 102 | ``` go 103 | func MaxInt64N(v ...int64) int64 104 | ``` 105 | MaxInt64N returns the largest int64 in the set provided. 106 | If no values are provided, MaxInt64 returns 0. 107 | 108 | 109 | ## func MaxInt8 110 | ``` go 111 | func MaxInt8(a, b int8) int8 112 | ``` 113 | MaxInt8 returns the larger of two int8s. 114 | 115 | 116 | ## func MaxInt8N 117 | ``` go 118 | func MaxInt8N(v ...int8) int8 119 | ``` 120 | MaxInt8N returns the largest int8 in the set provided. 121 | If no values are provided, MaxInt8 returns 0. 122 | 123 | 124 | ## func MaxIntN 125 | ``` go 126 | func MaxIntN(v ...int) int 127 | ``` 128 | MaxIntN returns the largest int in the set provided. 129 | If no values are provided, MaxInt returns 0. 130 | 131 | 132 | ## func MaxN 133 | ``` go 134 | func MaxN(v ...int) int 135 | ``` 136 | MaxN returns the largest int in the set provided. 137 | If no values are provided, Max returns 0. 138 | 139 | 140 | ## func MaxUint 141 | ``` go 142 | func MaxUint(a, b uint) uint 143 | ``` 144 | MaxUint returns the larger of two uints. 145 | 146 | 147 | ## func MaxUint16 148 | ``` go 149 | func MaxUint16(a, b uint16) uint16 150 | ``` 151 | MaxUint16 returns the larger of two uint16s. 152 | 153 | 154 | ## func MaxUint16N 155 | ``` go 156 | func MaxUint16N(v ...uint16) uint16 157 | ``` 158 | MaxUint16N returns the largest uint16 in the set provided. 159 | If no values are provided, MaxUint16 returns 0. 160 | 161 | 162 | ## func MaxUint32 163 | ``` go 164 | func MaxUint32(a, b uint32) uint32 165 | ``` 166 | MaxUint32 returns the larger of two uint32s. 167 | 168 | 169 | ## func MaxUint32N 170 | ``` go 171 | func MaxUint32N(v ...uint32) uint32 172 | ``` 173 | MaxUint32N returns the largest uint32 in the set provided. 174 | If no values are provided, MaxUint32 returns 0. 175 | 176 | 177 | ## func MaxUint64 178 | ``` go 179 | func MaxUint64(a, b uint64) uint64 180 | ``` 181 | MaxUint64 returns the larger of two uint64s. 182 | 183 | 184 | ## func MaxUint64N 185 | ``` go 186 | func MaxUint64N(v ...uint64) uint64 187 | ``` 188 | MaxUint64N returns the largest uint64 in the set provided. 189 | If no values are provided, MaxUint64 returns 0. 190 | 191 | 192 | ## func MaxUint8 193 | ``` go 194 | func MaxUint8(a, b uint8) uint8 195 | ``` 196 | MaxUint8 returns the larger of two uint8s. 197 | 198 | 199 | ## func MaxUint8N 200 | ``` go 201 | func MaxUint8N(v ...uint8) uint8 202 | ``` 203 | MaxUint8N returns the largest uint8 in the set provided. 204 | If no values are provided, MaxUint8 returns 0. 205 | 206 | 207 | ## func MaxUintN 208 | ``` go 209 | func MaxUintN(v ...uint) uint 210 | ``` 211 | MaxUintN returns the largest uint in the set provided. 212 | If no values are provided, MaxUint returns 0. 213 | 214 | 215 | ## func Min 216 | ``` go 217 | func Min(a, b int) int 218 | ``` 219 | Min returns the smaller of two ints. 220 | 221 | 222 | ## func MinBigInt 223 | ``` go 224 | func MinBigInt(a, b *big.Int) *big.Int 225 | ``` 226 | MinBigInt returns the smaller of the two *big.Ints 227 | 228 | 229 | ## func MinBigRat 230 | ``` go 231 | func MinBigRat(a, b *big.Rat) *big.Rat 232 | ``` 233 | MinBigRat returns the smaller of the two *big.Rats 234 | 235 | 236 | ## func MinInt 237 | ``` go 238 | func MinInt(a, b int) int 239 | ``` 240 | MinInt returns the smaller of two ints. 241 | 242 | 243 | ## func MinInt16 244 | ``` go 245 | func MinInt16(a, b int16) int16 246 | ``` 247 | MinInt16 returns the smaller of two int16s. 248 | 249 | 250 | ## func MinInt16N 251 | ``` go 252 | func MinInt16N(v ...int16) int16 253 | ``` 254 | MinInt16N returns the smallest int16 in the set provided. 255 | If no values are provided, MinInt16 returns 0. 256 | 257 | 258 | ## func MinInt32 259 | ``` go 260 | func MinInt32(a, b int32) int32 261 | ``` 262 | MinInt32 returns the smaller of two int32s. 263 | 264 | 265 | ## func MinInt32N 266 | ``` go 267 | func MinInt32N(v ...int32) int32 268 | ``` 269 | MinInt32N returns the smallest int32 in the set provided. 270 | If no values are provided, MinInt32 returns 0. 271 | 272 | 273 | ## func MinInt64 274 | ``` go 275 | func MinInt64(a, b int64) int64 276 | ``` 277 | MinInt64 returns the smaller of two int64s. 278 | 279 | 280 | ## func MinInt64N 281 | ``` go 282 | func MinInt64N(v ...int64) int64 283 | ``` 284 | MinInt64N returns the smallest int64 in the set provided. 285 | If no values are provided, MinInt64 returns 0. 286 | 287 | 288 | ## func MinInt8 289 | ``` go 290 | func MinInt8(a, b int8) int8 291 | ``` 292 | MinInt8 returns the smaller of two int8s. 293 | 294 | 295 | ## func MinInt8N 296 | ``` go 297 | func MinInt8N(v ...int8) int8 298 | ``` 299 | MinInt8N returns the smallest int8 in the set provided. 300 | If no values are provided, MinInt8 returns 0. 301 | 302 | 303 | ## func MinIntN 304 | ``` go 305 | func MinIntN(v ...int) int 306 | ``` 307 | MinIntN returns the smallest int in the set provided. 308 | If no values are provided, MinInt returns 0. 309 | 310 | 311 | ## func MinN 312 | ``` go 313 | func MinN(v ...int) int 314 | ``` 315 | MinN returns the smallest int in the set provided. 316 | If no values are provided, Min returns 0. 317 | 318 | 319 | ## func MinUint 320 | ``` go 321 | func MinUint(a, b uint) uint 322 | ``` 323 | MinUint returns the smaller of two uints. 324 | 325 | 326 | ## func MinUint16 327 | ``` go 328 | func MinUint16(a, b uint16) uint16 329 | ``` 330 | MinUint16 returns the smaller of two uint16s. 331 | 332 | 333 | ## func MinUint16N 334 | ``` go 335 | func MinUint16N(v ...uint16) uint16 336 | ``` 337 | MinUint16N returns the smallest uint16 in the set provided. 338 | If no values are provided, MinUint16 returns 0. 339 | 340 | 341 | ## func MinUint32 342 | ``` go 343 | func MinUint32(a, b uint32) uint32 344 | ``` 345 | MinUint32 returns the smaller of two uint32s. 346 | 347 | 348 | ## func MinUint32N 349 | ``` go 350 | func MinUint32N(v ...uint32) uint32 351 | ``` 352 | MinUint32N returns the smallest uint32 in the set provided. 353 | If no values are provided, MinUint32 returns 0. 354 | 355 | 356 | ## func MinUint64 357 | ``` go 358 | func MinUint64(a, b uint64) uint64 359 | ``` 360 | MinUint64 returns the smaller of two uint64s. 361 | 362 | 363 | ## func MinUint64N 364 | ``` go 365 | func MinUint64N(v ...uint64) uint64 366 | ``` 367 | MinUint64N returns the smallest uint64 in the set provided. 368 | If no values are provided, MinUint64 returns 0. 369 | 370 | 371 | ## func MinUint8 372 | ``` go 373 | func MinUint8(a, b uint8) uint8 374 | ``` 375 | MinUint8 returns the smaller of two uint8s. 376 | 377 | 378 | ## func MinUint8N 379 | ``` go 380 | func MinUint8N(v ...uint8) uint8 381 | ``` 382 | MinUint8N returns the smallest uint8 in the set provided. 383 | If no values are provided, MinUint8 returns 0. 384 | 385 | 386 | ## func MinUintN 387 | ``` go 388 | func MinUintN(v ...uint) uint 389 | ``` 390 | MinUintN returns the smallest uint in the set provided. 391 | If no values are provided, MinUint returns 0. 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | - - - 402 | Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) -------------------------------------------------------------------------------- /bigint.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "math/big" 4 | 5 | // MaxBigInt returns the larger of the two *big.Ints 6 | func MaxBigInt(a, b *big.Int) *big.Int { 7 | if a.Cmp(b) > 0 { 8 | return a 9 | } 10 | return b 11 | } 12 | 13 | // MinBigInt returns the smaller of the two *big.Ints 14 | func MinBigInt(a, b *big.Int) *big.Int { 15 | if a.Cmp(b) < 0 { 16 | return a 17 | } 18 | return b 19 | } 20 | 21 | // EqualBigInt returns true if both *big.Ints are equal 22 | func EqualBigInt(a, b *big.Int) bool { return a.Cmp(b) == 0 } 23 | -------------------------------------------------------------------------------- /bigint_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import ( 4 | "math/big" 5 | "testing" 6 | ) 7 | 8 | func i(i int64) *big.Int { return big.NewInt(i) } 9 | 10 | var equalBigIntTests = []struct { 11 | a, b *big.Int 12 | want bool 13 | }{ 14 | {i(-1), i(-1), true}, 15 | {i(-1), i(0), false}, 16 | {i(0), i(0), true}, 17 | {i(0), i(-1), false}, 18 | {i(1), i(-1), false}, 19 | {i(-1), i(1), false}, 20 | {i(1), i(1), true}, 21 | } 22 | 23 | func TestEqualBigInt(t *testing.T) { 24 | for i, tt := range equalBigIntTests { 25 | got := EqualBigInt(tt.a, tt.b) 26 | if got != tt.want { 27 | t.Errorf("%d: EqualBigInt(%v, %v) = %v, want %v", i+1, tt.a, tt.b, got, tt.want) 28 | } 29 | } 30 | } 31 | 32 | var maxBigIntTests = []struct { 33 | a, b *big.Int 34 | want *big.Int 35 | }{ 36 | {i(0), i(0), i(0)}, 37 | {i(1), i(1), i(1)}, 38 | {i(1), i(2), i(2)}, 39 | {i(2), i(1), i(2)}, 40 | {i(-2), i(1), i(1)}, 41 | {i(-1), i(1), i(1)}, 42 | {i(1), i(-1), i(1)}, 43 | } 44 | 45 | func TestMaxBigInt(t *testing.T) { 46 | for i, tt := range maxBigIntTests { 47 | got := MaxBigInt(tt.a, tt.b) 48 | if !EqualBigInt(tt.want, got) { 49 | t.Errorf("%d: MaxBigInt(%v, %v) = %v, want %v", i+1, tt.a, tt.b, got, tt.want) 50 | } 51 | } 52 | } 53 | 54 | var minBigIntTests = []struct { 55 | a, b *big.Int 56 | want *big.Int 57 | }{ 58 | {i(0), i(0), i(0)}, 59 | {i(1), i(1), i(1)}, 60 | {i(1), i(2), i(1)}, 61 | {i(2), i(1), i(1)}, 62 | {i(-2), i(1), i(-2)}, 63 | {i(-1), i(1), i(-1)}, 64 | {i(1), i(-1), i(-1)}, 65 | } 66 | 67 | func TestMinBigInt(t *testing.T) { 68 | for i, tt := range minBigIntTests { 69 | got := MinBigInt(tt.a, tt.b) 70 | if !EqualBigInt(tt.want, got) { 71 | t.Errorf("%d: MinBigInt(%v, %v) = %v, want %v", i+1, tt.a, tt.b, got, tt.want) 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /bigrat.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "math/big" 4 | 5 | // MaxBigRat returns the larger of the two *big.Rats 6 | func MaxBigRat(a, b *big.Rat) *big.Rat { 7 | if a.Cmp(b) > 0 { 8 | return a 9 | } 10 | return b 11 | } 12 | 13 | // MinBigRat returns the smaller of the two *big.Rats 14 | func MinBigRat(a, b *big.Rat) *big.Rat { 15 | if a.Cmp(b) < 0 { 16 | return a 17 | } 18 | return b 19 | } 20 | 21 | // EqualBigRat returns true if both *big.Rats are equal 22 | func EqualBigRat(a, b *big.Rat) bool { return a.Cmp(b) == 0 } 23 | -------------------------------------------------------------------------------- /bigrat_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import ( 4 | "math/big" 5 | "testing" 6 | ) 7 | 8 | func r(n, d int64) *big.Rat { return big.NewRat(n, d) } 9 | 10 | var equalBigRatTests = []struct { 11 | a, b *big.Rat 12 | want bool 13 | }{ 14 | {r(1, 2), r(1, 2), true}, 15 | {r(1, 1), r(1, 1), true}, 16 | {r(1, 2), r(2, 4), true}, 17 | {r(1, 2), r(2, 3), false}, 18 | } 19 | 20 | func TestEqualBigRat(t *testing.T) { 21 | for i, tt := range equalBigRatTests { 22 | got := EqualBigRat(tt.a, tt.b) 23 | if got != tt.want { 24 | t.Errorf("%d: EqualBigRat(%v, %v) = %v, want %v", i+1, tt.a, tt.b, got, tt.want) 25 | } 26 | } 27 | } 28 | 29 | var maxBigRatTests = []struct { 30 | a, b *big.Rat 31 | want *big.Rat 32 | }{ 33 | {r(1, 2), r(1, 3), r(1, 2)}, 34 | {r(1, 3), r(1, 2), r(1, 2)}, 35 | {r(1, 2), r(1, 4), r(1, 2)}, 36 | {r(1, 4), r(1, 2), r(1, 2)}, 37 | } 38 | 39 | func TestMaxBigRat(t *testing.T) { 40 | for i, tt := range maxBigRatTests { 41 | got := MaxBigRat(tt.a, tt.b) 42 | if !EqualBigRat(tt.want, got) { 43 | t.Errorf("%d: MaxBigRat(%v, %v) = %v, want %v", i+1, tt.a, tt.b, got, tt.want) 44 | } 45 | } 46 | } 47 | 48 | var minBigRatTests = []struct { 49 | a, b *big.Rat 50 | want *big.Rat 51 | }{ 52 | {r(1, 2), r(1, 3), r(1, 3)}, 53 | {r(1, 3), r(1, 2), r(1, 3)}, 54 | {r(1, 2), r(1, 4), r(1, 4)}, 55 | {r(1, 4), r(1, 2), r(1, 4)}, 56 | } 57 | 58 | func TestMinBigRat(t *testing.T) { 59 | for i, tt := range minBigRatTests { 60 | got := MinBigRat(tt.a, tt.b) 61 | if !EqualBigRat(tt.want, got) { 62 | t.Errorf("%d: MinBigRat(%v, %v) = %v, want %v", i+1, tt.a, tt.b, got, tt.want) 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Package math provides helper functions for mathematical operations 2 | // over all integer Go types. 3 | // 4 | // Almost all files in this package are automatically generated. 5 | // 6 | // To regenerate this package 7 | // 8 | // make -B 9 | // 10 | // This package relies on github.com/davecheney/godoc2md. 11 | package math 12 | -------------------------------------------------------------------------------- /extra.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // Max returns the larger of two ints. 4 | func Max(a, b int) int { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // Min returns the smaller of two ints. 12 | func Min(a, b int) int { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxN returns the largest int in the set provided. 20 | // If no values are provided, Max returns 0. 21 | func MaxN(v ...int) int { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return Max(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxN(MaxN(v[:l]...), MaxN(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinN returns the smallest int in the set provided. 36 | // If no values are provided, Min returns 0. 37 | func MinN(v ...int) int { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return Min(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinN(MinN(v[:l]...), MinN(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /extra_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxNTests = []struct { 6 | v []int 7 | want int 8 | }{ 9 | {[]int{}, 0}, 10 | {[]int{0}, 0}, 11 | {[]int{-1}, -1}, 12 | {[]int{0, 0}, 0}, 13 | {[]int{1, 1}, 1}, 14 | {[]int{1, 2}, 2}, 15 | {[]int{2, 1}, 2}, 16 | {[]int{-1, 1}, 1}, 17 | {[]int{1, -1}, 1}, 18 | {[]int{-1, 0, 1}, 1}, 19 | {[]int{100, 42, 42, 3}, 100}, 20 | {[]int{100, 42, 17, 2, 3}, 100}, 21 | } 22 | 23 | func TestMaxN(t *testing.T) { 24 | for i, tt := range maxNTests { 25 | got := MaxN(tt.v...) 26 | if tt.want != got { 27 | t.Errorf("%d: Max(%v) = %v, want %v", i+1, tt.v, got, tt.want) 28 | } 29 | } 30 | } 31 | 32 | var minNTests = []struct { 33 | v []int 34 | want int 35 | }{ 36 | {[]int{}, 0}, 37 | {[]int{0}, 0}, 38 | {[]int{-1}, -1}, 39 | {[]int{0, 0}, 0}, 40 | {[]int{1, 1}, 1}, 41 | {[]int{1, 2}, 1}, 42 | {[]int{2, 1}, 1}, 43 | {[]int{-1, 1}, -1}, 44 | {[]int{1, -1}, -1}, 45 | {[]int{-1, 0, 1}, -1}, 46 | {[]int{100, 42, 17, -3}, -3}, 47 | {[]int{100, 42, 17, 2, 3}, 2}, 48 | } 49 | 50 | func TestMin(t *testing.T) { 51 | for i, tt := range minNTests { 52 | got := MinN(tt.v...) 53 | if tt.want != got { 54 | t.Errorf("%d: Min(%v) = %v, want %v", i+1, tt.v, got, tt.want) 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /int.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxInt returns the larger of two ints. 4 | func MaxInt(a, b int) int { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinInt returns the smaller of two ints. 12 | func MinInt(a, b int) int { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxIntN returns the largest int in the set provided. 20 | // If no values are provided, MaxInt returns 0. 21 | func MaxIntN(v ...int) int { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxInt(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxIntN(MaxIntN(v[:l]...), MaxIntN(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinIntN returns the smallest int in the set provided. 36 | // If no values are provided, MinInt returns 0. 37 | func MinIntN(v ...int) int { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinInt(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinIntN(MinIntN(v[:l]...), MinIntN(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /int16.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxInt16 returns the larger of two int16s. 4 | func MaxInt16(a, b int16) int16 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinInt16 returns the smaller of two int16s. 12 | func MinInt16(a, b int16) int16 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxInt16N returns the largest int16 in the set provided. 20 | // If no values are provided, MaxInt16 returns 0. 21 | func MaxInt16N(v ...int16) int16 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxInt16(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxInt16N(MaxInt16N(v[:l]...), MaxInt16N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinInt16N returns the smallest int16 in the set provided. 36 | // If no values are provided, MinInt16 returns 0. 37 | func MinInt16N(v ...int16) int16 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinInt16(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinInt16N(MinInt16N(v[:l]...), MinInt16N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /int16_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxInt16NTests = []struct { 6 | v []int16 7 | want int16 8 | }{ 9 | {[]int16{}, 0}, 10 | {[]int16{0}, 0}, 11 | {[]int16{-1}, -1}, 12 | {[]int16{0, 0}, 0}, 13 | {[]int16{1, 1}, 1}, 14 | {[]int16{1, 2}, 2}, 15 | {[]int16{2, 1}, 2}, 16 | {[]int16{-1, 1}, 1}, 17 | {[]int16{1, -1}, 1}, 18 | {[]int16{-1, 0, 1}, 1}, 19 | {[]int16{100, 42, 42, 3}, 100}, 20 | {[]int16{100, 42, 17, 2, 3}, 100}, 21 | } 22 | 23 | func TestMaxInt16N(t *testing.T) { 24 | for i, tt := range maxInt16NTests { 25 | got := MaxInt16N(tt.v...) 26 | if tt.want != got { 27 | t.Errorf("%d: MaxInt16(%v) = %v, want %v", i+1, tt.v, got, tt.want) 28 | } 29 | } 30 | } 31 | 32 | var minInt16NTests = []struct { 33 | v []int16 34 | want int16 35 | }{ 36 | {[]int16{}, 0}, 37 | {[]int16{0}, 0}, 38 | {[]int16{-1}, -1}, 39 | {[]int16{0, 0}, 0}, 40 | {[]int16{1, 1}, 1}, 41 | {[]int16{1, 2}, 1}, 42 | {[]int16{2, 1}, 1}, 43 | {[]int16{-1, 1}, -1}, 44 | {[]int16{1, -1}, -1}, 45 | {[]int16{-1, 0, 1}, -1}, 46 | {[]int16{100, 42, 17, -3}, -3}, 47 | {[]int16{100, 42, 17, 2, 3}, 2}, 48 | } 49 | 50 | func TestMinInt16(t *testing.T) { 51 | for i, tt := range minInt16NTests { 52 | got := MinInt16N(tt.v...) 53 | if tt.want != got { 54 | t.Errorf("%d: MinInt16(%v) = %v, want %v", i+1, tt.v, got, tt.want) 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /int32.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxInt32 returns the larger of two int32s. 4 | func MaxInt32(a, b int32) int32 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinInt32 returns the smaller of two int32s. 12 | func MinInt32(a, b int32) int32 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxInt32N returns the largest int32 in the set provided. 20 | // If no values are provided, MaxInt32 returns 0. 21 | func MaxInt32N(v ...int32) int32 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxInt32(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxInt32N(MaxInt32N(v[:l]...), MaxInt32N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinInt32N returns the smallest int32 in the set provided. 36 | // If no values are provided, MinInt32 returns 0. 37 | func MinInt32N(v ...int32) int32 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinInt32(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinInt32N(MinInt32N(v[:l]...), MinInt32N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /int32_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxInt32NTests = []struct { 6 | v []int32 7 | want int32 8 | }{ 9 | {[]int32{}, 0}, 10 | {[]int32{0}, 0}, 11 | {[]int32{-1}, -1}, 12 | {[]int32{0, 0}, 0}, 13 | {[]int32{1, 1}, 1}, 14 | {[]int32{1, 2}, 2}, 15 | {[]int32{2, 1}, 2}, 16 | {[]int32{-1, 1}, 1}, 17 | {[]int32{1, -1}, 1}, 18 | {[]int32{-1, 0, 1}, 1}, 19 | {[]int32{100, 42, 42, 3}, 100}, 20 | {[]int32{100, 42, 17, 2, 3}, 100}, 21 | } 22 | 23 | func TestMaxInt32N(t *testing.T) { 24 | for i, tt := range maxInt32NTests { 25 | got := MaxInt32N(tt.v...) 26 | if tt.want != got { 27 | t.Errorf("%d: MaxInt32(%v) = %v, want %v", i+1, tt.v, got, tt.want) 28 | } 29 | } 30 | } 31 | 32 | var minInt32NTests = []struct { 33 | v []int32 34 | want int32 35 | }{ 36 | {[]int32{}, 0}, 37 | {[]int32{0}, 0}, 38 | {[]int32{-1}, -1}, 39 | {[]int32{0, 0}, 0}, 40 | {[]int32{1, 1}, 1}, 41 | {[]int32{1, 2}, 1}, 42 | {[]int32{2, 1}, 1}, 43 | {[]int32{-1, 1}, -1}, 44 | {[]int32{1, -1}, -1}, 45 | {[]int32{-1, 0, 1}, -1}, 46 | {[]int32{100, 42, 17, -3}, -3}, 47 | {[]int32{100, 42, 17, 2, 3}, 2}, 48 | } 49 | 50 | func TestMinInt32(t *testing.T) { 51 | for i, tt := range minInt32NTests { 52 | got := MinInt32N(tt.v...) 53 | if tt.want != got { 54 | t.Errorf("%d: MinInt32(%v) = %v, want %v", i+1, tt.v, got, tt.want) 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /int64.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxInt64 returns the larger of two int64s. 4 | func MaxInt64(a, b int64) int64 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinInt64 returns the smaller of two int64s. 12 | func MinInt64(a, b int64) int64 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxInt64N returns the largest int64 in the set provided. 20 | // If no values are provided, MaxInt64 returns 0. 21 | func MaxInt64N(v ...int64) int64 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxInt64(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxInt64N(MaxInt64N(v[:l]...), MaxInt64N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinInt64N returns the smallest int64 in the set provided. 36 | // If no values are provided, MinInt64 returns 0. 37 | func MinInt64N(v ...int64) int64 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinInt64(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinInt64N(MinInt64N(v[:l]...), MinInt64N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /int64_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxInt64NTests = []struct { 6 | v []int64 7 | want int64 8 | }{ 9 | {[]int64{}, 0}, 10 | {[]int64{0}, 0}, 11 | {[]int64{-1}, -1}, 12 | {[]int64{0, 0}, 0}, 13 | {[]int64{1, 1}, 1}, 14 | {[]int64{1, 2}, 2}, 15 | {[]int64{2, 1}, 2}, 16 | {[]int64{-1, 1}, 1}, 17 | {[]int64{1, -1}, 1}, 18 | {[]int64{-1, 0, 1}, 1}, 19 | {[]int64{100, 42, 42, 3}, 100}, 20 | {[]int64{100, 42, 17, 2, 3}, 100}, 21 | } 22 | 23 | func TestMaxInt64N(t *testing.T) { 24 | for i, tt := range maxInt64NTests { 25 | got := MaxInt64N(tt.v...) 26 | if tt.want != got { 27 | t.Errorf("%d: MaxInt64(%v) = %v, want %v", i+1, tt.v, got, tt.want) 28 | } 29 | } 30 | } 31 | 32 | var minInt64NTests = []struct { 33 | v []int64 34 | want int64 35 | }{ 36 | {[]int64{}, 0}, 37 | {[]int64{0}, 0}, 38 | {[]int64{-1}, -1}, 39 | {[]int64{0, 0}, 0}, 40 | {[]int64{1, 1}, 1}, 41 | {[]int64{1, 2}, 1}, 42 | {[]int64{2, 1}, 1}, 43 | {[]int64{-1, 1}, -1}, 44 | {[]int64{1, -1}, -1}, 45 | {[]int64{-1, 0, 1}, -1}, 46 | {[]int64{100, 42, 17, -3}, -3}, 47 | {[]int64{100, 42, 17, 2, 3}, 2}, 48 | } 49 | 50 | func TestMinInt64(t *testing.T) { 51 | for i, tt := range minInt64NTests { 52 | got := MinInt64N(tt.v...) 53 | if tt.want != got { 54 | t.Errorf("%d: MinInt64(%v) = %v, want %v", i+1, tt.v, got, tt.want) 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /int8.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxInt8 returns the larger of two int8s. 4 | func MaxInt8(a, b int8) int8 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinInt8 returns the smaller of two int8s. 12 | func MinInt8(a, b int8) int8 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxInt8N returns the largest int8 in the set provided. 20 | // If no values are provided, MaxInt8 returns 0. 21 | func MaxInt8N(v ...int8) int8 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxInt8(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxInt8N(MaxInt8N(v[:l]...), MaxInt8N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinInt8N returns the smallest int8 in the set provided. 36 | // If no values are provided, MinInt8 returns 0. 37 | func MinInt8N(v ...int8) int8 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinInt8(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinInt8N(MinInt8N(v[:l]...), MinInt8N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /int8_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxInt8NTests = []struct { 6 | v []int8 7 | want int8 8 | }{ 9 | {[]int8{}, 0}, 10 | {[]int8{0}, 0}, 11 | {[]int8{-1}, -1}, 12 | {[]int8{0, 0}, 0}, 13 | {[]int8{1, 1}, 1}, 14 | {[]int8{1, 2}, 2}, 15 | {[]int8{2, 1}, 2}, 16 | {[]int8{-1, 1}, 1}, 17 | {[]int8{1, -1}, 1}, 18 | {[]int8{-1, 0, 1}, 1}, 19 | {[]int8{100, 42, 42, 3}, 100}, 20 | {[]int8{100, 42, 17, 2, 3}, 100}, 21 | } 22 | 23 | func TestMaxInt8N(t *testing.T) { 24 | for i, tt := range maxInt8NTests { 25 | got := MaxInt8N(tt.v...) 26 | if tt.want != got { 27 | t.Errorf("%d: MaxInt8(%v) = %v, want %v", i+1, tt.v, got, tt.want) 28 | } 29 | } 30 | } 31 | 32 | var minInt8NTests = []struct { 33 | v []int8 34 | want int8 35 | }{ 36 | {[]int8{}, 0}, 37 | {[]int8{0}, 0}, 38 | {[]int8{-1}, -1}, 39 | {[]int8{0, 0}, 0}, 40 | {[]int8{1, 1}, 1}, 41 | {[]int8{1, 2}, 1}, 42 | {[]int8{2, 1}, 1}, 43 | {[]int8{-1, 1}, -1}, 44 | {[]int8{1, -1}, -1}, 45 | {[]int8{-1, 0, 1}, -1}, 46 | {[]int8{100, 42, 17, -3}, -3}, 47 | {[]int8{100, 42, 17, 2, 3}, 2}, 48 | } 49 | 50 | func TestMinInt8(t *testing.T) { 51 | for i, tt := range minInt8NTests { 52 | got := MinInt8N(tt.v...) 53 | if tt.want != got { 54 | t.Errorf("%d: MinInt8(%v) = %v, want %v", i+1, tt.v, got, tt.want) 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /int_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxIntNTests = []struct { 6 | v []int 7 | want int 8 | }{ 9 | {[]int{}, 0}, 10 | {[]int{0}, 0}, 11 | {[]int{-1}, -1}, 12 | {[]int{0, 0}, 0}, 13 | {[]int{1, 1}, 1}, 14 | {[]int{1, 2}, 2}, 15 | {[]int{2, 1}, 2}, 16 | {[]int{-1, 1}, 1}, 17 | {[]int{1, -1}, 1}, 18 | {[]int{-1, 0, 1}, 1}, 19 | {[]int{100, 42, 42, 3}, 100}, 20 | {[]int{100, 42, 17, 2, 3}, 100}, 21 | } 22 | 23 | func TestMaxIntN(t *testing.T) { 24 | for i, tt := range maxIntNTests { 25 | got := MaxIntN(tt.v...) 26 | if tt.want != got { 27 | t.Errorf("%d: MaxInt(%v) = %v, want %v", i+1, tt.v, got, tt.want) 28 | } 29 | } 30 | } 31 | 32 | var minIntNTests = []struct { 33 | v []int 34 | want int 35 | }{ 36 | {[]int{}, 0}, 37 | {[]int{0}, 0}, 38 | {[]int{-1}, -1}, 39 | {[]int{0, 0}, 0}, 40 | {[]int{1, 1}, 1}, 41 | {[]int{1, 2}, 1}, 42 | {[]int{2, 1}, 1}, 43 | {[]int{-1, 1}, -1}, 44 | {[]int{1, -1}, -1}, 45 | {[]int{-1, 0, 1}, -1}, 46 | {[]int{100, 42, 17, -3}, -3}, 47 | {[]int{100, 42, 17, 2, 3}, 2}, 48 | } 49 | 50 | func TestMinInt(t *testing.T) { 51 | for i, tt := range minIntNTests { 52 | got := MinIntN(tt.v...) 53 | if tt.want != got { 54 | t.Errorf("%d: MinInt(%v) = %v, want %v", i+1, tt.v, got, tt.want) 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /uint.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxUint returns the larger of two uints. 4 | func MaxUint(a, b uint) uint { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinUint returns the smaller of two uints. 12 | func MinUint(a, b uint) uint { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxUintN returns the largest uint in the set provided. 20 | // If no values are provided, MaxUint returns 0. 21 | func MaxUintN(v ...uint) uint { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxUint(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxUintN(MaxUintN(v[:l]...), MaxUintN(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinUintN returns the smallest uint in the set provided. 36 | // If no values are provided, MinUint returns 0. 37 | func MinUintN(v ...uint) uint { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinUint(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinUintN(MinUintN(v[:l]...), MinUintN(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint16.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxUint16 returns the larger of two uint16s. 4 | func MaxUint16(a, b uint16) uint16 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinUint16 returns the smaller of two uint16s. 12 | func MinUint16(a, b uint16) uint16 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxUint16N returns the largest uint16 in the set provided. 20 | // If no values are provided, MaxUint16 returns 0. 21 | func MaxUint16N(v ...uint16) uint16 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxUint16(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxUint16N(MaxUint16N(v[:l]...), MaxUint16N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinUint16N returns the smallest uint16 in the set provided. 36 | // If no values are provided, MinUint16 returns 0. 37 | func MinUint16N(v ...uint16) uint16 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinUint16(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinUint16N(MinUint16N(v[:l]...), MinUint16N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint16_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxUint16NTests = []struct { 6 | v []uint16 7 | want uint16 8 | }{ 9 | {[]uint16{}, 0}, 10 | {[]uint16{0}, 0}, 11 | {[]uint16{0, 0}, 0}, 12 | {[]uint16{1, 1}, 1}, 13 | {[]uint16{1, 2}, 2}, 14 | {[]uint16{2, 1}, 2}, 15 | {[]uint16{1, 0, 2}, 2}, 16 | {[]uint16{100, 200, 42, 17, 2, 3}, 200}, 17 | } 18 | 19 | func TestMaxUint16N(t *testing.T) { 20 | for i, tt := range maxUint16NTests { 21 | got := MaxUint16N(tt.v...) 22 | if tt.want != got { 23 | t.Errorf("%d: MaxUint16(%v) = %v, want %v", i+1, tt.v, got, tt.want) 24 | } 25 | } 26 | } 27 | 28 | var minUint16NTests = []struct { 29 | v []uint16 30 | want uint16 31 | }{ 32 | {[]uint16{}, 0}, 33 | {[]uint16{0}, 0}, 34 | {[]uint16{0, 0}, 0}, 35 | {[]uint16{1, 1}, 1}, 36 | {[]uint16{1, 2}, 1}, 37 | {[]uint16{2, 1}, 1}, 38 | {[]uint16{1, 0, 2}, 0}, 39 | {[]uint16{100, 200, 42, 17, 2, 3}, 2}, 40 | } 41 | 42 | func TestMinUint16(t *testing.T) { 43 | for i, tt := range minUint16NTests { 44 | got := MinUint16N(tt.v...) 45 | if tt.want != got { 46 | t.Errorf("%d: MinUint16(%v) = %v, want %v", i+1, tt.v, got, tt.want) 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint32.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxUint32 returns the larger of two uint32s. 4 | func MaxUint32(a, b uint32) uint32 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinUint32 returns the smaller of two uint32s. 12 | func MinUint32(a, b uint32) uint32 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxUint32N returns the largest uint32 in the set provided. 20 | // If no values are provided, MaxUint32 returns 0. 21 | func MaxUint32N(v ...uint32) uint32 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxUint32(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxUint32N(MaxUint32N(v[:l]...), MaxUint32N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinUint32N returns the smallest uint32 in the set provided. 36 | // If no values are provided, MinUint32 returns 0. 37 | func MinUint32N(v ...uint32) uint32 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinUint32(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinUint32N(MinUint32N(v[:l]...), MinUint32N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint32_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxUint32NTests = []struct { 6 | v []uint32 7 | want uint32 8 | }{ 9 | {[]uint32{}, 0}, 10 | {[]uint32{0}, 0}, 11 | {[]uint32{0, 0}, 0}, 12 | {[]uint32{1, 1}, 1}, 13 | {[]uint32{1, 2}, 2}, 14 | {[]uint32{2, 1}, 2}, 15 | {[]uint32{1, 0, 2}, 2}, 16 | {[]uint32{100, 200, 42, 17, 2, 3}, 200}, 17 | } 18 | 19 | func TestMaxUint32N(t *testing.T) { 20 | for i, tt := range maxUint32NTests { 21 | got := MaxUint32N(tt.v...) 22 | if tt.want != got { 23 | t.Errorf("%d: MaxUint32(%v) = %v, want %v", i+1, tt.v, got, tt.want) 24 | } 25 | } 26 | } 27 | 28 | var minUint32NTests = []struct { 29 | v []uint32 30 | want uint32 31 | }{ 32 | {[]uint32{}, 0}, 33 | {[]uint32{0}, 0}, 34 | {[]uint32{0, 0}, 0}, 35 | {[]uint32{1, 1}, 1}, 36 | {[]uint32{1, 2}, 1}, 37 | {[]uint32{2, 1}, 1}, 38 | {[]uint32{1, 0, 2}, 0}, 39 | {[]uint32{100, 200, 42, 17, 2, 3}, 2}, 40 | } 41 | 42 | func TestMinUint32(t *testing.T) { 43 | for i, tt := range minUint32NTests { 44 | got := MinUint32N(tt.v...) 45 | if tt.want != got { 46 | t.Errorf("%d: MinUint32(%v) = %v, want %v", i+1, tt.v, got, tt.want) 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint64.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxUint64 returns the larger of two uint64s. 4 | func MaxUint64(a, b uint64) uint64 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinUint64 returns the smaller of two uint64s. 12 | func MinUint64(a, b uint64) uint64 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxUint64N returns the largest uint64 in the set provided. 20 | // If no values are provided, MaxUint64 returns 0. 21 | func MaxUint64N(v ...uint64) uint64 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxUint64(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxUint64N(MaxUint64N(v[:l]...), MaxUint64N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinUint64N returns the smallest uint64 in the set provided. 36 | // If no values are provided, MinUint64 returns 0. 37 | func MinUint64N(v ...uint64) uint64 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinUint64(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinUint64N(MinUint64N(v[:l]...), MinUint64N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint64_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxUint64NTests = []struct { 6 | v []uint64 7 | want uint64 8 | }{ 9 | {[]uint64{}, 0}, 10 | {[]uint64{0}, 0}, 11 | {[]uint64{0, 0}, 0}, 12 | {[]uint64{1, 1}, 1}, 13 | {[]uint64{1, 2}, 2}, 14 | {[]uint64{2, 1}, 2}, 15 | {[]uint64{1, 0, 2}, 2}, 16 | {[]uint64{100, 200, 42, 17, 2, 3}, 200}, 17 | } 18 | 19 | func TestMaxUint64N(t *testing.T) { 20 | for i, tt := range maxUint64NTests { 21 | got := MaxUint64N(tt.v...) 22 | if tt.want != got { 23 | t.Errorf("%d: MaxUint64(%v) = %v, want %v", i+1, tt.v, got, tt.want) 24 | } 25 | } 26 | } 27 | 28 | var minUint64NTests = []struct { 29 | v []uint64 30 | want uint64 31 | }{ 32 | {[]uint64{}, 0}, 33 | {[]uint64{0}, 0}, 34 | {[]uint64{0, 0}, 0}, 35 | {[]uint64{1, 1}, 1}, 36 | {[]uint64{1, 2}, 1}, 37 | {[]uint64{2, 1}, 1}, 38 | {[]uint64{1, 0, 2}, 0}, 39 | {[]uint64{100, 200, 42, 17, 2, 3}, 2}, 40 | } 41 | 42 | func TestMinUint64(t *testing.T) { 43 | for i, tt := range minUint64NTests { 44 | got := MinUint64N(tt.v...) 45 | if tt.want != got { 46 | t.Errorf("%d: MinUint64(%v) = %v, want %v", i+1, tt.v, got, tt.want) 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint8.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | // MaxUint8 returns the larger of two uint8s. 4 | func MaxUint8(a, b uint8) uint8 { 5 | if a > b { 6 | return a 7 | } 8 | return b 9 | } 10 | 11 | // MinUint8 returns the smaller of two uint8s. 12 | func MinUint8(a, b uint8) uint8 { 13 | if a < b { 14 | return a 15 | } 16 | return b 17 | } 18 | 19 | // MaxUint8N returns the largest uint8 in the set provided. 20 | // If no values are provided, MaxUint8 returns 0. 21 | func MaxUint8N(v ...uint8) uint8 { 22 | switch len(v) { 23 | case 0: 24 | return 0 25 | case 1: 26 | return v[0] 27 | case 2: 28 | return MaxUint8(v[0], v[1]) 29 | default: 30 | l := len(v) / 2 31 | return MaxUint8N(MaxUint8N(v[:l]...), MaxUint8N(v[l:]...)) 32 | } 33 | } 34 | 35 | // MinUint8N returns the smallest uint8 in the set provided. 36 | // If no values are provided, MinUint8 returns 0. 37 | func MinUint8N(v ...uint8) uint8 { 38 | switch len(v) { 39 | case 0: 40 | return 0 41 | case 1: 42 | return v[0] 43 | case 2: 44 | return MinUint8(v[0], v[1]) 45 | default: 46 | l := len(v) / 2 47 | return MinUint8N(MinUint8N(v[:l]...), MinUint8N(v[l:]...)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint8_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxUint8NTests = []struct { 6 | v []uint8 7 | want uint8 8 | }{ 9 | {[]uint8{}, 0}, 10 | {[]uint8{0}, 0}, 11 | {[]uint8{0, 0}, 0}, 12 | {[]uint8{1, 1}, 1}, 13 | {[]uint8{1, 2}, 2}, 14 | {[]uint8{2, 1}, 2}, 15 | {[]uint8{1, 0, 2}, 2}, 16 | {[]uint8{100, 200, 42, 17, 2, 3}, 200}, 17 | } 18 | 19 | func TestMaxUint8N(t *testing.T) { 20 | for i, tt := range maxUint8NTests { 21 | got := MaxUint8N(tt.v...) 22 | if tt.want != got { 23 | t.Errorf("%d: MaxUint8(%v) = %v, want %v", i+1, tt.v, got, tt.want) 24 | } 25 | } 26 | } 27 | 28 | var minUint8NTests = []struct { 29 | v []uint8 30 | want uint8 31 | }{ 32 | {[]uint8{}, 0}, 33 | {[]uint8{0}, 0}, 34 | {[]uint8{0, 0}, 0}, 35 | {[]uint8{1, 1}, 1}, 36 | {[]uint8{1, 2}, 1}, 37 | {[]uint8{2, 1}, 1}, 38 | {[]uint8{1, 0, 2}, 0}, 39 | {[]uint8{100, 200, 42, 17, 2, 3}, 2}, 40 | } 41 | 42 | func TestMinUint8(t *testing.T) { 43 | for i, tt := range minUint8NTests { 44 | got := MinUint8N(tt.v...) 45 | if tt.want != got { 46 | t.Errorf("%d: MinUint8(%v) = %v, want %v", i+1, tt.v, got, tt.want) 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /uint_test.go: -------------------------------------------------------------------------------- 1 | package math 2 | 3 | import "testing" 4 | 5 | var maxUintNTests = []struct { 6 | v []uint 7 | want uint 8 | }{ 9 | {[]uint{}, 0}, 10 | {[]uint{0}, 0}, 11 | {[]uint{0, 0}, 0}, 12 | {[]uint{1, 1}, 1}, 13 | {[]uint{1, 2}, 2}, 14 | {[]uint{2, 1}, 2}, 15 | {[]uint{1, 0, 2}, 2}, 16 | {[]uint{100, 200, 42, 17, 2, 3}, 200}, 17 | } 18 | 19 | func TestMaxUintN(t *testing.T) { 20 | for i, tt := range maxUintNTests { 21 | got := MaxUintN(tt.v...) 22 | if tt.want != got { 23 | t.Errorf("%d: MaxUint(%v) = %v, want %v", i+1, tt.v, got, tt.want) 24 | } 25 | } 26 | } 27 | 28 | var minUintNTests = []struct { 29 | v []uint 30 | want uint 31 | }{ 32 | {[]uint{}, 0}, 33 | {[]uint{0}, 0}, 34 | {[]uint{0, 0}, 0}, 35 | {[]uint{1, 1}, 1}, 36 | {[]uint{1, 2}, 1}, 37 | {[]uint{2, 1}, 1}, 38 | {[]uint{1, 0, 2}, 0}, 39 | {[]uint{100, 200, 42, 17, 2, 3}, 2}, 40 | } 41 | 42 | func TestMinUint(t *testing.T) { 43 | for i, tt := range minUintNTests { 44 | got := MinUintN(tt.v...) 45 | if tt.want != got { 46 | t.Errorf("%d: MinUint(%v) = %v, want %v", i+1, tt.v, got, tt.want) 47 | } 48 | } 49 | } 50 | --------------------------------------------------------------------------------