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

guy at gorodish.UUCP guy at gorodish.UUCP
Mon Feb 9 09:52:21 AEST 1987


>	#define	fabs(x) ( (x) & 0x7FFF )
>
>is probably a *lot* faster than your assembly language function,

On my machine, at least, it's incredibly fast, taking no time at all.
This is because using "fabs" on a floating-point value causes the
compiler to reject the code, so you don't waste cycles actually
running the program.

Sorry, but the only way you'd get something like this to work would be to
cast a pointer to the appropriate piece of the floating-point number
to a pointer to some integral type, and dereference that pointer to get
at the appropriate bits and to stuff the result back there.
According to the October 1, 1986 ANSI C draft:

	3.3.10 Bitwise AND Operator

	...

	Constraints

	   Each of the operands shall have integral type.

>Yeah, a lot of it's not exciting.  The standard complaint is that everything
>is really done in double precision, when you can often get by with single.

Fixed in ANSI C, which does not oblige you to perform floating-point
computations in double precision.



More information about the Comp.lang.c mailing list