2018-08-29 02:30:34 +00:00
|
|
|
package zxcvbnmath
|
2015-10-01 13:38:42 +00:00
|
|
|
|
2016-01-13 22:49:04 +00:00
|
|
|
import "math"
|
2015-10-01 13:38:42 +00:00
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
/*
|
|
|
|
NChoseK http://blog.plover.com/math/choose.html
|
2015-10-01 13:38:42 +00:00
|
|
|
I am surprised that I have to define these. . . Maybe i just didn't look hard enough for a lib.
|
2016-01-13 22:49:04 +00:00
|
|
|
*/
|
2015-10-06 18:43:37 +00:00
|
|
|
func NChoseK(n, k float64) float64 {
|
|
|
|
if k > n {
|
2015-10-01 13:38:42 +00:00
|
|
|
return 0
|
2015-10-06 18:43:37 +00:00
|
|
|
} else if k == 0 {
|
2015-10-01 13:38:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:43:37 +00:00
|
|
|
var r float64 = 1
|
2015-10-01 13:38:42 +00:00
|
|
|
|
2015-10-06 18:43:37 +00:00
|
|
|
for d := float64(1); d <= k; d++ {
|
|
|
|
r *= n
|
2015-10-01 13:38:42 +00:00
|
|
|
r /= d
|
2015-10-06 18:43:37 +00:00
|
|
|
n--
|
2015-10-01 13:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
2015-10-06 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 02:30:34 +00:00
|
|
|
// Round a number
|
2015-10-06 18:43:37 +00:00
|
|
|
func Round(val float64, roundOn float64, places int) (newVal float64) {
|
|
|
|
var round float64
|
|
|
|
pow := math.Pow(10, float64(places))
|
|
|
|
digit := pow * val
|
|
|
|
_, div := math.Modf(digit)
|
|
|
|
if div >= roundOn {
|
|
|
|
round = math.Ceil(digit)
|
|
|
|
} else {
|
|
|
|
round = math.Floor(digit)
|
|
|
|
}
|
|
|
|
newVal = round / pow
|
|
|
|
return
|
2016-01-13 22:49:04 +00:00
|
|
|
}
|