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 1 commit
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
Next Next commit
Implement float is_integer method
  • Loading branch information
DoDaek committed Oct 2, 2019
commit 318fec0eb9eac51a299c27cf18c6d2f3814f6ae0
15 changes: 15 additions & 0 deletions py/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,21 @@ 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
}
b := math.Abs(f - math.Round(f))
return NewBool(b < math.SmallestNonzeroFloat64), nil;
Copy link
Collaborator

@corona10 corona10 Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to use this logic?
CPython check the float is an integer by using the floor API.

 o = (floor(x) == x) ? Py_True : Py_False;

Copy link
Contributor Author

@DoDaek DoDaek Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculating the difference between rounded value and original value.
If the difference is less than the minimum value of float64, I think that it would be an integer.

I'll check the Cpython and then fix that part by using floor API.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
return nil, AttributeError
Copy link
Collaborator

@corona10 corona10 Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return nil, cantConvert(a, "float")

might be better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change that part with cantConvert method.

}, 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"