abs(-5)
Second
import math
math.fabs(-5)
How do these methods differ?
Second
How do these methods differ?
python
|
|||
add a comment
|
up vote
59 down vote
|
In addition to floats,
|
|||
add a comment
|
up vote
4 down vote
|
Edit: as @aix suggested, a better (more fair) way to compare the speed difference:
So it seems In addition to what @aix has said, one more thing to consider is the speed difference:
So |
||||||||||||||||||||
|
abs
works with far more than just integers and floats, and the result type is not always the same as the argument, e.g.abs(3+4j)
. – agf May 27 '12 at 7:24fabs
taking longer due to its always-float nature and you've got the right answer! – Patrick Perini May 27 '12 at 7:25__builtin__.abs()
be successfully applied to? – NPE May 27 '12 at 7:31__abs__
magic method – agf May 27 '12 at 17:59