473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Troubles with IEEE 754 floating point numbers

Hi,

I am currently working on a program to decode data that is collected on a gps receiver and i've ran into a problem. Some of the data is encoded in IEEE 754. I wrote the following functions to decode the the binary:
Expand|Select|Wrap|Line Numbers
  1. /**************************************************************************************************
  2. *Converts binary into IEEE 754 single percision
  3. ***************************************************************************************************/
  4. float bin2float(unsigned long binary){
  5.       float fp;
  6.       unsigned int s=0,e=0,f=0;
  7.       s=binary>>31;/*sign bit*/
  8.       e=(binary>>23&0xff);/*exponent*/
  9.       f=binary&0x7fffff;/*fraction*/
  10.       fp=(sign_bit(s)<<(e-127))*(1+f*(pow(2,-23)));
  11.  
  12.       return(fp);
  13. }
  14. /**************************************************************************************************
  15. *Converts binary into IEEE 754 double percision
  16. ***************************************************************************************************/
  17. double bin2double(int binary1, int binary2){//binary1=bits 0-31 binary2=bits 32-63
  18.       double fp;
  19.       unsigned int s=0,e=0,f=0;
  20.       s=binary2>>31;/*sign bit*/
  21.       e=(binary2>>20&0x7ff);/*exponent*/
  22.       f=(binary1*pow(2,-51)+((binary2&0x7ffff)*pow(2,-21)));/*fraction*/
  23.       fp=(sign_bit(s)*pow(2,(e-1023)))*(1+f);
  24.  
  25.       return(fp);
  26.  
  27. }
these return the correct values but I do not have enough processing power available to use the pow function. Any ideas on a way to decrease cycles? Is there a predefined way in C to convert the binary to IEEE 754? Any help would be appreciated I don't work with C too often. Oh almost forgot, I am running this on a linux platform. Thanks
-Matt
Jul 10 '07 #1
5 3406
weaknessforcats
9,208 Expert Mod 8TB
Is there some reason you can't just:

Expand|Select|Wrap|Line Numbers
  1. unsigned long binary = 1234;
  2. float f = binary;
  3. double d = binary;
  4.  
??
Jul 10 '07 #2
AdrianH
1,251 Expert 1GB
Is there some reason you can't just:

Expand|Select|Wrap|Line Numbers
  1. unsigned long binary = 1234;
  2. float f = binary;
  3. double d = binary;
  4.  
??
True. But the data is coming from somewhere else, so a cast is really what is needed.

Expand|Select|Wrap|Line Numbers
  1. void * address;  // contains the address of the double
  2. double value = (double*)address;
  3.  

Adrian
Jul 10 '07 #3
AdrianH
1,251 Expert 1GB
Since all of your pow() calls are using a base of 2 use the >> or << operators instead. But your algorithm is probably slower then a cast.


Adrian
Jul 10 '07 #4
thanks for the help guys, I got it working fast enough. My problem was i was using a 4 byte int. once i changed it to a long long that could store 8 bytes of data then i was able to do everything by using bit shifting.
Jul 10 '07 #5
AdrianH
1,251 Expert 1GB
thanks for the help guys, I got it working fast enough. My problem was i was using a 4 byte int. once i changed it to a long long that could store 8 bytes of data then i was able to do everything by using bit shifting.
But why don't you just let the compiler/processor interpret the floating point number? Why do the manual translation?


Adrian
Jul 10 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Grant Edwards | last post by:
I've read over and over that Python leaves floating point issues up to the underlying platform. This seems to be largely true, but not always. My underlying platform (IA32 Linux) correctly...
2
by: Madhusudan Singh | last post by:
I am querying an instrument in a measurement application. The values are returned as IEEE format binary floating point numbers. There are 4 bytes per point. Multiple points are not separated by...
4
by: ej | last post by:
I ran into something today I don't quite understand and I don't know all the nitty gritty details about how Python stores and handles data internally. (I think) I understand why, when you type in...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
2
by: Andre | last post by:
I read some place that for performance reasons, IEEE floating point standard has not been implemented by .NET and that different machine architectures may produce different results.. but what if...
33
by: Nick Maclaren | last post by:
The numerical robustness of Python is very poor - this is not its fault, but that of IEEE 754 and (even more) C99. In particular, erroneous numerical operations often create apparently valid...
13
by: revuesbio | last post by:
Hi Does anyone have the python version of the conversion from msbin to ieee? Thank u
5
by: =?ISO-8859-1?Q?Roman_T=F6ngi?= | last post by:
IEEE-754 Arithmetic: Most real numbers can't be stored exactly on the computer, but there can be stated the range within which a machine number lies. For the following example, I assume double...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.