Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / June 2005

Tip: Looking for answers? Try searching our database.

CompareTo return values are not consistent.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
howard39@nospam.nospam - 10 Jun 2005 21:20 GMT
It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int, long,
uint, float, double, string, or DateTime types, depending on whether x is
less than, equal to, or greater than y. But when x and y are sbyte, short,
byte, or ushort types, the return value is y-x expressed as a signed integer.

Although this behavior conforms to the definition Of IComparable, it seems
inconsistent. Is there a reason for this?

The code I used to test this is like the following:

short short1 = 5;
short short2 = 10;
Console.WriteLine ("For short, {0} compared to {1} is {2}.", short1, short2,
short1.CompareTo(short2));
           
int int1 = 5;
int int2 = 10;
Console.WriteLine ("For int, {0} compared to {1} is {2}.", int1, int2,
int1.CompareTo(int2));
Jon Skeet [C# MVP] - 10 Jun 2005 22:18 GMT
> It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int, long,
> uint, float, double, string, or DateTime types, depending on whether x is
[quoted text clipped - 3 lines]
> Although this behavior conforms to the definition Of IComparable, it seems
> inconsistent. Is there a reason for this?

I suspect it's the easiest way of computing it. Note that you couldn't
compute it that way for the other types, as you could end up wrapping
round (eg x=-very big number, y=+very big number, x-y=+big number).
This can't happen with the shorter types, due to the range of int
easily coping with subtracting the largest positive short (etc) from
the largest negative one.

Given that having done the subtraction the result is already there, why
bother to convert it to -1/0/1?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

howard39@nospam.nospam - 10 Jun 2005 22:35 GMT
That seems like an implementation-dependent choice. Anyhow, is it clear that
one subtract is faster than one or two compares?

> > It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int, long,
> > uint, float, double, string, or DateTime types, depending on whether x is
[quoted text clipped - 13 lines]
> Given that having done the subtraction the result is already there, why
> bother to convert it to -1/0/1?
Jon Skeet [C# MVP] - 11 Jun 2005 07:50 GMT
> That seems like an implementation-dependent choice.

Absolutely - which is entirely consistent with the interface.

> Anyhow, is it clear that one subtract is faster than one or two compares?

I don't know, but I would guess they've tried it.

I'm afraid I really don't see the problem though. It correctly
implements the interface, so unless someone starts relying on behaviour
which isn't specified in the interface (in which case they're at fault)
there should be no problem.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

cody - 20 Jun 2005 20:40 GMT
You should use Math.Sign(short1.CompareTo(short2)) which will return only
1,0 or -1.

> It seems that x.CompareTo(y) returns -1, 0, or 1 when x and y are int,
> long,
[quoted text clipped - 18 lines]
> Console.WriteLine ("For int, {0} compared to {1} is {2}.", int1, int2,
> int1.CompareTo(int2));

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.