Skip to content

Commit

Permalink
Use math.Round for rounding in RoundToInt32
Browse files Browse the repository at this point in the history
... instead of re-implementing it (with side-effects).

Since RoundToInt32 ends up being a type-casting wrapper around
math.Round, deprecate it in favour of the latter.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Nov 24, 2023
1 parent cf03d44 commit fc7c3d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions integer/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package integer

import "math"

// IntMax returns the maximum of the params
func IntMax(a, b int) int {
if b > a {
Expand Down Expand Up @@ -65,9 +67,7 @@ func Int64Min(a, b int64) int64 {
}

// RoundToInt32 rounds floats into integer numbers.
// Deprecated: use math.Round() and a cast directly.
func RoundToInt32(a float64) int32 {
if a < 0 {
return int32(a - 0.5)
}
return int32(a + 0.5)
return int32(math.Round(a))
}
4 changes: 4 additions & 0 deletions integer/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ func TestRoundToInt32(t *testing.T) {
num: 0,
exp: 0,
},
{
num: 0.49999999999999994,
exp: 0,
},
}

for i, test := range tests {
Expand Down

0 comments on commit fc7c3d4

Please sign in to comment.