Float to bits

I am trying to break apart a float but none of the bit instructions seem to work on a float.
I want to take the float and break it in to 2 words HiWord and LoWord.
#define makeLong(hi, low) ((hi) << 16 & (low))
//This defines a command that will shift the high value 16 bits to the left and add it to the low value:

#define makeLong(hi, low) (((long) hi) << 16 | (low))
#define highWord(w) ((w) >> 16)
#define lowWord(w) ((w) & 0xffff)

This does not work or my data is really not in float format.

Any ideas?

Cast it to an unsigned long and operate on that

What do you intend to do with the bits from a iEEE74 representation of a float? Nothing comes to mind as a reasonable thing to do.

I haven't tried this, but you could also define a union, like:

union {
   int byte1;
   int byte2;
   int byte3;
   int byte4;
   float val;
} myUnion;

   myUnion.val = 1.2345;

and then inspect each byte in the union to see what you want to do.

Check some floating bits I wrote here - Arduino Playground - IEEE754tools -

econjack:

union {

int byte1;
   int byte2;
   int byte3;
   int byte4;
   float val;
} myUnion;

In this union "byte1", 2, 3 and 4 all overlap the same memory location.
It's also misleading to name these "byteX" since they are int sized.
You would want something like:

union {
   int as_int[2];
   long as_long;
   byte as_byte[4];
   float as_float;
} myUnion;

Then you could say:

union myUnion val;
val.as_float = 3.1415927;
Serial.println(val.as_int[0]);
Serial.println(val.as_int[1]);
// etc.

@gardner: yep, you're right.

splitting a float in 4 bytes or 2 ints makes only sense when transferring it from platform A to platform B e.g. over serial line from Arduino to PC.

You can however split a float in its exponent, mantisse and sign by means of a struct.

I am trying to move a float to a device that only reads words. Not sure of the bit order on the other side once moved as a 3rd party will read and display on the screen.

I am trying to move a float to a device that only reads words.

Words as in "Thirty seven point eight"? Or words as in Microsoft's stupid definition of word?

A word is an integer value. You need to truncate the float as an int (long, actually) before you begin to worry about byte order.

PaulS:
What do you intend to do with the bits from a iEEE74 representation of a float? Nothing comes to mind as a reasonable thing to do.

Maybe gain an understanding of how floats work?

Maybe gain an understanding of how floats work?

Consulting the ATMega data sheet would show how the float is represented in memory. That seems like a more reasonable thing to do than to try to learn something about how the bits are set by turning LEDs on or off.

PaulS:

Maybe gain an understanding of how floats work?

Consulting the ATMega data sheet would show how the float is represented in memory. That seems like a more reasonable thing to do than to try to learn something about how the bits are set by turning LEDs on or off.

Sorry to seem to be picking on you this morning Paul, but I would be very surprised if the ATmega datasheet could cover float uses and storage as there is no floating point hardware in a mega device. Surely it's gcc or some avr math library that would define how floats are used and stored in atmega program?

Lefty

Sorry to seem to be picking on you this morning Paul, but I would be very surprised if the ATmega datasheet could cover float uses and storage as there is no floating point hardware in a mega device.

No problem. The compiler supports floats, so I thought that how a float was stored in memory would have to be documented in the ATMega data sheet. I'll look, first, next time.

moorsb:
I am trying to move a float to a device that only reads words. Not sure of the bit order on the other side once moved as a 3rd party will read and display on the screen.

Do you have a link to the datasheet of the device?

I am reading a DHT11 humidity and temperature in float on a nano with a enj2860 ethernet board. I want to send this data via Modbus TCP
here is an article

I then use a tablet running Telsa Modbus Scada app over wifi and can display modbus floating point.

The usual recommendation for transferring float data is to not worry about float length, format or byte order, and instead convert the floats to a text format. dtostre() will convert floats to a compact scientific notation that can be easily parsed by another system with no loss of range or resolution, and no risk of format or byte order problems.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga6c140bdd3b9bd740a1490137317caa44

moorsb:
I am reading a DHT11 humidity and temperature in float on a nano with a enj2860 ethernet board. I want to send this data via Modbus TCP
here is an article
http://www.chipkin.com/how-real-floating-point-and-32-bit-data-is-encoded-in-modbus-rtu-messages/

I then use a tablet running Telsa Modbus Scada app over wifi and can display modbus floating point.

The DHT11 gives only integer values for both T and H (I wrote a lib for DHT series) so using float is not needed - at least not from the DHT11 point of view.

I also want data from a load cell in float sent via modbus tcp.

I have to conform to the modbus float format

Is the load cell actually giving you a value in float ?