Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!zaphod.mps.ohio-state.edu!van-bc!ubc-cs!uw-beaver!uw-june!ka From: ka@cs.washington.edu (Kenneth Almquist) Newsgroups: comp.lang.c Subject: Computing the absolute value of an integer Message-ID: <11679@june.cs.washington.edu> Date: 3 May 90 05:07:40 GMT Organization: U of Washington, Computer Science, Seattle Lines: 17 I want a function that will compute the absolute value of any integer that can be stored in a variable of type int. What makes this difficult is that on some machines the absolute value of the most negative integer will not fit in an int. The following solution works on all machines I know of: unsigned my_abs(int a) { if (a >= 0) return a; else return -a; } Does anyone know of machines on which this will fail? Can anyone suggest a more portable implementation? Kenneth Almquist