github.com/eframework-cn/EP.GO.UTIL@v1.0.0/xmath/xmath.go (about) 1 //-----------------------------------------------------------------------// 2 // GNU GENERAL PUBLIC LICENSE // 3 // Version 2, June 1991 // 4 // // 5 // Copyright (C) EFramework, https://eframework.cn, All rights reserved. // 6 // Everyone is permitted to copy and distribute verbatim copies // 7 // of this license document, but changing it is not allowed. // 8 // SEE LICENSE.md FOR MORE DETAILS. // 9 //-----------------------------------------------------------------------// 10 11 // 封装了一些常用的数学函数. 12 package xmath 13 14 import ( 15 "encoding/binary" 16 "math/rand" 17 ) 18 19 const ( 20 INT8_MIN = -0x7f - 1 21 INT16_MIN = -0x7fff - 1 22 INT32_MIN = -0x7fffffff - 1 23 INT64_MIN = -0x7fffffffffffffff - 1 24 INT8_MAX = 0x7f 25 INT16_MAX = 0x7fff 26 INT32_MAX = 0x7fffffff 27 INT64_MAX = 0x7fffffffffffffff 28 UINT8_MAX = 0xff 29 UINT16_MAX = 0xffff 30 UINT32_MAX = 0xffffffff 31 UINT64_MAX = 0xffffffffffffffff 32 ) 33 34 var ( 35 BYTE_ORDER binary.ByteOrder = &binary.LittleEndian // 字节序(小端) 36 ) 37 38 // 最大值 39 func MaxValue(a int, b int) int { 40 if a >= b { 41 return a 42 } 43 return b 44 } 45 46 // 最小值 47 func MinValue(a int, b int) int { 48 if a >= b { 49 return b 50 } 51 return a 52 } 53 54 // 随机数 55 // min: 左区间 56 // max: 右区间 57 func RandInt(min int, max int) int { 58 if min >= max { 59 return max 60 } 61 return rand.Intn(max-min) + min 62 } 63 64 // uint32转字节数组 65 func Uint32ToBytes(value uint32) []byte { 66 bytes := make([]byte, 4) 67 BYTE_ORDER.PutUint32(bytes, value) 68 return bytes 69 } 70 71 // 字节数组转uint32 72 func Uint32FromBytes(value []byte) uint32 { 73 return BYTE_ORDER.Uint32(value) 74 }