Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Interesting numbers about hashing - 32 bits are clearly not enough

Author: Sune Fischer

Date: 08:54:06 12/07/01

Go up one level in this thread


On December 07, 2001 at 11:20:57, Uri Blass wrote:

>On December 07, 2001 at 11:12:22, Sune Fischer wrote:
>
>>Wups, reverse that, the logic was bad before.
>>
>>char *PrintInt64(long64 x, char *s)
>>{
>>int  MEGA=1000000;
>>long64 a = x/((long64) MEGA);
>>long64 b = x%((long64) MEGA);
>>
>>  if (a==0)
>>  {
>>     sprintf(s,"%d",b);
>>     return(s);
>>  }
>>
>>  if      (b<MEGA/100000)  sprintf(s,"%d00000%d",a,b);
>>  else if (b<MEGA/10000)   sprintf(s,"%d0000%d",a,b);
>>  else if (b<MEGA/1000)    sprintf(s,"%d000%d",a,b);
>>  else if (b<MEGA/100)     sprintf(s,"%d00%d",a,b);
>>  else if (b<MEGA/10)      sprintf(s,"%d0%d",a,b);
>>  else if (b>=MEGA/10)     sprintf(s,"%d%d",a,b);
>>  else sprintf(s,"Error in PrintInt64");
>>
>>  return(s);
>>}
>
>does not work
>long64 is a mistake and when I try to use __int64
>I get wrong ansewer when I try to print 2^31 by this function (I get -2^31)

Me too, apparently sprintf doesn't care about unsigned ???
It totally ignores it, if you print 4 billion by sprintf, it will print -2
billion something???

Anyway, there where other bugs, I corrected to this:

char *PrintInt64(long64 x, char *s)
{
unsigned int GIGA = (unsigned int)  1000000000;
unsigned int a = (unsigned int)(x/((long64) GIGA));
unsigned int b = (unsigned int)(x%((long64) GIGA));

  if (a==0)
  {
     sprintf(s,"%d",b);
     return(s);
  }

  if      (b<GIGA/100000000)  sprintf(s,"%d00000000%d",a,b);
  else if (b<GIGA/10000000)   sprintf(s,"%d0000000%d",a,b);
  else if (b<GIGA/1000000)    sprintf(s,"%d000000%d",a,b);
  else if (b<GIGA/100000)     sprintf(s,"%d00000%d",a,b);
  else if (b<GIGA/10000)      sprintf(s,"%d0000%d",a,b);
  else if (b<GIGA/1000)       sprintf(s,"%d000%d",a,b);
  else if (b<GIGA/100)        sprintf(s,"%d00%d",a,b);
  else if (b<GIGA/10)         sprintf(s,"%d0%d",a,b);
  else if (b>=GIGA/10)        sprintf(s,"%d%d",a,b);
  else sprintf(s,"Error in PrintInt64");

  return(s);
}

its output for the mask[] array:

1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
1125899906842624
2251799813685248
4503599627370496
9007199254740992
18014398509481984
36028797018963968
72057594037927936
144115188075855872
288230376151711744
576460752303423488
1152921504606846976
-1989124287213693952
316718722427387904
633437444854775808

The highest 3 bits can still overflow.
But this is a radix convertion problem you can't escape, if you devide by
GIGA=4200000000 you can't pad with zeros and print decimal by decimal!

>I will use my program that seem to be simpler than it
>I have also there a loop and not a list of if's
>
>Uri

Ok, that is a matter of taste, I prefer this way, I think it is easier to read.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.