Path: utzoo!attcan!uunet!ginosko!uakari.primate.wisc.edu!uflorida!haven!mimsy!chris
From: chris@mimsy.UUCP (Chris Torek)
Newsgroups: comp.lang.c
Subject: Re: ABS for longs and ints
Message-ID: <19898@mimsy.UUCP>
Date: 30 Sep 89 18:02:31 GMT
References: abs, labs, macro, math.h <1381@cipc1.Dayton.NCR.COM>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 27

In article <1381@cipc1.Dayton.NCR.COM> gmaranca@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@mimsy.umd.edu	Path:	uunet!mimsy!chris