Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6564 in orxonox.OLD


Ignore:
Timestamp:
Jan 18, 2006, 3:05:09 PM (18 years ago)
Author:
bwuest
Message:

Converter.cc changed: float converter should work now except for numbers with absolute value less than 10(-30) and ± infinity

Location:
branches/network/src
Files:
2 added
6 edited

Legend:

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

    r6341 r6564  
    2222/* include your own header */
    2323#include "converter.h"
     24#include "shell_command.h"
     25
     26#include <limits>
     27
     28SHELL_COMMAND_STATIC(debug, Converter, Converter::debug);
    2429
    2530
     
    182187const int expmult = 8388608; //2^23
    183188
     189float Converter::getDenormConst()
     190{
     191  const int exp = 126;
     192  float result = 1.0f;
     193  for (int i = 0; i < exp; i++)
     194    result /= 2.0f;
     195  return result;
     196}
     197
    184198/*!
    185199 * Converts a float value into a byte-array
     
    189203byte* Converter::floatToByteArray(float x)
    190204{
     205  byte* result = new byte[4];
     206  floatToByteArray(x, result, 4);
     207  return result;
     208  /*
    191209  int mantisse = 0;
    192210  int exponent = 128;
     
    201219    sgn = 1;
    202220
    203   float sub = 1;
    204   while (sub < x)
    205   {
    206     sub *= 2;
     221  if (x == 0)
     222  {
     223    exponent = 255;
     224    mantisse = 0;
     225  }
     226  else
     227  {
     228    //if (x < getDenormConst())
     229    //  printf("Denormalisiert!\n");
     230    //printf("DenormConst = %e", getDenormConst());
     231   
     232    float sub = 1;
     233    while (sub < x)
     234    {
     235      sub *= 2;
     236      exponent++;
     237    }
     238
     239    while (x > 0)
     240    {
     241      if (x >= sub)
     242      {
     243        mantisse += 1;
     244        x -= sub;
     245      }
     246
     247      mantisse *= 2;
     248      exponent--;
     249      sub /= 2;
     250    }
    207251    exponent++;
    208   }
    209 
    210   while (x > 0)
    211   {
    212     if (x >= sub)
    213     {
    214       mantisse += 1;
    215       x -= sub;
    216     }
    217 
    218     mantisse *= 2;
    219     exponent--;
    220     sub /= 2;
    221   }
    222   exponent++;
    223   mantisse /= 2;
    224   while (mantisse < expmult)
    225   {
    226     mantisse *= 2;
    227     exponent--;
    228   }
    229 
    230   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
    231 
    232   mantisse -= expmult;
     252    mantisse /= 2;
     253   
     254    printf("Conv:        mantisse = %i exponent = %i \n", mantisse, exponent);
     255
     256   
     257    if (mantisse != 0)
     258    {
     259      while (mantisse < expmult)
     260      {
     261        mantisse *= 2;
     262        exponent--;
     263      }
     264   
     265      mantisse -= expmult;
     266    }
     267  }
     268
     269  printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent);
     270
    233271
    234272  int hx = mantisse + expmult * exponent;
     
    238276
    239277  return result;
     278  */
    240279}
    241280
     
    248287float Converter::byteArrayToFloat(byte* a)
    249288{
     289  byte* h = new byte[4];
     290  float result = 0.0f;
     291  byteArrayToFloat(a, &result);
     292  return result;
     293  /*
     294  int hexp = a[2] + a[3] * 256;
     295  int exponent = (hexp / 128) % 256;
     296 
    250297  int hmant = a[0] + a[1] * 256 + a[2] * 65536;
    251298  int mantisse = hmant % expmult;
     299  if (mantisse == 0 && exponent == 255)
     300    return 0;
     301 
    252302  mantisse += expmult;
    253 
    254   int hexp = a[2] + a[3] * 256;
    255   int exponent = (hexp / 128) % 256 - 128;
     303  exponent -= 128;
     304
    256305
    257306  int sgn;
     
    261310    sgn = 1;
    262311
    263   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
     312  printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent);
    264313
    265314  float emult = 1;
     
    276325
    277326  return result;
    278 }
    279 
    280 /*!
    281  * Converts a float value into a byte-array
    282  * @param x: The float which is to convert
    283  * @return: A byte-array which accords the given float
    284  */
    285 byte* Converter::_floatToByteArray(float x)
    286 {
    287   byte* p = (byte*)&x;
    288   byte* result = new byte[4];
    289   for (int i = 0; i < 4; i++)
    290     result[i] = p[i];
    291   return result;
    292 }
    293 
    294 
    295 /*!
    296  * Converts a byte-array into a float value
    297  * @param a: The byte-array which is to convert
    298  * @return: A float value which accords the given byte-array
    299  */
    300 float Converter::_byteArrayToFloat(byte* a)
    301 {
    302   float* p = (float*)a;
    303   float result = *p;
    304   return result;
     327  */
    305328}
    306329
     
    319342
    320343  //handle 0 else function will loop for ever
    321   if ( x == 0 )
     344  /*if ( x == 0 )
    322345  {
    323346    for ( int i = 0; i<FLOATSIZE; i++)
    324347      a[i] = 0;
    325348    return FLOATSIZE;
    326   }
     349}*/
    327350
    328351  int mantisse = 0;
     
    338361    sgn = 1;
    339362
    340   float sub = 1;
    341   while (sub < x)
    342   {
    343     sub *= 2;
     363  if (x == 0)
     364  {
     365    exponent = 255;
     366    mantisse = 0;
     367  }
     368  else
     369  {
     370    //if (x < getDenormConst())
     371    //  printf("Denormalisiert!\n");
     372    //printf("DenormConst = %e", getDenormConst());
     373   
     374    float sub = 1;
     375    while (sub < x)
     376    {
     377      sub *= 2;
     378      exponent++;
     379    }
     380
     381    while (x > 0)
     382    {
     383      if (x >= sub)
     384      {
     385        mantisse += 1;
     386        x -= sub;
     387      }
     388
     389      mantisse *= 2;
     390      exponent--;
     391      sub /= 2;
     392    }
    344393    exponent++;
    345   }
    346 
    347   while (x > 0)
    348   {
    349     if (x >= sub)
    350     {
    351       mantisse += 1;
    352       x -= sub;
    353     }
    354 
    355     mantisse *= 2;
    356     exponent--;
    357     sub /= 2;
    358   }
    359   exponent++;
    360   mantisse /= 2;
    361   while (mantisse < expmult)
    362   {
    363     mantisse *= 2;
    364     exponent--;
    365   }
    366 
    367   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
    368 
    369   mantisse -= expmult;
    370 
     394    mantisse /= 2;
     395   
     396
     397///    printf("Conv:        mantisse = %i exponent = %i \n", mantisse, exponent);
     398
     399   
     400    if (mantisse != 0)
     401    {
     402      while (mantisse < expmult)
     403      {
     404        mantisse *= 2;
     405        exponent--;
     406      }
     407   
     408      mantisse -= expmult;
     409    }
     410  }
     411
     412///  printf("Conv: mantisse = %i exponent = %i \n", mantisse, exponent);
     413
     414 
    371415  int hx = mantisse + expmult * exponent;
    372416  int result = intToByteArray(hx, a, length);
     
    374418    a[3] += sgnadd;
    375419
     420
     421//  int hx = mantisse + expmult * exponent;
     422//  byte* result = intToByteArray(hx);
     423//  if (sgn == -1)
     424//    result[3] += sgnadd;
     425
    376426  return result;
    377427}
     
    385435int Converter::byteArrayToFloat(const byte* a, float* x)
    386436{
    387   //handle 0
    388   for (int i = 0; i<FLOATSIZE; i++)
     437    //handle 0
     438  /*for (int i = 0; i<FLOATSIZE; i++)
    389439  {
    390440    if (a[i]!=0)
     
    395445      return FLOATSIZE;
    396446    }
    397   }
    398 
    399 
     447}*/
     448
     449  int hexp = a[2] + a[3] * 256;
     450  int exponent = (hexp / 128) % 256;
     451 
    400452  int hmant = a[0] + a[1] * 256 + a[2] * 65536;
    401453  int mantisse = hmant % expmult;
     454 
     455  //handle 0
     456  if (mantisse == 0 && exponent == 255)
     457    return 0;
     458 
    402459  mantisse += expmult;
    403 
    404   int hexp = a[2] + a[3] * 256;
    405   int exponent = (hexp / 128) % 256 - 128;
     460  exponent -= 128;
     461
    406462
    407463  int sgn;
     
    411467    sgn = 1;
    412468
    413   //printf("mantisse = %i exponent = %i \n", mantisse, exponent);
     469///  printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent);
    414470
    415471  float emult = 1;
     
    421477      emult /= 2;
    422478
     479  /*
     480  float result = mantisse * emult;
     481  if (sgn == -1)
     482    result = -result;
     483
     484  return result;
     485  */
     486
    423487  *x = mantisse * emult;
    424488  if (sgn == -1)
     
    426490
    427491  return FLOATSIZE;
     492}
     493
     494/*!
     495 * Converts a float value into a byte-array
     496 * @param x: The float which is to convert
     497 * @return: A byte-array which accords the given float
     498 */
     499byte* Converter::_floatToByteArray(float x)
     500{
     501  byte* p = (byte*)&x;
     502  byte* result = new byte[4];
     503  for (int i = 0; i < 4; i++)
     504    result[i] = p[i];
     505  return result;
     506}
     507
     508
     509/*!
     510 * Converts a byte-array into a float value
     511 * @param a: The byte-array which is to convert
     512 * @return: A float value which accords the given byte-array
     513 */
     514float Converter::_byteArrayToFloat(byte* a)
     515{
     516  float* p = (float*)a;
     517  float result = *p;
     518  return result;
    428519}
    429520
     
    504595}
    505596
     597
     598
     599
     600void Converter::floatTest(float x)
     601{
     602  //float x = 8.0f;
     603  //float x = numeric_limits<float>::infinity();
     604 
     605  printf("To Convert: %e\n", x);
     606 
     607  byte* res = floatToByteArray(x);
     608  for (int i = 0; i < 4; i++)
     609    printf("%i ", res[i]);
     610  printf("\n");
     611 
     612  float y = byteArrayToFloat(res);
     613  printf("ReConvert: %e\n", y);
     614 
     615  if (x == y)
     616    printf("equal\n");
     617  else
     618    printf("different\n");
     619}
     620
     621void Converter::debug()
     622{
     623  printf("We rulez\n");
     624 
     625  //Denormalized?
     626  //floatTest(-9.87624e-38f);
     627  //floatTest(-9.87624e-37f);
     628  //floatTest(-9.87624e-36f);
     629  //floatTest(-9.87624e-35f);
     630 
     631  /*
     632  floatTest(14.7f);
     633  floatTest(12.07e15f);
     634  floatTest(0.0f);
     635  floatTest(5.67e-15f);
     636  */
     637 
     638  //floatTest(-18.0098f);
     639  //floatTest(-24.07e23f);
     640  //floatTest(-0.0f);
     641  //floatTest(-5.67e-29f);
     642  floatTest(-45.7e-32f);
     643  floatTest(45.7e-33f);
     644  floatTest(-45.7e-34f);
     645  floatTest(45.7e-35f);
     646}
  • branches/network/src/lib/network/converter.h

    r6341 r6564  
    99/* include this file, it contains some default definitions */
    1010#include "netdefs.h"
     11
    1112
    1213/* include base_object.h since all classes are derived from this one */
     
    2122 * a class that can convert int to byte-array and vice versa
    2223 */
    23 class Converter: public BaseObject
     24class Converter : public BaseObject
    2425{
    2526  public:
     
    4546    static byte* _floatToByteArray(float x);
    4647    static float _byteArrayToFloat(byte* a);
     48   
     49   
     50    static void debug();
     51    static void floatTest(float x);
     52    static float getDenormConst();
     53 
     54   
    4755  private:
    4856    Converter();
     
    5058};
    5159
     60#undef byte
     61
    5262#endif /*_CONVERTER*/
  • branches/network/src/subprojects/network/Makefile.am

    r6341 r6564  
    2727                  $(MAINSRCDIR)/lib/util/helper_functions.cc \
    2828                  $(MAINSRCDIR)/lib/util/executor/executor.cc \
    29                   $(MAINSRCDIR)/lib/util/multi_type.cc
     29                  $(MAINSRCDIR)/lib/util/multi_type.cc \
     30                  \
     31                  \
     32                  $(MAINSRCDIR)/lib/coord/p_node.cc        \
     33                  $(MAINSRCDIR)/world_entities/world_entity.cc
     34                 
    3035
    3136
  • branches/network/src/subprojects/network/read_sync.cc

    r6341 r6564  
    2929 */
    3030ReadSync::ReadSync(const char* name)
    31   : Synchronizeable(name)
     31  : Synchronizeable()
    3232{
    3333  /* define the local buffer size */
  • branches/network/src/subprojects/network/simple_sync.cc

    r6341 r6564  
    2929 */
    3030SimpleSync::SimpleSync(const char* name)
    31   : Synchronizeable(name)
     31  : Synchronizeable()
    3232{
    3333  /* define the local buffer size */
  • branches/network/src/subprojects/network/write_sync.cc

    r6341 r6564  
    2929 */
    3030WriteSync::WriteSync(const char* name)
    31   : Synchronizeable(name)
     31  : Synchronizeable()
    3232{
    3333  /* define the local buffer size */
Note: See TracChangeset for help on using the changeset viewer.