fabs(x) vs. (x) < 0 ? -(x) : (x)

ark at alice.UUCP ark at alice.UUCP
Sun Jan 11 10:41:55 AEST 1987


If you were doing it in C++, you could say:

	inline double fabs (double x)
		{ return x < 0? -x: x; }

and it would do the right thing, efficiently.  Moreover, you could say:

	overload abs;

	inline double abs (double x)
		{ return x < 0? -x: x; }
	inline long abs (long x)
		{ return x < 0? -x: x; }
	inline int abs (int x)
		{ return x < 0? -x: x; }
	inline float abs (float x)
		{ return x < 0? -x: x; }

and not have to remember the type of your arguments.  Generalizing
the second example above to an unbounded family of types is left
as an exercise for the reader.



More information about the Comp.lang.c mailing list