Skip to content

Commit

Permalink
Merge pull request #81 from Teamwork/feat-ceil-plus
Browse files Browse the repository at this point in the history
Feat: expand mathutil to add CeilPlus
  • Loading branch information
tonisanta authored Aug 23, 2024
2 parents 7869872 + bb8c22d commit d630e9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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

0 comments on commit d630e9a

Please sign in to comment.