# [Numpy-discussion] round() and abs() Ufuncs???

Chris Barker chrishbarker at home.net
Wed Sep 26 11:35:05 CDT 2001

```Hi all,

I was recently surprised to find that there are no round(0 or abs()
Ufuncs with Numeric. I'm imagining that they might exist under other
names, but if not, I submit my versions for critique (lightly tested)

-Chris

from Numeric import *

def Uabs(a):
"""

A Ufunc version of the Python abs() function

"""
a = asarray(a)
if a.typecode() == 'D' or a.typecode() == 'F':# for complex numbers
return sqrt(a.imag**2 + a.real**2)
else:
return where(a < 0, -a, a)

def Uround(a,n=0):
"""

A Ufunc version of the Python round() function. It should behave in
the same way

Note: I think this is the right thing to do for negative numbers,
but not totally sure. (Uround(-0.5) = 0, but Uround(-0.5000001) =
-1)

It won't work for complex numbers

"""

a = asarray(a)
n = asarray(n)
return floor((a * 10.**n) + 0.5) / 10.**n

--
Christopher Barker,
Ph.D.
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------

```