Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Add abs() for int, float and long. #48

Closed
wants to merge 1 commit into from

Conversation

JayNakrani
Copy link

For bool, it's already supported. This produces,

$ cat build/src/haha/haha.py 
# int
haha = -42
print dir(haha)
print abs(haha)

print abs(9223372036854775807)

# float
hihi = -10.546
print dir(hihi)
print abs(hihi)

# bool
hehe = True
print dir(hehe)
print abs(hehe)

# long
huhu = long(-42)
print dir(huhu)
print abs(huhu)

$ go build haha

$ ./haha 
['__abs__', '__add__', '__and__', '__class__', '__delattr__', '__dict__', '__div__', '__eq__', '__float__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__int__', '__invert__', '__le__', '__long__', '__lshift__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__or__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rrshift__', '__rshift__', '__rsub__', '__rxor__', '__setattr__', '__sub__', '__xor__']
42
-9223372036854775808
['__abs__', '__add__', '__class__', '__delattr__', '__dict__', '__div__', '__eq__', '__float__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__radd__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__rsub__', '__setattr__', '__sub__']
10.546
['__abs__', '__add__', '__and__', '__class__', '__delattr__', '__dict__', '__div__', '__eq__', '__float__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__int__', '__invert__', '__le__', '__long__', '__lshift__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__or__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rrshift__', '__rshift__', '__rsub__', '__rxor__', '__setattr__', '__sub__', '__xor__']
1
['__abs__', '__add__', '__and__', '__class__', '__delattr__', '__dict__', '__div__', '__eq__', '__float__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__int__', '__invert__', '__le__', '__long__', '__lshift__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__nonzero__', '__or__', '__radd__', '__rand__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rrshift__', '__rshift__', '__rsub__', '__rxor__', '__setattr__', '__str__', '__sub__', '__xor__']
42

TestFoo fails but it is failing on master too.

Known limitation: abs(MaxInt) overflows on some platform because in Go, int(veryLargeFloat64) overflows. I've a todo for myself to figure out a safe way to handle them. Hopefully we can figure it out during code-review.

@trotterdylan
Copy link
Contributor

Thanks for the PR! Unfortunately there's already a work in progress PR that implements the same functionality: #28

If you wouldn't mind reviewing that request, I would appreciate having a second set of eyes on it.

@JayNakrani
Copy link
Author

Oh, didn't notice it there.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants