Go Down

Topic: Convert IEEE 754 32 to float (Read 11731 times) previous topic - next topic

michinyon

Your method will probably create the wrong result.   Aren't you putting the bytes together in the wrong order ?

jurs

Your method will probably create the wrong result.   Aren't you putting the bytes together in the wrong order ?
The byte order is platform dependent. Each platform might be either using
- byte order 'big endian'
- byte order 'little endian'
- byte order 'mixed endian'

So perhaps when transferring bytes from one system to another system which are providing a different endianess, the bytes possibly needs to be changed: The byte order may be the same or the byte order may be reversed, or the highword may be mixed with the loword.

The example code I posted handles each byte seperately, so its easy to mix the byte order as needed. Just as the sending platform provides the bytes, you could use:
Code: [Select]

  ((byte*)&x)[3]= 0x3F;
  ((byte*)&x)[2]= 0x32;
  ((byte*)&x)[1]= 0x2E;
  ((byte*)&x)[0]= 0x3F;

or
Code: [Select]

  ((byte*)&x)[0]= 0x3F;
  ((byte*)&x)[1]= 0x32;
  ((byte*)&x)[2]= 0x2E;
  ((byte*)&x)[3]= 0x3F;

or
Code: [Select]

  ((byte*)&x)[2]= 0x3F;
  ((byte*)&x)[3]= 0x32;
  ((byte*)&x)[0]= 0x2E;
  ((byte*)&x)[1]= 0x3F;

Or as the transfer between different platforms requires.

So while the 4 bytes of a float are defined by IEEE 754, the byte order is defined by the computer platform. So byte order may be different for sender and receiver. Should be easy to find out when sending a known float value.

michinyon

Did you bother to read any of the other posts in this thread ?

The OP has already posted the long value he is getting,   and we have demonsrated that,  contrary to OP's belief,  it IS a float  and DOES  represent the float value he expects.

Your code in reply #14  is ** wrong **  for this OP's application.

joey120373

@ michinyon,

You are correct, I did not research this as much as I needed to obviously, I did however do some research on the IEEE 754 format, with his led me to several pages on how to do the math conversions, the same ones you have provided.

Last night I did eventually find that Arduino uses the IEEE 754, my bad.

I want to thank you for all of the help you have provided I spite of my ignorance.

robtillaart

reminds me of some things I did a while ago - http://playground.arduino.cc/Main/IEEE754tools -
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

joey120373

Thanks AWOL, for this:

uint32_t x = 0x3f322e3f;
float y = *(float*)&x;
Serial.print (y, 6);

It works perfect. I would love it if someone could give me a detailed description of why this works though.

If arduino uses the IEEE754 standard to represent floating point numbers, then why doesn't " float x = 0x........ " ( Where " 0x........" Is any given value generated by union-ing the 4 bytes that are received )
I was under the impression that "*" is a multiply command, but I am thinking that that is not how it is being used in this instance. The AND(&) command I am guessing is doing what and & command does but I would like to know what kind of voodoo is going on with the *(float*) part. Again, thanks for the help.

AWOL

float y = *(float*)&x;

&x give the address of x.
This is a pointer to x, and because x is a uint32_t, the type of the pointer is "pointer to uint32_t".
So, cast that pointer to a pointer to float, and dereference that pointer to fetch the value there as a float, and assign that value to a float variable y.

joey120373

@ michinyon

I'm afraid I still have to disagree with you on one point,

0x3f322e3f is not inherently a float, nor would any other combination of 8 HEX characters.

While this and any other string is short hand for 32 1s and 0s, to say it is obviously a float doesn't make sense.

It is also 1060253247
Or 7714427077 (octal)
Or it could be translated into
4 ASCII characters

And all would still be a valid claim wouldn't they?
The thing I was ignorant about was how to get arduino to interpret it as a float.
I thought simply declaring it as such "float y = 0x3......." Would do it but obviously as has been pointed out that is not the case.
I do like your math suggestions though, as they were exactly what I was asking for, and are very helpful for visually understanding what needs to happen.
Regards.




racpi

for what is worth I found this web tool quite handy for checking float to hex conversions when sending fp data between different devices over serial/ rf  links
http://www.h-schmidt.net/FloatConverter/
richard

nsatish

Hi,

Whats the logic to convert a float value to  IEEE 754 half (16 signed bit) or single (32 signed bit) precision?

is any library available in Arduino ?

Go Up