Menu

#1 Numeric Comparator overflows.

open
nobody
None
5
2014-08-15
2006-06-27
Anonymous
No

Hiya, Have just started using SGLIb and like plenty,
one small problem I've had is with the standard numeric
comparator:

#define SGLIB_NUMERIC_COMPARATOR(x, y) ((int)((x) - (y)))

It's easy to overflow, leading to incorrect results
(sometimes) e.g.
printf("%d",SGLIB_NUMERIC_COMPARATOR_OLD(INT_MIN,INT_MAX));

Something like this works better (I think):
#define COMPARATOR(x, y) ((int)((x==y)?0:(x>y)?1:-1))

Discussion

  • Marian Vittek

    Marian Vittek - 2006-07-03

    Logged In: YES
    user_id=53477

    Hi,

    many thanks for reporting the problem. I have considered my
    solution so beautifull and fast and did not think about the
    overflow problem :) I will look for some fast and correct
    definition of this comparator and will change the default
    definition.

    Best regards,

    Marian

     
  • Marian Vittek

    Marian Vittek - 2006-07-03
    • status: open --> closed
     
  • Marian Vittek

    Marian Vittek - 2006-07-03
    • status: closed --> open
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.