Changeset 6564 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- Jan 18, 2006, 3:05:09 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/converter.cc
r6341 r6564 22 22 /* include your own header */ 23 23 #include "converter.h" 24 #include "shell_command.h" 25 26 #include <limits> 27 28 SHELL_COMMAND_STATIC(debug, Converter, Converter::debug); 24 29 25 30 … … 182 187 const int expmult = 8388608; //2^23 183 188 189 float 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 184 198 /*! 185 199 * Converts a float value into a byte-array … … 189 203 byte* Converter::floatToByteArray(float x) 190 204 { 205 byte* result = new byte[4]; 206 floatToByteArray(x, result, 4); 207 return result; 208 /* 191 209 int mantisse = 0; 192 210 int exponent = 128; … … 201 219 sgn = 1; 202 220 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 } 207 251 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 233 271 234 272 int hx = mantisse + expmult * exponent; … … 238 276 239 277 return result; 278 */ 240 279 } 241 280 … … 248 287 float Converter::byteArrayToFloat(byte* a) 249 288 { 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 250 297 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 251 298 int mantisse = hmant % expmult; 299 if (mantisse == 0 && exponent == 255) 300 return 0; 301 252 302 mantisse += expmult; 253 254 int hexp = a[2] + a[3] * 256; 255 int exponent = (hexp / 128) % 256 - 128; 303 exponent -= 128; 304 256 305 257 306 int sgn; … … 261 310 sgn = 1; 262 311 263 //printf("mantisse = %i exponent = %i \n", mantisse, exponent);312 printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent); 264 313 265 314 float emult = 1; … … 276 325 277 326 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 */ 305 328 } 306 329 … … 319 342 320 343 //handle 0 else function will loop for ever 321 if ( x == 0 )344 /*if ( x == 0 ) 322 345 { 323 346 for ( int i = 0; i<FLOATSIZE; i++) 324 347 a[i] = 0; 325 348 return FLOATSIZE; 326 } 349 }*/ 327 350 328 351 int mantisse = 0; … … 338 361 sgn = 1; 339 362 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 } 344 393 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 371 415 int hx = mantisse + expmult * exponent; 372 416 int result = intToByteArray(hx, a, length); … … 374 418 a[3] += sgnadd; 375 419 420 421 // int hx = mantisse + expmult * exponent; 422 // byte* result = intToByteArray(hx); 423 // if (sgn == -1) 424 // result[3] += sgnadd; 425 376 426 return result; 377 427 } … … 385 435 int Converter::byteArrayToFloat(const byte* a, float* x) 386 436 { 387 //handle 0388 for (int i = 0; i<FLOATSIZE; i++)437 //handle 0 438 /*for (int i = 0; i<FLOATSIZE; i++) 389 439 { 390 440 if (a[i]!=0) … … 395 445 return FLOATSIZE; 396 446 } 397 } 398 399 447 }*/ 448 449 int hexp = a[2] + a[3] * 256; 450 int exponent = (hexp / 128) % 256; 451 400 452 int hmant = a[0] + a[1] * 256 + a[2] * 65536; 401 453 int mantisse = hmant % expmult; 454 455 //handle 0 456 if (mantisse == 0 && exponent == 255) 457 return 0; 458 402 459 mantisse += expmult; 403 404 int hexp = a[2] + a[3] * 256; 405 int exponent = (hexp / 128) % 256 - 128; 460 exponent -= 128; 461 406 462 407 463 int sgn; … … 411 467 sgn = 1; 412 468 413 //printf("mantisse = %i exponent = %i \n", mantisse, exponent);469 /// printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent); 414 470 415 471 float emult = 1; … … 421 477 emult /= 2; 422 478 479 /* 480 float result = mantisse * emult; 481 if (sgn == -1) 482 result = -result; 483 484 return result; 485 */ 486 423 487 *x = mantisse * emult; 424 488 if (sgn == -1) … … 426 490 427 491 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 */ 499 byte* 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 */ 514 float Converter::_byteArrayToFloat(byte* a) 515 { 516 float* p = (float*)a; 517 float result = *p; 518 return result; 428 519 } 429 520 … … 504 595 } 505 596 597 598 599 600 void 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 621 void 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 9 9 /* include this file, it contains some default definitions */ 10 10 #include "netdefs.h" 11 11 12 12 13 /* include base_object.h since all classes are derived from this one */ … … 21 22 * a class that can convert int to byte-array and vice versa 22 23 */ 23 class Converter : public BaseObject24 class Converter : public BaseObject 24 25 { 25 26 public: … … 45 46 static byte* _floatToByteArray(float x); 46 47 static float _byteArrayToFloat(byte* a); 48 49 50 static void debug(); 51 static void floatTest(float x); 52 static float getDenormConst(); 53 54 47 55 private: 48 56 Converter(); … … 50 58 }; 51 59 60 #undef byte 61 52 62 #endif /*_CONVERTER*/
Note: See TracChangeset
for help on using the changeset viewer.