bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: proposed ARITH_COMPARE macro


From: Bruno Haible
Subject: Re: proposed ARITH_COMPARE macro
Date: Mon, 16 May 2011 02:32:45 +0200
User-agent: KMail/1.9.9

Hi Paul,

> While we're on the topic of C's arithmetic deficiencies, how about if
> we also add a macro for safe and portable comparison?

Yes, absolutely! It's a deficiency in the C standard libraries that there
are no library functions for doing that.

> Something like "ARITH_COMPARE (A, OP, B)" to compare A and B
> numerically rather than using C's rules.  So, for example,
> ARITH_COMPARE (-1, <, sizeof "x") would return true even though
> typically (-1 > sizeof "x") in C, and
> ARITH_COMPARE ((float) LONG_MAX, <, LONG_MAX) even though typically
> (float) LONG_MAX == LONG_MAX in C.

Can you do this purely with macros? I think we should start from a
function, or a set of functions (depending on the input types).

Find attached some code for a beginning. Caveats:
  - For the integers it assumes a two's complement representation. 
  - For the floating-point type it assumes a representation with a fixed
    number of mantissa bits (*not* the 'long double' representation of IRIX
    and PowerPC that consists of two 'double's).

> This new macro could be in a new arith-compare module, or perhaps
> we could stretch things a bit and put it into intprops, as all of
> the deficiencies it's correcting are related to integers.

'arith-compare' sounds fine. Yes, I wouldn't put floating-point related
stuff into 'intprops', because then 'intprops' will soon need to depend on
the 'float' module.

Bruno
-- 
In memoriam Dang Xuan Hoan <http://www.vietmemorial.org/myweb/thelist.html>

Attachment: arith-compare.c
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]