float type format

I was wondering how do I go about finding how Arduino stores floats in memory, is based in IEEE754 format?

Yes.

I'm wondering why you need to know that. Diddling with the bits is not a good idea.

Surely diddling with the bits is what its all about!

Surely diddling with the bits is what its all about!

Maybe I'm missing something, but I can't see that

float pi = 3.14159;
bitWrite(pi, 18, 0);

is going to achieve something useful.

Thanks, I want to convert these bits back to a float on a different platform. it seems much faster to transfer this as 4 bytes rather then the entire float number.

it seems much faster to transfer this as 4 bytes rather then the entire float number.

The four bytes are the entire float number.

Yeah, a 4 byte float is much smaller than this as an ascii "3.4028235E+38"

I am outputting the value of the float as hex and I do not understand the last 2 bytes. A1:10:51:C0:0D:0A. It should be just A1:10:51:C0. Maybe this is not right "(char *)&number"?

float number = -3.26664;
void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println((char *)&number);
  delay(1000); 
}

println adds CR/LF

Thanks, i just went to my ascii table and looked up the values. duh! I am sitting here doing binary sign calculations to see if it was something odd. Next question, do I need to worry about the char not being unsigned. I get an error saying "call of overloaded 'println(unsigned char*)' is ambiguous" when I have this (unsigned char *)&number. I would imagine I would the full 0-255 here.

Why aren't you using the write method?

Hi fnsnoop,
if the println function expects a NULL terminated string as parameter, this code

Serial.println((char *)&number);

can give problems because you are pointing a memory area whose content, after the 4 bytes of the number variable, in unknown.

Just a thought:
Do you really want to use floating point numbers or
are your really looking at fixed point integers.
For example if you are dealing with length to the nearest 10th of an inch,
store your length in 10ths of an inch (i.e. 12.3 inches is stored as the integer 123 ) .
Floating point numbers are hugely slow and awkward.

A little more info about your application would be helpful.

fnsnoop:
I am outputting the value of the float as hex and I do not understand the last 2 bytes. A1:10:51:C0:0D:0A. It should be just A1:10:51:C0. Maybe this is not right "(char *)&number"?

float number = -3.26664;

void setup() {
 Serial.begin(9600);
}
void loop() {
 Serial.println((char *)&number);
 delay(1000);
}

What you are doing there is called 'type punning' but you're doing it the wrong way. The right way is to use a union:

  union floatUIntUnion {
    float fl;
    uint32_t u32;
    uint8_t u8q[4];
  };
  
  floatUIntUnion fiu;
 
  float flibble=50509.324;
  fiu.fl=flibble;
  Serial.println(fiu.u32,HEX); 
  //Serial.write(fiu.u8q,4); //to write bytes
  
  uint32_t someNumberIEEE754=0x40490FDB;
  fiu.u32=someNumberIEEE754;
  Serial.println(fiu.fl,7);

Google 'C++ union' and 'type punning' to find out more :slight_smile: