ABS for longs and ints

Gabriel Maranca gmaranca at cipc1.Dayton.NCR.COM
Sat Sep 30 07:23:21 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 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