Changeset 6634 in orxonox.OLD for trunk/src/lib/network/converter.cc
- Timestamp:
- Jan 21, 2006, 1:18:19 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/network/converter.cc
r6341 r6634 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 … … 310 333 * @return: A byte-array which accords the given float 311 334 */ 312 int Converter:: floatToByteArray(float x, byte* a, int length)335 int Converter::_floatToByteArray(float x, byte* a, int length) 313 336 { 314 337 if (length < FLOATSIZE) … … 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; 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 370 414 371 415 int hx = mantisse + expmult * exponent; … … 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 } … … 383 433 * @return: A float value which accords the given byte-array 384 434 */ 385 int Converter:: byteArrayToFloat(const byte* a, float* x)386 { 387 //handle 0388 for (int i = 0; i<FLOATSIZE; i++)435 int Converter::_byteArrayToFloat(const byte* a, float* x) 436 { 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 447 }*/ 448 449 int hexp = a[2] + a[3] * 256; 450 int exponent = (hexp / 128) % 256; 399 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 { 458 *x = 0; 459 return FLOATSIZE; 460 } 461 402 462 mantisse += expmult; 403 404 int hexp = a[2] + a[3] * 256; 405 int exponent = (hexp / 128) % 256 - 128; 463 exponent -= 128; 464 406 465 407 466 int sgn; … … 411 470 sgn = 1; 412 471 413 //printf("mantisse = %i exponent = %i \n", mantisse, exponent);472 /// printf("ReConv: mantisse = %i exponent = %i \n", mantisse, exponent); 414 473 415 474 float emult = 1; … … 421 480 emult /= 2; 422 481 482 /* 483 float result = mantisse * emult; 484 if (sgn == -1) 485 result = -result; 486 487 return result; 488 */ 489 423 490 *x = mantisse * emult; 424 491 if (sgn == -1) … … 426 493 427 494 return FLOATSIZE; 495 } 496 497 /*! 498 * Converts a float value into a byte-array 499 * @param x: The float which is to convert 500 * @return: A byte-array which accords the given float 501 */ 502 int Converter::floatToByteArray(float x, byte* a, int length) 503 { 504 if ( length<4 ) 505 { 506 PRINTF(1)("Byte Array to small\n"); 507 return 0; 508 } 509 byte* p = (byte*)&x; 510 511 for (int i = 0; i < 4; i++) 512 a[i] = p[i]; 513 return 4; 514 } 515 516 517 /*! 518 * Converts a byte-array into a float value 519 * @param a: The byte-array which is to convert 520 * @return: A float value which accords the given byte-array 521 */ 522 int Converter::byteArrayToFloat(const byte* a, float* x) 523 { 524 *x = *((float*)a); 525 526 return 4; 428 527 } 429 528 … … 504 603 } 505 604 605 606 607 608 void Converter::floatTest(float x) 609 { 610 //float x = 8.0f; 611 //float x = numeric_limits<float>::infinity(); 612 613 printf("To Convert: %e\n", x); 614 615 byte* res = floatToByteArray(x); 616 for (int i = 0; i < 4; i++) 617 printf("%i ", res[i]); 618 printf("\n"); 619 620 float y = byteArrayToFloat(res); 621 printf("ReConvert: %e\n", y); 622 623 if (x == y) 624 printf("equal\n"); 625 else 626 printf("different\n"); 627 } 628 629 void Converter::debug() 630 { 631 printf("We rulez\n"); 632 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 */ 645 646 //floatTest(-18.0098f); 647 //floatTest(-24.07e23f); 648 //floatTest(-0.0f); 649 //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 }
Note: See TracChangeset
for help on using the changeset viewer.