[postgis-devel] strange behavoir of LW_ABS

nicklas.aven at jordogskog.no nicklas.aven at jordogskog.no
Wed Sep 16 05:04:15 PDT 2009


Yes, you are right But is this what is wanted. Why do we have that macro, is it faster than fabs(); From your description Mark I see that putting the -sign outside the parenteses in the macro eliminates this risk:#define LW_ABS(a)   ((a) < (0) ? -(a) : (a)) then it works as I want :-)  Or is this special behavior wanted and why the macro is there? This is no problem now when I have found why (and where) my code broke, but just curious. /Nicklas

2009-09-16 Mark Cave-Ayland wrote:

nicklas.aven at jordogskog.no wrote:
>
>> Hallo
>> 
>> Maybe this is my lack of c-knowledge, but i'm very confused of the 
>> behavior of LW_ABS
>> 
>> The code below I thought would return -1, but i returns -3.
>> 
>> Is this expected behavior?
>> 
>> Why do we have LW_ABS() instead of fabs() ?
>> 
>> 
>> #include
>> #define LW_ABS(a) ((a) < (0) ? (-a) : (a))
>> int main()
>> {
>> int d,e,f;
>> d = 1;
>> e = -2;
>> f = LW_ABS(d+e);
>> printf ("%d\n",f );
>> return 0;
>> }
>> 
>> Thanks
>> Nicklas
>
>It's because #defines are macros which are simply substituted into the 
>code by the pre-processor before the code is compiled. Hence what you 
>actually have is this:
>
>f = LW_ABS(d + e)
>f = ((d + e) < (0) ? (-d + e) : (d + e))
>f = (-1) < (0) ? (-3) : (-1)
>
>Probably an extra pair of parentheses may help here...
>
>
>HTH,
>
>Mark.
>
>-- 
>Mark Cave-Ayland - Senior Technical Architect
>PostgreSQL - PostGIS
>Sirius Corporation plc - control through freedom
>http://www.siriusit.co.uk
>t: +44 870 608 0063
>
>Sirius Labs: http://www.siriusit.co.uk/labs
>_______________________________________________
>postgis-devel mailing list
>postgis-devel at postgis.refractions.net
>http://postgis.refractions.net/mailman/listinfo/postgis-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-devel/attachments/20090916/85e4d12a/attachment.html>


More information about the postgis-devel mailing list