-
Notifications
You must be signed in to change notification settings - Fork 95
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
Changes from 1 commit
Commits
File filter
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
commit 318fec0eb9eac51a299c27cf18c6d2f3814f6ae0
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -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; | ||
} | ||
return nil, AttributeError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return nil, cantConvert(a, "float") might be better There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, https://github.com/python/cpython/blob/279f44678c8b84a183f9eeb85e0b086228154497/Objects/floatobject.c#L853