ABS for longs and ints
Chris Torek
chris at mimsy.UUCP
Sun Oct 1 04:02:31 AEST 1989
In article <1381 at cipc1.Dayton.NCR.COM> gmaranca at cipc1.Dayton.NCR.COM
(Gabriel Maranca) writes:
>Does anybody know why the abs math function is not type independent?
Because it is a function, not a macro.
>Or has this been changed in ANSI "C"?
No.
>I use the following macro instead of the library function:
>
>#define ABS(x) (((long)(x) < 0L)? -(x) : (x))
The cast to long and the 0L are both unnecessary. If left out, this
ABS will do its comparison in whatever type x has.
Note that ABS(k++) and the like will do `mysterious' things, and
that on most machines, ABS(largest_negative_integer) is either
a (compile or run)-time trap or simply largest_negative_integer.
>Is this non-ANSI or unportable? It works for me.
It is not part of the proposed ANSI standard, but it is portable.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list