-
Notifications
You must be signed in to change notification settings - Fork 652
Conversation
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.
Thanks for putting this together! Please just confirm that the command "make precommit" succeeds.
|
I realized that the right way to implement this is with an __abs__ slot
that's implemented by int, float etc.
…On Wed, Jan 4, 2017 at 9:32 PM Meador Inge ***@***.***> wrote:
make precommit passed.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#28 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAHLeyFSeiDK6fywFbiqDAM9au_QFFCaks5rPIBRgaJpZM4LbVqk>
.
|
Thanks for catching that! I reworked the patch to use the |
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.
Couple more small suggestions. Also a friendly reminder to make precommit
(I will get Travis doing that soon...)
func intAbs(f *Frame, o *Object) (*Object, *BaseException) { | ||
z := toIntUnsafe(o).Value() | ||
if z <= 0 { | ||
z = -z |
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.
This can overflow if z == MinInt so check for that case and promote to Long.
@@ -66,6 +66,14 @@ func (i *Int) IsTrue() bool { | |||
// IntType is the object representing the Python 'int' type. | |||
var IntType = newBasisType("int", reflect.TypeOf(Int{}), toIntUnsafe, ObjectType) | |||
|
|||
func intAbs(f *Frame, o *Object) (*Object, *BaseException) { | |||
z := toIntUnsafe(o).Value() | |||
if z <= 0 { |
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.
To avoid an unnecessary allocation in the positive case, how about:
z := toIntUnsafe(o)
if z.Value() > 0 {
return z.ToObject(), nil
}
// .. check for overflow as mentioned below
return NewInt(-x).ToObject(), nil
Implement the `abs` as defined here: * https://docs.python.org/2.7/library/functions.html#abs
Thanks for the review. I address the copy for > 0 and overflow issues. |
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.
This is great! Thanks for getting this done.
Implement the
abs
as defined here: