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: - /**************************************************************************************************
-
*Converts binary into IEEE 754 single percision
-
***************************************************************************************************/
-
float bin2float(unsigned long binary){
-
float fp;
-
unsigned int s=0,e=0,f=0;
-
s=binary>>31;/*sign bit*/
-
e=(binary>>23&0xff);/*exponent*/
-
f=binary&0x7fffff;/*fraction*/
-
fp=(sign_bit(s)<<(e-127))*(1+f*(pow(2,-23)));
-
-
return(fp);
-
}
-
/**************************************************************************************************
-
*Converts binary into IEEE 754 double percision
-
***************************************************************************************************/
-
double bin2double(int binary1, int binary2){//binary1=bits 0-31 binary2=bits 32-63
-
double fp;
-
unsigned int s=0,e=0,f=0;
-
s=binary2>>31;/*sign bit*/
-
e=(binary2>>20&0x7ff);/*exponent*/
-
f=(binary1*pow(2,-51)+((binary2&0x7ffff)*pow(2,-21)));/*fraction*/
-
fp=(sign_bit(s)*pow(2,(e-1023)))*(1+f);
-
-
return(fp);
-
-
}
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
5 3406
Is there some reason you can't just: -
unsigned long binary = 1234;
-
float f = binary;
-
double d = binary;
-
??
Is there some reason you can't just: -
unsigned long binary = 1234;
-
float f = binary;
-
double d = binary;
-
??
True. But the data is coming from somewhere else, so a cast is really what is needed. -
void * address; // contains the address of the double
-
double value = (double*)address;
-
Adrian
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: revuesbio |
last post by:
Hi
Does anyone have the python version of the conversion from msbin to
ieee?
Thank u
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
|
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...
| |