473,473 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GetHashCode returns the same value for two doubles

Dan
Hi,

I did a test in C#

double x1 = 1.0;
double x2 = 5.29980882362664E-315;
int h1 = x1.GetHashCode();
int h2 = x2.GetHashCode();

It turned out that both h1 and h2 = 1072693248

It is not unique! Is it a defect?
Thanks.

Dan

Nov 17 '05 #1
8 5755
See
http://msdn.microsoft.com/library/de...hcodetopic.asp

In the Remarks it says:

"The default implementation of GetHashCode does not guarantee uniqueness or
consistency; therefore, it must not be used as a unique object identifier
for hashing purposes. Derived classes must override GetHashCode with an
implementation that returns a unique hash code."

So; it's not a "defect" per say.

Shariq
sh****@shariqkhan.com

"Dan" <da*****@canada.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I did a test in C#

double x1 = 1.0;
double x2 = 5.29980882362664E-315;
int h1 = x1.GetHashCode();
int h2 = x2.GetHashCode();

It turned out that both h1 and h2 = 1072693248

It is not unique! Is it a defect?
Thanks.

Dan

Nov 17 '05 #2
Dan <da*****@canada.com> wrote:
I did a test in C#

double x1 = 1.0;
double x2 = 5.29980882362664E-315;
int h1 = x1.GetHashCode();
int h2 = x2.GetHashCode();

It turned out that both h1 and h2 = 1072693248

It is not unique! Is it a defect?


How could all doubles have unique hashcodes? There are more doubles
than there are ints.

Anything which relies on two semantically different values having
different hashcodes is broken.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
hi,
every time it calculates 1072693248 for x1 and x2. but x2 has a special
value.
For instance;
x2+1.0= ?
answer is quite surprising: 1.0
for x2=2.0 it calculates 1073741824
difference it is 1048576=1024*1024
there are 1048576 distinct values. Hashing function simply calculates in
this way...

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

"Dan" <da*****@canada.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I did a test in C#

double x1 = 1.0;
double x2 = 5.29980882362664E-315;
int h1 = x1.GetHashCode();
int h2 = x2.GetHashCode();

It turned out that both h1 and h2 = 1072693248

It is not unique! Is it a defect?
Thanks.

Dan

Nov 17 '05 #4
Yunus Emre ALPÖZEN [MCAD.NET] <ye***@msakademik.net> wrote:
hi,
every time it calculates 1072693248 for x1 and x2. but x2 has a special
value.
For instance;
x2+1.0= ?
answer is quite surprising: 1.0
for x2=2.0 it calculates 1073741824
difference it is 1048576=1024*1024
there are 1048576 distinct values. Hashing function simply calculates in
this way...


That's quite a leap of logic there...

Here's a counterexample - this class just generates random doubles and
checks whether or not the hashcode has already been seen:

using System;
using System.Collections;

class Test
{
static void Main()
{
Hashtable map = new Hashtable();
Random rng = new Random();

int distinct = 0;
while (true)
{
double d = rng.Next();

int hash = d.GetHashCode();
if (!map.ContainsKey(hash))
{
map[hash]=hash;
distinct++;
if (distinct % 1000==0)
{
Console.WriteLine (distinct);
}
}
}
}
}

It doesn't take it long to go over the number of distinct values you
claim.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Could not understand what u mean ???

I made several changes on your code:

using System;
using System.Collections;

class Test
{
static void Main()
{
Hashtable map = new Hashtable();
Random rng = new Random();

int distinct = 0;
int same = 0;
while (true)
{
double d = rng.Next();

int hash = d.GetHashCode();
if (!map.ContainsKey(hash))
{
map[hash]=d;
distinct++;
if (distinct % 1000==0)
{
//Console.WriteLine (distinct);
}
}
else
{
if (((double)map[hash])!=d)
{
same++;
Console.WriteLine("{0}\t{1}",d,map[hash]);
}
}
}
}
}
--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Yunus Emre ALPÖZEN [MCAD.NET] <ye***@msakademik.net> wrote:
hi,
every time it calculates 1072693248 for x1 and x2. but x2 has a special
value.
For instance;
x2+1.0= ?
answer is quite surprising: 1.0
for x2=2.0 it calculates 1073741824
difference it is 1048576=1024*1024
there are 1048576 distinct values. Hashing function simply calculates in
this way...


That's quite a leap of logic there...

Here's a counterexample - this class just generates random doubles and
checks whether or not the hashcode has already been seen:

using System;
using System.Collections;

class Test
{
static void Main()
{
Hashtable map = new Hashtable();
Random rng = new Random();

int distinct = 0;
while (true)
{
double d = rng.Next();

int hash = d.GetHashCode();
if (!map.ContainsKey(hash))
{
map[hash]=hash;
distinct++;
if (distinct % 1000==0)
{
Console.WriteLine (distinct);
}
}
}
}
}

It doesn't take it long to go over the number of distinct values you
claim.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Yunus Emre ALPÖZEN [MCAD.NET] <ye***@msakademik.net> wrote:
Could not understand what u mean ???
You claimed there were 1048576 distinct values returned by
Double.GetHashCode. My program claimed that there are many more than
that - and I would strongly suspect that there are actually as many
values as there are ints, seeing as it would be easily to implement
that way.
I made several changes on your code:


Your changes show that collisions occur, which is unsurprising as I
explained in another post - there are far more doubles available than
ints! I don't see what else your version shows.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Sorry, I misunderstood u. But i said that there are 1048576 distinct values
between 1.0 and 2.0
GetHashCode run as a one way function and maps a double value to another
integer value.

At last, there is no doubt; GetHashCode never returns a unique value. It
operates on value, not memory reference. I wish, GetHashCode returns a
unique value every time by using memory locations and value...
--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Yunus Emre ALPÖZEN [MCAD.NET] <ye***@msakademik.net> wrote:
Could not understand what u mean ???
You claimed there were 1048576 distinct values returned by
Double.GetHashCode. My program claimed that there are many more than
that - and I would strongly suspect that there are actually as many
values as there are ints, seeing as it would be easily to implement
that way.
I made several changes on your code:


Your changes show that collisions occur, which is unsurprising as I
explained in another post - there are far more doubles available than
ints! I don't see what else your version shows.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Yunus Emre ALPÖZEN [MCAD.NET] <ye***@msakademik.net> wrote:
Sorry, I misunderstood u. But i said that there are 1048576 distinct values
between 1.0 and 2.0
Ah - you didn't, but I can accept that you meant to :)

Actually, again there are more distinct values than that, even between
1.0 and 2.0. The following program uses the fact that when you look at
the bit representations of doubles as a long, they're ordered:

using System;
using System.Collections;

class Test
{
static void Main()
{
long d1 = BitConverter.DoubleToInt64Bits(1.0);
long d2 = BitConverter.DoubleToInt64Bits(2.0);

Console.WriteLine (d2-d1);

Hashtable map = new Hashtable();
int distinct = 0;

for (long l = d1; l <= d2; l++)
{
double d = BitConverter.Int64BitsToDouble(l);
int hash = d.GetHashCode();
if (!map.ContainsKey(hash))
{
map[hash]=""; // Avoid double boxing
distinct++;
if ((distinct%1000)==0)
{
Console.WriteLine (distinct);
}
}
}
}
}

The above finds over 10 million distinct hash codes on my box before it
runs out of memory.
GetHashCode run as a one way function and maps a double value to another
integer value.
Yes.
At last, there is no doubt; GetHashCode never returns a unique value.
Not for doubles, no.
It operates on value, not memory reference. I wish, GetHashCode returns a
unique value every time by using memory locations and value...


I'm glad it doesn't, otherwise it would be screwed up by the garbage
collector moving objects around in memory...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Avin Patel | last post by:
Hi I have question for GetHashCode() function, Is it correct in following code or there is more efficient way to implement GetHashCode() function class IntArray public int data public...
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
1
by: MariusI | last post by:
I have some business objects which overrides Equals to provide syntax equality instead of just reference equality. Overriding equals gives me a warning that i must override GetHashcode(). Msdn says...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
3
by: =?Utf-8?B?QWJlUg==?= | last post by:
I am trying to look for change in an instanciated object that contains a generic list<>. I first get an int value of objA.GetHashCode(). I add a few objects to the list<collection in objA I...
5
by: RAM | last post by:
Hello, I would like to ask programmers such question: I have a class containing a few fields: int i; string a; bool b; int j; bool q; decimal d; I need to write GetHashCode function for this...
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
10
by: Tony | last post by:
Hello! Here I have a Card class with several methods. Now to my question I don't really fully understand what purpose this GetHashCode is used for. That's why I bring it up again. In this...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.