-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a function to check if whole number (#14)
- Loading branch information
1 parent
129d5a6
commit 076114a
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |