delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1992/09/30/14:57:49

From: Eric Backus <ericb AT lsid DOT hp DOT com>
Subject: Re: abs()olutely hopeless?
To: djgpp AT sun DOT soe DOT clarkson DOT edu (djgpp)
Date: Wed, 30 Sep 92 11:45:19 PDT
Mailer: Elm [revision: 66.25]

> ABS works better as a macro -- then the compiler can determine type
> dynamically.
> 
> #define ABS(x) ((x) < 0 ? -x : x)
> 
> should work, unless I'm missing some obscure ANSI type promotion problem. If
> you want to get fancy, you can use typeof with GCC to coerce 0 to the proper
> type, but I'm not at all sure it's necessary.

It shouldn't be necessary.  But you should enclose ALL the x's in
parentheses, and you this will still evaluate x twice if it's an
expression.
--
				Eric Backus
				ericb%hplsla AT hplabs DOT hp DOT com
				(206) 335-2495

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019