IBM developerWorks
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 16th, 2003, 01:25 AM
wizade wizade is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 119 wizade User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 18 sec
Reputation Power: 6
Weird floating point question

hello,

Ive run into a problem that I think there may be a good solution for.

I have a single precision floating point value given to me in the following format:

3 bytes
-------------------------------------
SFFFFFFF FFFFFFFF EEEEEEEE

thats 1 Sign bit, 15 Mantissa, 8 exponent.
(a different order than the IEEE standard)

Right now I just have the 3 bytes as unsigned char variables to hold these values. Now the question is, Is there an easy way to turn these 3 seperate bytes back into a floating point number?

I can manually shift them around but that seems way too hard.

Thanks in advance : )
__________________
Web Hosting: http://www.cybertarp.com


Reply With Quote
  #2  
Old April 16th, 2003, 10:27 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 57 m 9 sec
Reputation Power: 437
I think you're pretty much stuck with shifting them around, but that shouldn't be too much work. Only the exponent will need to be shifted.

Let's see, off the top of my head:
Code:
//3 bytes
//-------------------------------------
//SFFFFFFF FFFFFFFF EEEEEEEE

typedef unsigned char BYTE;

float ConvertFlt(BYTE flt_in[])
{
    BYTE flt_out[4];
    BYTE temp;

    flt_out[3] = 0;
    flt_out[2] = flt_in[1];
    flt_out[1] = flt_in[0] & 0x7F;  // mask out sign bit
    if (flt_in[2] & 0x01)  // "shift" exp LSB in from the left
        flt_out[1] |= 0x80;
    flt_out[0] = (flt_in[0] & 0x80) | ((flt_in[2] >> 1) & 0x7F);
    return *((float*)&flt_out[0]);
}


Bear in mind that this is ignoring Intel's reversed-order of multibyte data ("little-endian"). In that case, you would have to have indexed the flt_out bytes in the reversed order. I figured that if I did that here, then it would just make the code confusing.

I am also ignoring any deviation of the exponent's format or of the mantissa's from the IEEE standard.

I also have not tested this code, but I think it should be close.

Finally, float has one more byte than your simple floating-point type, but adding the fourth byte of zero should not change the precision of the original.

Reply With Quote
  #3  
Old April 16th, 2003, 06:48 PM
wizade wizade is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 119 wizade User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 18 sec
Reputation Power: 6
perfect !

thanks

Reply With Quote
  #4  
Old April 16th, 2003, 06:58 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 h 5 m 45 sec
Reputation Power: 797
You could also possibly use the union structure to do your conversions. Declare a union with two members, a float and a char array. Now, all you need to do is assign to one of the members and then, read from the other one, for the conversion. The following code converts between float and chars:
Code:
#include <stdio.h>
typedef union {
  char c[sizeof(float)];
  float f;
} Foo;

int main (void) {
  Foo bar;
  int x;
  bar.f = 12.34;
  printf("Float %f is converted to ", bar.f);
  for (x = 0; x < sizeof(float); x++)
    printf("%d ", (int)bar.c[x]);
  printf("\n");

  bar.c[0] = -92;
  bar.c[1] = 112;
  bar.c[2] = 69;
  bar.c[3] = 65;
  printf("Chars ");
  for (x = 0; x < sizeof(float); x++)
    printf("%d ", (int)bar.c[x]);
  printf("is converted to %f\n", bar.f);

  return 0;
}


Hope this helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Weird floating point question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway