2017-11-19 21:39:56 +00:00
|
|
|
package geo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2018-06-07 22:50:01 +00:00
|
|
|
// RounderFunc is a function which converts a floating point number into an
|
|
|
|
// integer.
|
|
|
|
type RounderFunc func(float64) int64
|
2017-11-19 21:39:56 +00:00
|
|
|
|
2018-06-07 22:50:01 +00:00
|
|
|
// Round is helper for calling the RounderFunc and converting the result to an
|
|
|
|
// int.
|
|
|
|
func (rf RounderFunc) Round(f float64) int {
|
|
|
|
return int(rf(f))
|
|
|
|
}
|
2017-11-19 21:39:56 +00:00
|
|
|
|
2018-06-07 22:50:01 +00:00
|
|
|
// A few RounderFuncs which can be used. Set the Rounder global variable to pick
|
|
|
|
// one.
|
|
|
|
var (
|
|
|
|
Floor RounderFunc = func(f float64) int64 { return int64(math.Floor(f)) }
|
|
|
|
Ceil RounderFunc = func(f float64) int64 { return int64(math.Ceil(f)) }
|
|
|
|
Round RounderFunc = func(f float64) int64 {
|
2017-11-19 21:39:56 +00:00
|
|
|
if f < 0 {
|
|
|
|
f = math.Ceil(f - 0.5)
|
|
|
|
}
|
|
|
|
f = math.Floor(f + 0.5)
|
2018-06-07 22:50:01 +00:00
|
|
|
return int64(f)
|
2017-11-19 21:39:56 +00:00
|
|
|
}
|
2018-06-07 22:50:01 +00:00
|
|
|
)
|
2017-11-19 21:39:56 +00:00
|
|
|
|
2018-06-07 22:50:01 +00:00
|
|
|
// Rounder is the RounderFunc which will be used by all functions and methods in
|
|
|
|
// this package when needed.
|
|
|
|
var Rounder = Ceil
|