base_utils.go 858 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package mathutil
  2. //EPSILON very small number
  3. var EPSILON float32 = 0.00000001
  4. //FloatEquals float equals
  5. func FloatEquals(a, b float32) bool {
  6. if (a-b) < EPSILON && (b-a) < EPSILON {
  7. return true
  8. }
  9. return false
  10. }
  11. //EPSILON64 very small number
  12. var EPSILON64 = 0.00000001
  13. //Float64Equals float equal
  14. func Float64Equals(a, b float64) bool {
  15. if (a-b) < EPSILON64 && (b-a) < EPSILON64 {
  16. return true
  17. }
  18. return false
  19. }
  20. //Min min
  21. func Min(a, b int) int {
  22. if a < b {
  23. return a
  24. }
  25. if b < a {
  26. return b
  27. }
  28. return a
  29. }
  30. //Max max
  31. func Max(a, b int) int {
  32. if a > b {
  33. return a
  34. }
  35. if b > a {
  36. return b
  37. }
  38. return a
  39. }
  40. // IntersectInt64 intersect map
  41. func IntersectInt64(as, bs []int64) []int64 {
  42. i := make([]int64, 0, Max(len(as), len(bs)))
  43. for _, a := range as {
  44. for _, b := range bs {
  45. if a == b {
  46. i = append(i, a)
  47. }
  48. }
  49. }
  50. return i
  51. }