Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: expand mathutil to add CeilPlus #81

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mathutil/mathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. 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
}

// Min gets the lowest of two numbers.
func Min(a, b int64) int64 {
if a > b {
Expand Down
22 changes: 22 additions & 0 deletions mathutil/mathutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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("CeilPlus(%f) => %f, want %f", c.in, got, c.want)
}
}
}

func TestIsSignedZero(t *testing.T) {
cases := []struct {
in string
Expand Down
Loading