Skip to content

Commit

Permalink
added a function to check if whole number (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankethkini authored Oct 11, 2023
1 parent 129d5a6 commit 076114a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions number/number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package number

import "math"

func IsWhole(num float64) bool {
if math.IsInf(num, 0) || math.IsNaN(num) {
return false
}
return math.Trunc(num) == num
}
61 changes: 61 additions & 0 deletions number/number_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package number

import (
"fmt"
"math"
"testing"
)

func TestIsWhole(t *testing.T) {
tests := []struct {
input float64
expected bool
}{
{
input: 0.000,
expected: true,
},
{
input: 2.90,
expected: false,
},
{
input: 1.9,
expected: false,
},
{
input: 1,
expected: true,
},
{
input: 0.11,
expected: false,
},
{
input: -0.11,
expected: false,
},
{
input: -1,
expected: true,
},
{
input: math.Inf(-1),
expected: false,
},
{
input: math.Inf(1),
expected: false,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("test isWholeNumber for %f", test.input), func(t *testing.T) {
resp := IsWhole(test.input)

if resp != test.expected {
t.Errorf("unexpected result %v", resp)
}
})
}
}

0 comments on commit 076114a

Please sign in to comment.