incubator-stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: Question about comparing floats and doubles for equality
Date Thu, 05 Apr 2007 22:23:28 GMT
David Ritter wrote:
> In reviewing the following file for some information on how to compare
> floats and doubles for equality I had some questions I wanted to ask the
> list.
> 
> http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/src/valcmp.cpp
> ?view=markup
> 
> I found the methods rw_fltcmp and rw_dblcmp that are implemented in
> valcmp.cpp but I have a few questions about them.
> 
> In rw_fltcmp() the method starts off with the following block of code:
> 
> #if _RWSTD_SHRT_SIZE == _RWSTD_INT_SIZE
>     typedef short IntT;
>     const IntT imin = _RWSTD_SHRT_MIN;
> #elif _RWSTD_FLT_SIZE == _RWSTD_INT_SIZE
>     typedef int IntT;
>     const IntT imin = _RWSTD_INT_MIN;
> #elif _RWSTD_FLT_SIZE == _RWSTD_LONG_SIZE
>     typedef long IntT;
>     const IntT imin = _RWSTD_LONG_MIN;
> #elif _RWSTD_FLT_SIZE == _RWSTD_LLONG_SIZE
>     typedef _RWSTD_LONG_LONG IntT;
>     const IntT imin = _RWSTD_LLONG_MIN;
> #else
> 
> Why is the size of a short being compared to the size of an int when all
> the other comparisons are made against float?  As a side note, that
> first check seems to be missing from rw_dblcmp, is it unnecessary there?

I suspect the first conditional has a couple of typos and should
instead read: #if _RWSTD_FLT_SIZE == _RWSTD_SHRT_SIZE. Let me fix
it. The rw_dblcmp case makes the assumption that doubles are at
least as big as ints so it doesn't bother to check short. The
same assumption would probably be equally as safe in rf_fltcmp
(i.e., it's unlikely that sizeof(short) == sizeof(float)).

> 
> My other question relates to rw_dblcmp.  In the method there appears to
> be two different ways of checking the equality, depending on whether the
> size of a long long is less than the size of a double.  Is that a
> performance optimization?

No, it's the next best thing to the integer hack in the absence
of a integer type that's as wide as long double :) It might be
possible to use the integer trick for long doubles that are so
close that they differ only in their low words and their high
words are the same.

Martin

PS The integer hack assumes IEEE 754 format of floating point
values. It most likely won't work with any other format (such
as the IBM OS/360 hex format).

Mime
View raw message