From 42a7c1cfe404b941223a1fcf7dbe8c714b416e10 Mon Sep 17 00:00:00 2001 From: Toni Santandreu Sureda Date: Thu, 25 Jul 2024 16:26:07 +0200 Subject: [PATCH 1/3] Feat: expand mathutils to add CeilPlus --- mathutil/mathutil.go | 8 ++++++++ mathutil/mathutil_test.go | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 1c752fd..cc2d40b 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -24,6 +24,14 @@ func RoundPlus(f float64, precision int) float64 { return Round(f*shift) / shift } +// CeilPlus will ceil the value to the given precision. +// +// e.g. RoundPlus(123.233333, 2) will return 123.24 +func CeilPlus(f float64, precision int) float64 { + multiplier := math.Pow10(precision) + return math.Ceil(f*multiplier) / multiplier +} + // Min gets the lowest of two numbers. func Min(a, b int64) int64 { if a > b { diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index 494a77e..b100170 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -37,6 +37,7 @@ func TestRoundPlus(t *testing.T) { {123.555555, 3, 123.556}, {123.558, 2, 123.56}, {-123.555555, 3, -123.556}, + {123.233333, 2, 123.23}, } for _, c := range cases { @@ -48,6 +49,27 @@ func TestRoundPlus(t *testing.T) { } +func TestCeilPlus(t *testing.T) { + cases := []struct { + in float64 + precision int + want float64 + }{ + {123.554999, 3, 123.555}, + {123.555555, 3, 123.556}, + {123.558, 2, 123.56}, + {-123.555555, 3, -123.555}, + {123.233333, 2, 123.24}, + } + + for _, c := range cases { + got := CeilPlus(c.in, c.precision) + if got != c.want { + t.Errorf("Round(%f) => %f, want %f", c.in, got, c.want) + } + } +} + func TestIsSignedZero(t *testing.T) { cases := []struct { in string From 1999fd9a5a7f4c63aa519d1e3b18c273610d7ae1 Mon Sep 17 00:00:00 2001 From: Toni Santandreu Sureda Date: Thu, 25 Jul 2024 16:30:34 +0200 Subject: [PATCH 2/3] typo --- mathutil/mathutil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index cc2d40b..42606a8 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -26,7 +26,7 @@ func RoundPlus(f float64, precision int) float64 { // CeilPlus will ceil the value to the given precision. // -// e.g. RoundPlus(123.233333, 2) will return 123.24 +// e.g. CeilPlus(123.233333, 2) will return 123.24 func CeilPlus(f float64, precision int) float64 { multiplier := math.Pow10(precision) return math.Ceil(f*multiplier) / multiplier From 04b10b3f58a9051c3e72a1da1859e8d7ddd6e35f Mon Sep 17 00:00:00 2001 From: Toni Santandreu Sureda Date: Thu, 25 Jul 2024 16:35:01 +0200 Subject: [PATCH 3/3] typo copy-paste --- mathutil/mathutil_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index b100170..2305dae 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -65,7 +65,7 @@ func TestCeilPlus(t *testing.T) { for _, c := range cases { got := CeilPlus(c.in, c.precision) if got != c.want { - t.Errorf("Round(%f) => %f, want %f", c.in, got, c.want) + t.Errorf("CeilPlus(%f) => %f, want %f", c.in, got, c.want) } } }