<html>
<head>
<title></title>
<meta name="GENERATOR" content="MSHTML 8.00.6001.18812"></meta>
</head>
<body>
<div align="left">Yes, you are right</div>
<div align="left"> </div>
<div align="left">But is this what is wanted. Why do we have that macro, is it faster than fabs();</div>
<div align="left"> </div>
<div align="left">From your description Mark I see that putting the -sign outside the parenteses in the macro eliminates this risk:</div>
<div align="left">#define LW_ABS(a) ((a) < (0) ? -(a) : (a))</div>
<div align="left"> </div>
<div align="left">then it works as I want :-) </div>
<div align="left"> </div>
<div align="left">Or is this special behavior wanted and why the macro is there?</div>
<div align="left"> </div>
<div align="left">This is no problem now when I have found why (and where) my code broke, but just curious.</div>
<div align="left"> </div>
<div align="left">/Nicklas<br />
<br />
2009-09-16 Mark Cave-Ayland wrote:<br />
<br />
nicklas.aven@jordogskog.no wrote:<br />
><br />
>> Hallo<br />
>> <br />
>> Maybe this is my lack of c-knowledge, but i'm very confused of the <br />
>> behavior of LW_ABS<br />
>> <br />
>> The code below I thought would return -1, but i returns -3.<br />
>> <br />
>> Is this expected behavior?<br />
>> <br />
>> Why do we have LW_ABS() instead of fabs() ?<br />
>> <br />
>> <br />
>> #include
<stdio.h></stdio.h><br />
>> #define LW_ABS(a) ((a) < (0) ? (-a) : (a))<br />
>> int main()<br />
>> {<br />
>> int d,e,f;<br />
>> d = 1;<br />
>> e = -2;<br />
>> f = LW_ABS(d+e);<br />
>> printf ("%d\n",f );<br />
>> return 0;<br />
>> }<br />
>> <br />
>> Thanks<br />
>> Nicklas<br />
><br />
>It's because #defines are macros which are simply substituted into the <br />
>code by the pre-processor before the code is compiled. Hence what you <br />
>actually have is this:<br />
><br />
>f = LW_ABS(d + e)<br />
>f = ((d + e) < (0) ? (-d + e) : (d + e))<br />
>f = (-1) < (0) ? (-3) : (-1)<br />
><br />
>Probably an extra pair of parentheses may help here...<br />
><br />
><br />
>HTH,<br />
><br />
>Mark.<br />
><br />
>-- <br />
>Mark Cave-Ayland - Senior Technical Architect<br />
>PostgreSQL - PostGIS<br />
>Sirius Corporation plc - control through freedom<br />
>http://www.siriusit.co.uk<br />
>t: +44 870 608 0063<br />
><br />
>Sirius Labs: http://www.siriusit.co.uk/labs<br />
>_______________________________________________<br />
>postgis-devel mailing list<br />
>postgis-devel@postgis.refractions.net<br />
>http://postgis.refractions.net/mailman/listinfo/postgis-devel<br />
><br />
></div>
</body>
</html>