get rid of Abs()

Started by Peter Eisentrautover 3 years ago3 messageshackers
Jump to latest
#1Peter Eisentraut
peter_e@gmx.net

I was wondering why we have a definition of Abs() in c.h when there are
more standard functions such as abs() and fabs() in widespread use. I
think this one is left over from pre-ANSI-C days. The attached patches
replace all uses of Abs() with more standard functions.

The first patch installs uses of abs() and fabs(). These are already in
use in the tree and should be straightforward.

The next two patches install uses of llabs() and fabsf(), which are not
in use yet. But they are in C99.

The last patch removes the definition of Abs().

Fun fact: The current definition

#define Abs(x) ((x) >= 0 ? (x) : -(x))

is slightly wrong for floating-point values. Abs(-0.0) returns -0.0,
but fabs(-0.0) returns +0.0.

Attachments:

0001-Remove-unnecessary-uses-of-Abs.patchtext/plain; charset=UTF-8; name=0001-Remove-unnecessary-uses-of-Abs.patchDownload+54-50
0002-Use-llabs-instead-of-Abs-where-appropriate.patchtext/plain; charset=UTF-8; name=0002-Use-llabs-instead-of-Abs-where-appropriate.patchDownload+7-8
0003-Use-fabsf-instead-of-Abs-where-appropriate.patchtext/plain; charset=UTF-8; name=0003-Use-fabsf-instead-of-Abs-where-appropriate.patchDownload+5-5
0004-Remove-Abs.patchtext/plain; charset=UTF-8; name=0004-Remove-Abs.patchDownload+0-7
#2Zhang Mingli
zmlpostgres@gmail.com
In reply to: Peter Eisentraut (#1)
Re: get rid of Abs()

Hi,

On Oct 4, 2022, 15:07 +0800, Peter Eisentraut <peter.eisentraut@enterprisedb.com>, wrote:

I was wondering why we have a definition of Abs() in c.h when there are
more standard functions such as abs() and fabs() in widespread use. I
think this one is left over from pre-ANSI-C days. The attached patches
replace all uses of Abs() with more standard functions.

The first patch installs uses of abs() and fabs(). These are already in
use in the tree and should be straightforward.

The next two patches install uses of llabs() and fabsf(), which are not
in use yet. But they are in C99.

The last patch removes the definition of Abs().

Fun fact: The current definition

#define Abs(x) ((x) >= 0 ? (x) : -(x))

is slightly wrong for floating-point values. Abs(-0.0) returns -0.0,
but fabs(-0.0) returns +0.0.

+1,

Like patch3, also found some places where could use fabsf instead of fabs if possible, add a patch to replace them.

Regards,
Zhang Mingli

Attachments:

v2-0005-replace-fabs-with-fabsf-if-possible.patchapplication/octet-streamDownload+3-4
#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#1)
Re: get rid of Abs()

Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:

I was wondering why we have a definition of Abs() in c.h when there are
more standard functions such as abs() and fabs() in widespread use. I
think this one is left over from pre-ANSI-C days. The attached patches
replace all uses of Abs() with more standard functions.

I'm not in favor of the llabs() changes. I think what we really want
in those places, or at least most of them, is "abs() for int64".
That could be had by #define'ing "iabs64" (or some name along that
line) as labs or llabs depending on which type we are using for int64.

Seems OK beyond that nitpick.

regards, tom lane