diff --git a/go_helpers_suite_test.go b/float/float_suite_test.go similarity index 55% rename from go_helpers_suite_test.go rename to float/float_suite_test.go index ed36a97..ea52763 100644 --- a/go_helpers_suite_test.go +++ b/float/float_suite_test.go @@ -1,4 +1,4 @@ -package helpers_test +package float_test import ( . "github.com/onsi/ginkgo" @@ -7,7 +7,7 @@ import ( "testing" ) -func TestGoHelpers(t *testing.T) { +func TestFloat(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "GoHelpers Suite") + RunSpecs(t, "Float Suite") } diff --git a/float/round.go b/float/round.go new file mode 100644 index 0000000..9c201d0 --- /dev/null +++ b/float/round.go @@ -0,0 +1,14 @@ +package float + +import "math" + +// Round returns a float rounded according to "Round to nearest, ties away from zero" IEEE floaing point rounding rule +func Round(x float64) float64 { + var absx, y float64 + absx = math.Abs(x) + y = math.Floor(absx) + if absx-y >= 0.5 { + y += 1.0 + } + return math.Copysign(y, x) +} diff --git a/float/round_test.go b/float/round_test.go new file mode 100644 index 0000000..a6257bf --- /dev/null +++ b/float/round_test.go @@ -0,0 +1,35 @@ +package float_test + +import ( + "math" + + . "github.com/Luzifer/go_helpers/float" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Round", func() { + + It("should match the example table of IEEE 754 rules", func() { + Expect(Round(11.5)).To(Equal(12.0)) + Expect(Round(12.5)).To(Equal(13.0)) + Expect(Round(-11.5)).To(Equal(-12.0)) + Expect(Round(-12.5)).To(Equal(-13.0)) + }) + + It("should have correct rounding for numbers near 0.5", func() { + Expect(Round(0.499999999997)).To(Equal(0.0)) + Expect(Round(-0.499999999997)).To(Equal(0.0)) + }) + + It("should be able to handle +/-Inf", func() { + Expect(Round(math.Inf(1))).To(Equal(math.Inf(1))) + Expect(Round(math.Inf(-1))).To(Equal(math.Inf(-1))) + }) + + It("should be able to handle NaN", func() { + Expect(math.IsNaN(Round(math.NaN()))).To(Equal(true)) + }) + +}) diff --git a/slice.go b/str/slice.go similarity index 96% rename from slice.go rename to str/slice.go index 292f91b..f93af69 100644 --- a/slice.go +++ b/str/slice.go @@ -1,4 +1,4 @@ -package helpers +package str // AppendIfMissing adds a string to a slice when it's not present yet func AppendIfMissing(slice []string, s string) []string { diff --git a/slice_test.go b/str/slice_test.go similarity index 95% rename from slice_test.go rename to str/slice_test.go index fa51118..d24ef6b 100644 --- a/slice_test.go +++ b/str/slice_test.go @@ -1,7 +1,7 @@ -package helpers_test +package str_test import ( - . "github.com/Luzifer/go_helpers" + . "github.com/Luzifer/go_helpers/str" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/str/str_suite_test.go b/str/str_suite_test.go new file mode 100644 index 0000000..9ec9128 --- /dev/null +++ b/str/str_suite_test.go @@ -0,0 +1,13 @@ +package str_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "testing" +) + +func TestStr(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Str Suite") +}