delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1992/09/30/13:54:35

Date: Wed, 30 Sep 1992 12:10:00 -0500
From: williams AT herky DOT cs DOT uiowa DOT edu (Kent Williams)
To: ESCHN705 AT RZ DOT Braunschweig DOT PTB DOT DBP DOT de DOT BITNET,
wolter AT wrcm2 DOT urz DOT uni-wuppertal DOT de
Subject: Re: abs()olutely hopeless?
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

>
>There are different standard library functions adapted to the special
>types of variables: 
>
>  abs() returns type int, 
>  labs() (not implemented on every system) returns long int,
>  fabs() returns float (sometimes double)
>
>
>You will have to include the correct header files "stdlib.h" or
>"math.h" (besides "stdio.h" of course) to be able to use them.
>
>  Robert Wolter
>  wolter AT wrcm2 DOT urz DOT uni-wuppertal DOT de
>
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.

- Raw text -


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