Skip to content

Implement float is_integer method #112

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

Merged
merged 3 commits into from
Oct 5, 2019
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
14 changes: 14 additions & 0 deletions py/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ func (a Float) M__ge__(other Object) (Object, error) {
return NotImplemented, nil
}

// Properties
func init() {
FloatType.Dict["is_integer"] = MustNewMethod("is_integer", func(self Object) (Object, error) {
if a, ok := convertToFloat(self); ok {
f, err := FloatAsFloat64(a)
if err != nil {
return nil, err
}
return NewBool(math.Floor(f) == f), nil
}
return cantConvert(self, "float")
}, 0, "is_integer() -> Return True if the float instance is finite with integral value, and False otherwise.")
}

// Check interface is satisfied
var _ floatArithmetic = Float(0)
var _ conversionBetweenTypes = Float(0)
Expand Down
4 changes: 4 additions & 0 deletions py/tests/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@
assert str(float("1.00")) == "1.0"
assert str(float("2.010")) == "2.01"

doc="is_integer"
assert (1.0).is_integer() == True
assert (2.3).is_integer() == False

doc="finished"