Changeset 6703 in orxonox.OLD
- Timestamp:
- Jan 25, 2006, 3:17:08 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/converter.cc
r6634 r6703 88 88 * @remarks: The int is stored in big-endian 89 89 * @param x: The int which is to convert 90 * @return: A byte-array that accords the given int value90 * @return: The number of written bytes 91 91 */ 92 92 int Converter::intToByteArray(int x, byte* a, int length) … … 124 124 * Converts a byte-array into an int 125 125 * @param a: The byte-array which is to convert 126 * @return: An int that accords the given byte-array 126 * @param x: The place where the result is stored 127 * @return: The number of read bytes 127 128 */ 128 129 int Converter::byteArrayToInt(const byte* a, int* x) … … 150 151 /*! 151 152 * Converts a float into a string containing its binary representation 153 * @param x: The float which is to convert 154 * @return: A string containing the float's binary representation 152 155 */ 153 156 char* Converter::floatToBinString(float x) … … 331 334 * Converts a float value into a byte-array and stores the result into a given byte-array 332 335 * @param x: The float which is to convert 333 * @return: A byte-array which accords the given float 336 * @param a: The byte array where the result is to store 337 * @param length: The length of the array a 338 * @return: The number of written bytes 334 339 */ 335 340 int Converter::_floatToByteArray(float x, byte* a, int length) … … 398 403 399 404 400 if (mantisse != 0)401 {405 //if (mantisse != 0) 406 //{ 402 407 while (mantisse < expmult) 403 408 { … … 406 411 } 407 412 408 mantisse -= expmult; 409 } 413 if (exponent >= 0) 414 mantisse -= expmult; 415 else 416 { 417 //Denormalized 418 while (exponent < 0) 419 { 420 mantisse /= 2; 421 exponent++; 422 } 423 printf("Conv: Denorm"); 424 } 425 //} 410 426 } 411 427 … … 414 430 415 431 int hx = mantisse + expmult * exponent; 416 int result = intToByteArray(hx, a, length); 432 //int result = intToByteArray(hx, a, length); 433 intToByteArray(hx, a, length); 417 434 if (sgn == -1) 418 435 a[3] += sgnadd; … … 424 441 // result[3] += sgnadd; 425 442 426 return result; 443 //return result; 444 return FLOATSIZE; 427 445 } 428 446 … … 431 449 * Converts a byte-array into a float value 432 450 * @param a: The byte-array which is to convert 433 * @return: A float value which accords the given byte-array 451 * @param x: The place where the result is to store 452 * @return: The number of read bytes 434 453 */ 435 454 int Converter::_byteArrayToFloat(const byte* a, float* x) … … 459 478 return FLOATSIZE; 460 479 } 461 462 mantisse += expmult; 463 exponent -= 128; 464 480 else if (exponent == 0 && mantisse != 0) 481 { 482 exponent = -126; 483 printf("ReConv: Denorm"); 484 } 485 else 486 { 487 mantisse += expmult; 488 exponent -= 128; 489 } 465 490 466 491 int sgn; … … 498 523 * Converts a float value into a byte-array 499 524 * @param x: The float which is to convert 500 * @return: A byte-array which accords the given float 525 * @param a: The array where the result is to store 526 * @param length: The length of the array a 527 * @return: The number of written bytes 501 528 */ 502 529 int Converter::floatToByteArray(float x, byte* a, int length) 503 530 { 504 if ( length< 4)531 if ( length< FLOATSIZE ) 505 532 { 506 533 PRINTF(1)("Byte Array to small\n"); … … 511 538 for (int i = 0; i < 4; i++) 512 539 a[i] = p[i]; 513 return 4; 540 541 return FLOATSIZE; 514 542 } 515 543 … … 518 546 * Converts a byte-array into a float value 519 547 * @param a: The byte-array which is to convert 520 * @return: A float value which accords the given byte-array 548 * @param x: The place where the result is to store 549 * @return: The number of read bytes 521 550 */ 522 551 int Converter::byteArrayToFloat(const byte* a, float* x) … … 524 553 *x = *((float*)a); 525 554 526 return 4; 527 } 555 return FLOATSIZE; 556 } 557 558 559 560 561 562 563 564 565 528 566 529 567 /** … … 627 665 } 628 666 667 void Converter::ArrayfloatTest(float x) 668 { 669 //float x = 8.0f; 670 //float x = numeric_limits<float>::infinity(); 671 672 printf("To Convert: %e\n", x); 673 byte* res = new byte[4]; 674 675 int wr = _floatToByteArray(x, res, 4); 676 for (int i = 0; i < 4; i++) 677 printf("%i ", res[i]); 678 printf(" written bytes: %i \n", wr); 679 680 float y; 681 int rd = _byteArrayToFloat(res, &y); 682 printf("ReConvert: %e\n", y); 683 printf("Read bytes: %i -> ", rd); 684 685 if (x == y) 686 printf("equal\n"); 687 else 688 printf("different\n"); 689 } 690 629 691 void Converter::debug() 630 692 { 631 693 printf("We rulez\n"); 632 694 633 //Denormalized? 634 //floatTest(-9.87624e-38f); 635 //floatTest(-9.87624e-37f); 636 //floatTest(-9.87624e-36f); 637 //floatTest(-9.87624e-35f); 638 639 /* 640 floatTest(14.7f); 641 floatTest(12.07e15f); 642 floatTest(0.0f); 643 floatTest(5.67e-15f); 644 */ 695 ArrayfloatTest(0.125f); 696 ArrayfloatTest(64.0f); 697 ArrayfloatTest(0.0f); 698 ArrayfloatTest(5.00091e-29f); 645 699 646 700 //floatTest(-18.0098f); … … 648 702 //floatTest(-0.0f); 649 703 //floatTest(-5.67e-29f); 650 floatTest(-45.7e-32f); 651 floatTest(45.7e-33f); 652 floatTest(-45.7e-34f); 653 floatTest(45.7e-35f); 654 } 704 705 706 //floatTest(-45.7e-32f); 707 //floatTest(45.7e-33f); 708 //floatTest(-45.7e-34f); 709 //floatTest(45.7e-35f); 710 } -
branches/network/src/lib/network/converter.h
r6634 r6703 50 50 static void debug(); 51 51 static void floatTest(float x); 52 static void ArrayfloatTest(float x); 52 53 static float getDenormConst(); 53 54
Note: See TracChangeset
for help on using the changeset viewer.