Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6128 in orxonox.OLD


Ignore:
Timestamp:
Dec 16, 2005, 1:07:54 PM (18 years ago)
Author:
bwuest
Message:

A function added in class converter.

Location:
branches/network/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/lib/network/converter.cc

    r6108 r6128  
    107107  return result;
    108108}
     109
     110/*char* Converter::floatToBinString(float x)
     111{
     112  char* result = new char[200];
     113  int pos = 0;
     114 
     115  int h = (int)x;
     116  if (h > x)
     117    h--;
     118  x -= h;
     119 
     120 
     121  while (h > 0 && pos < 200)
     122  {
     123    //printf("%i ", pos);
     124   
     125    if (h % 2 == 1)
     126    {
     127      result[pos] = '1';
     128      h -= 1;
     129    }
     130    else
     131      result[pos] = '0';
     132    pos++;
     133    h /= 2;
     134  }
     135 
     136  //printf("x = %f\n", x);
     137 
     138 
     139  //invert
     140  for (int i = 0; i < pos / 2; i++)
     141  {
     142    char temp = result[i];
     143    result[i] = result[pos - 1 - i];
     144    result[pos - 1 - i] = temp;
     145  }
     146 
     147 
     148  result[pos++] = '.';
     149  float sub = 0.5;
     150  while (x > 0 && pos < 200)
     151  {
     152    //printf("%i ", pos);
     153   
     154    if (x >= sub)
     155    {
     156      result[pos] = '1';
     157      x -= sub;
     158    }
     159    else
     160      result[pos] = '0';
     161    pos++;
     162    sub /= 2;
     163  }
     164 
     165 
     166  return result;
     167}*/
     168
     169char* Converter::floatToBinString(float x)
     170{
     171  char* result = new char[200];
     172  int pos = 0;
     173 
     174  float sub = 1;
     175  while (sub < x)
     176    sub *= 2;
     177 
     178  //printf("sub = %f\n", sub);
     179 
     180  //while (sub >= 1 && pos < 200)
     181  while ((x > 0 || sub >= 1) && pos < 200)
     182  {
     183    if (x >= sub)
     184    {
     185      result[pos] = '1';
     186      x -= sub;
     187    }
     188    else
     189      result[pos] = '0';
     190    pos++;
     191    sub /= 2;
     192   
     193    if (sub == 0.5f)
     194      result[pos++] = '.';
     195  }
     196 
     197  /*result[pos++] = '.';
     198  sub = 0.5;
     199  while (x > 0 && pos < 200)
     200  {
     201    if (x >= sub)
     202    {
     203      result[pos] = '1';
     204      x -= sub;
     205    }
     206    else
     207      result[pos] = '0';
     208    pos++;
     209    sub /= 2;
     210  }
     211  */
     212 
     213  return result;
     214}
  • branches/network/src/lib/network/converter.h

    r6106 r6128  
    2525    static int byteArrayToInt(byte* a);
    2626   
    27     //byte* floatToByteArray(int x);
    28     //int byteArrayToFloat(byte[] a);
     27    //byte* floatToByteArray(float x);
     28    //float byteArrayToFloat(byte[] a);
     29   
     30    //Test
     31    static char* floatToBinString(float x);
    2932   
    3033  private:
  • branches/network/src/subprojects/network/network_unit_test.cc

    r6108 r6128  
    299299
    300300 
     301  //float a = 5.4f;
     302  //float b = 2.0f;
     303  //printf("%f mod %f = %f", a, b, a % b);
     304 
     305  printf("\n");
     306 
     307  float y;
     308  char* s;
     309 
     310  y = 12.0f;
     311  s = Converter::floatToBinString(y);
     312  printf("%f = ", y);
     313  printf(s); printf("\n");
     314 
     315  y = 24549026.0f;
     316  s = Converter::floatToBinString(y);
     317  printf("%f = ", y);
     318  printf(s); printf("\n");
     319 
     320  y = 12.4e20f;
     321  s = Converter::floatToBinString(y);
     322  printf("%f = ", y);
     323  printf(s); printf("\n");
     324 
     325  y = 4.7824f;
     326  s = Converter::floatToBinString(y);
     327  printf("%f = ", y);
     328  printf(s); printf("\n");
     329 
     330 
     331 
    301332  return 0;
    302333}
Note: See TracChangeset for help on using the changeset viewer.