Type-independent absolute value

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Thu Oct 5 04:18:45 AEST 1989


In article <1389 at cipc1.Dayton.NCR.COM>,
	gmaranca at cipc1.Dayton.NCR.COM (Gabriel Maranca) writes:
|	[ she uses... ]
|  #define ABS(x) (((long)(x) < 0L)? -(x) : (x))

|	[ and asks... ]
|  Is this non-ANSI or unportable? It works for me.

  I don't see anything which is non-portable code, but have a few
suggestions:
	1. instead of -(x) use (-(x)).
	2. you might want to use (double) to work with more types
	3. you still have to be careful about what you use for an
	   argument to avoid things like ABS(x[m++]) which are only
	   evaluated once if ABS is a procedure.
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.lang.c mailing list