mathutils.go 672 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package zxcvbn_math
  2. import "math"
  3. /**
  4. I am surprised that I have to define these. . . Maybe i just didn't look hard enough for a lib.
  5. */
  6. //http://blog.plover.com/math/choose.html
  7. func NChoseK(n, k float64) float64 {
  8. if k > n {
  9. return 0
  10. } else if k == 0 {
  11. return 1
  12. }
  13. var r float64 = 1
  14. for d := float64(1); d <= k; d++ {
  15. r *= n
  16. r /= d
  17. n--
  18. }
  19. return r
  20. }
  21. func Round(val float64, roundOn float64, places int) (newVal float64) {
  22. var round float64
  23. pow := math.Pow(10, float64(places))
  24. digit := pow * val
  25. _, div := math.Modf(digit)
  26. if div >= roundOn {
  27. round = math.Ceil(digit)
  28. } else {
  29. round = math.Floor(digit)
  30. }
  31. newVal = round / pow
  32. return
  33. }