math.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package math
  2. import (
  3. "math"
  4. "strconv"
  5. )
  6. // Abs absolute value
  7. // php abs
  8. func Abs(x float64) float64 {
  9. return math.Abs(x)
  10. }
  11. // Round number round
  12. // php round
  13. func Round(x float64) int64 {
  14. return int64(math.Floor(x + 0.5))
  15. }
  16. // Floor returns the greatest integer value less than or equal to x
  17. // php floor
  18. func Floor(x float64) float64 {
  19. return math.Floor(x)
  20. }
  21. // Ceil returns the least integer value greater than or equal to x
  22. // php ceil
  23. func Ceil(x float64) float64 {
  24. return math.Ceil(x)
  25. }
  26. // Max returns the max num in nums
  27. // php max
  28. func Max(nums ...float64) float64 {
  29. if len(nums) < 2 {
  30. panic("nums: the nums length is less than 2")
  31. }
  32. maxNum := nums[0]
  33. for i := 1; i < len(nums); i++ {
  34. maxNum = math.Max(maxNum, nums[i])
  35. }
  36. return maxNum
  37. }
  38. // Min returns the min num in nums
  39. // php min
  40. func Min(nums ...float64) float64 {
  41. if len(nums) < 2 {
  42. panic("nums: the nums length is less than 2")
  43. }
  44. minNum := nums[0]
  45. for i := 1; i < len(nums); i++ {
  46. minNum = math.Min(minNum, nums[i])
  47. }
  48. return minNum
  49. }
  50. // DecBin Decimal to binary
  51. // php decbin
  52. func DecBin(number int64) string {
  53. return strconv.FormatInt(number, 2)
  54. }
  55. // DecHex Decimal to hexadecimal
  56. // php dechex
  57. func DecHex(number int64) string {
  58. return strconv.FormatInt(number, 16)
  59. }