Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Comparing fp types for equality

Reply
Thread Tools

Comparing fp types for equality

 
 
Kaz Kylheku
Guest
Posts: n/a
 
      12-20-2011
On 2011-12-20, Rui Maciel <(E-Mail Removed)> wrote:
> Edward Rutherford wrote:
>
>> This code for the comparison of fp types is taken from the C FAQ.
>> Any problems using it in a macro?
>>
>> /* compare 2 doubles for equality */
>> #define DBL_ISEQUAL(a,b) (((fabs)(((a)-(b)))<=(DBL_EPSILON)*(fabs)((a))))
>>
>> Do the same issues involved in comparing 2 fp types for equality
>> apply to comparing a float to zero? E.g. is if(x == 0.)
>> considered harmful?

>
> The macro normalizes the difference between scalar a and scalar b regarding
> scalar a. This may not be acceptable, as, in some border cases, it will
> cause a == b but b != a, and such a binary relation is not an equality
> relation.


It is not an equivalence relation because it is only symmetric and reflexive,
but not transitive: DBL_ISEQUAL(A, B) and DBL_ISEQUAL(B, C) does not imply
DBL_ISEQUAL(A, C). In other words, the relation fails to exhaustively
partition the domain into equivalence classes. I would call this "isnear" not
"isequal".
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      12-20-2011
Eric Sosman wrote:

> Whence came the numbers being compared? From the evaluation of a
> Chebyshev series, or maybe the quotient of a pair of polynomial
> approximations with iterative improvement, or (gasp!) an I/O device?
>
> An old acquaintance used to speak of clearing the beach of bottle
> caps and cigarette butts, so the sand would be nice and clean around
> the whale carcasses. In other words, don't sweat the small stuff.


I see what you mean and it's true that wasting time with small stuff is,
well, wasting time. Nevertheless, being "small stuff" is relative, in the
sense that some stuff may only be considered small when compared to other
stuff which may not be that small. As you've implied, once we compare a
meek implementation of DBL_ISEQUAL() test to complete implementations of
some algorithms, it doesn't make much sense wasting time overanalyzing a
small function. Yet, this implementation of DBL_ISEQUAL() was being
compared with an alternative implementation of the same function, which
means that, in this context, the small stuff may not be that small. In this
case, and when compiling the code to a x86-64 platform, while the straight
forward implementation of the Euclidean norm takes 5 instructions, the
fabs()-based implementation takes 19 instructions. Depending on the
algorithm, the effect of this might range from the irrelevant to the clearly
noticeable, but if we have a choice then we certainly should choose the best
option. After all, just because premature optimization is a bad thing it
doesn't mean that we should intentionally write or use bloated code.


Rui Maciel
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      12-20-2011
Edward Rutherford wrote:

> This code for the comparison of fp types is taken from the C FAQ.
> Any problems using it in a macro?
>
> /* compare 2 doubles for equality */
> #define DBL_ISEQUAL(a,b) (((fabs)(((a)-(b)))<=(DBL_EPSILON)*(fabs)((a))))
>
> Do the same issues involved in comparing 2 fp types for equality
> apply to comparing a float to zero? E.g. is if(x == 0.)
> considered harmful?


I've just noticed a considerable problem with your test macro. If a is zero
then DBL_ISEQUAL(a,b) is only true if b is also zero. This can cause
serious problems, such as infinite loops.


Rui Maciel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hash key types and equality of hash keys Tim McDaniel Perl Misc 2 03-01-2012 06:32 PM
Comparing objects for equality Jens Thoms Toerring Perl Misc 0 05-07-2007 11:25 PM
comparing Locales for equality and degree of specificity tom forsmo Java 2 04-18-2007 02:08 PM
comparing doubles for equality John Smith C Programming 12 01-03-2007 01:14 PM
Comparing two files for equality Edgardo Hames Ruby 11 01-18-2005 05:53 AM



Advertisments