Type-independent absolute value

Gabriel Maranca gmaranca at cipc1.Dayton.NCR.COM
Wed Oct 4 21:23:18 AEST 1989


Does anybody know why the abs() math function is not type independent? 
Or has this been changed in ANSI "C"?

I use the following macro instead of the standard library function:

#define ABS(x) (((long)(x) < 0L)? -(x) : (x))

This works for ints and longs, by casting the argument to a long for the
comparison, and then returning the absolute value of the argument with
its original type.

Is this non-ANSI or unportable? It works for me.

---
#Gabriel Maranca
#Gabriel.Maranca at cipc1.Dayton.NCR.COM
#...!uunet!ncrlnk!cipc1!gmaranca
-- 
#Gabriel Maranca
#Gabriel.Maranca at cipc1.Dayton.NCR.COM
#...!uunet!ncrlnk!cipc1!gmaranca



More information about the Comp.lang.c mailing list