The Negative Powers of Two

The negative powers of two are what happens when you start at 1 and keep dividing by 2. Although normally fractional numbers of this sort can’t exist for the integer type, I can simulate it using an array of decimal digits and my pattern recognition to write the small program below which gives the correct digits.

/*negative powers of two*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
 /*most important variable: number of digits*/
 int length=64;

 char *a;
 int alength=2,x,y,temp;

 a=(char*)malloc(length*sizeof(*a));if(a==NULL){printf("Failed to create array a\n");return(1);}

 x=0;
 while(x<length)
 {
  a[x]=0;
  x++;
 }
 a[0]=1;

 while(alength<length)
 {
  printf("%i.",a[0]);

  x=1;
  while(x<alength)
  {
   printf("%d",a[x]);
   x++;
  }
  printf("\n");

  y=0;
  x=0;
  while(x<length)
  {
   if( (a[x]&1)==1 ){temp=5;}else{temp=0;} 
   a[x]>>=1;
   a[x]+=y;
   y=temp;
   x++;
  }
  if(a[alength]>0){alength++;}

 }

 if(a!=NULL){free(a); a=NULL;}

 return 0;
}

The output of the above program is:

1.0
0.5
0.25
0.125
0.0625
0.03125
0.015625
0.0078125
0.00390625
0.001953125
0.0009765625
0.00048828125
0.000244140625
0.0001220703125
0.00006103515625
0.000030517578125
0.0000152587890625
0.00000762939453125
0.000003814697265625
0.0000019073486328125
0.00000095367431640625
0.000000476837158203125
0.0000002384185791015625
0.00000011920928955078125
0.000000059604644775390625
0.0000000298023223876953125
0.00000001490116119384765625
0.000000007450580596923828125
0.0000000037252902984619140625
0.00000000186264514923095703125
0.000000000931322574615478515625
0.0000000004656612873077392578125
0.00000000023283064365386962890625
0.000000000116415321826934814453125
0.0000000000582076609134674072265625
0.00000000002910383045673370361328125
0.000000000014551915228366851806640625
0.0000000000072759576141834259033203125
0.00000000000363797880709171295166015625
0.000000000001818989403545856475830078125
0.0000000000009094947017729282379150390625
0.00000000000045474735088646411895751953125
0.000000000000227373675443232059478759765625
0.0000000000001136868377216160297393798828125
0.00000000000005684341886080801486968994140625
0.000000000000028421709430404007434844970703125
0.0000000000000142108547152020037174224853515625
0.00000000000000710542735760100185871124267578125
0.000000000000003552713678800500929355621337890625
0.0000000000000017763568394002504646778106689453125
0.00000000000000088817841970012523233890533447265625
0.000000000000000444089209850062616169452667236328125
0.0000000000000002220446049250313080847263336181640625
0.00000000000000011102230246251565404236316680908203125
0.000000000000000055511151231257827021181583404541015625
0.0000000000000000277555756156289135105907917022705078125
0.00000000000000001387778780781445675529539585113525390625
0.000000000000000006938893903907228377647697925567626953125
0.0000000000000000034694469519536141888238489627838134765625
0.00000000000000000173472347597680709441192448139190673828125
0.000000000000000000867361737988403547205962240695953369140625
0.0000000000000000004336808689942017736029811203479766845703125
0.00000000000000000021684043449710088680149056017398834228515625

I know this post is not exactly Chess related but I am thinking of using this site to double as a way to share some of my computer programming code too. I also may be able to create mathematical data structures to represent the game of Chess in some way.

I am writing a book about Chess and so I may sometimes share cool programs like this that are heavily math related. My obsession with numbers is how I got my start in programming originally.

Comments

Please leave me any comments or questions you have! I will update posts if necessary based on user feedback!