Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!lll-lcc!mordor!styx!ames!oliveb!intelca!mipos3!pinkas From: pinkas@mipos3.UUCP (Israel Pinkas) Newsgroups: comp.lang.c Subject: Re: fabs(x) vs. (x) < 0 ? -(x) : (x) Message-ID: <359@mipos3.UUCP> Date: Thu, 8-Jan-87 11:45:37 EST Article-I.D.: mipos3.359 Posted: Thu Jan 8 11:45:37 1987 Date-Received: Fri, 9-Jan-87 01:38:53 EST References: <4477@ut-ngp.UUCP> Reply-To: pinkas@mipos3.UUCP (Israel Pinkas) Organization: Intel, Santa Clara, CA Lines: 29 In article <4477@ut-ngp.UUCP> jjr@ngp.UUCP (Jeff Rodriguez) writes: >What's the difference between > double x, y; > y = fabs(x) >and > #define abs(X) ((X) < 0 ? -(X) : (X)) > double x, y; > y = abs(x); >I.e., why isn't fabs() implemented as a macro? K & R point out (in their discussion of min and max macros) that creating a macro like this would cause X to be evaluted twice. While this might be OK in most cases, consider if you had a function called read_float, which read a float in from stdin. Then the call y = fabs(read_float()) would call read_float() twice if it was implemented as a macro, but only once if it was implemented as a function. Remembering that read_float() reads a new float every time it is called, the macro implementation would use the sign of the first number to determine whether to change the sign of the second number. -Israel -- ---------------------------------------------------------------------- UUCP: {amdcad,decwrl,hplabs,oliveb,pur-ee,qantel}!intelca!mipos3!pinkas ARPA: pinkas%mipos3.intel.com@relay.cs.net CSNET: pinkas%mipos3.intel.com