how to see the binary representation of a float ?

Hello,
I am a newbie for the arduino, and would like to see what is inside a float variable, ie its binary representation. This is something that I can do with other microcontrollers. I have tried something like that:

float RealNumber;
byte ArrayOfFourBytes[4];

void setup()
{
Serial.begin(9600);

RealNumber = 12.567;
&ArrayOfFourBytes=&RealNumber;

Serial.print(ArrayOfFourBytes[3], BIN);
Serial.print(ArrayOfFourBytes[2], BIN);
Serial.print(ArrayOfFourBytes[1], BIN);
Serial.print(ArrayOfFourBytes[0], BIN);
}

void loop()
{
delay(1000);
}

I had a compiling error. Can anyone help me ?
By the way, what is the best solution for programs that run only once, for debugging experiments, of course. Is the SleepNow function documented somewhere ?

best regards,

Edit: Added missing line of code.

Change this...

byte  ArrayOfFourBytes[4];

...to this...

byte  *ArrayOfFourBytes;

...and change this line...

&ArrayOfFourBytes=&RealNumber;

...to this...

ArrayOfFourBytes = (byte*) & RealNumber;

There are other ways to do it but the above change should fit well with what you already have.

By the way, what is the best solution for programs that run only once, for debugging experiments, of course

What you have looks good to me. Is there something specific you don't like about it?

and would like to see what is inside a float variable, ie its binary representation

Hello,
Yes I want to check that the float type is IEEE754 compliant.

There is still something unclear for me. If I remove the line
&ArrayOfFourBytes=&RealNumber;
how do I make the connection between the RealNumber variable
and the pointer ?

Thanks in advance for your reply,
Best regards,

Yes I want to check that the float type is IEEE754 compliant

Yes.

&ArrayOfFourBytes=&RealNumber;

As you have it, "ArrayOfFourBytes" is an actual array - you can't reassign the address of it, which is what the leading "&" would imply.

float RealNumber;
byte*  ArrayOfFourBytes;
..
..
ArrayOfFourBytes = (byte*) RealNumber;

Would be one way to go,

Yes I want to check that the float type is IEEE754 compliant.

I am curious to know how you will do the checking once you have converted it into bytes

why not convert it into an long and print the binary value

  float RealNumber = 1.234;
  unsigned long * longPtr  ;
  longPtr = (unsigned long*) &RealNumber;
  Serial.println(*longPtr, BIN);
  
  RealNumber = -1.234;
  longPtr = (unsigned long*) &RealNumber;
  Serial.println(*longPtr, BIN);

why not convert it into an long and print the binary value

If you do this, you will not be able to determine the byte order used. That is unless you know how a long is stored.

The relevance of such a question would be if you plan to move data between computers in binary format (serial, SD card etc.). E.g. is the "Arduino" float type binary compatible with Intel hardware? Another example would be to upload binary float data to EEPROM (using avrdude with a programmer or the bootloader) and then access this data from a sketch as binary floats - or the reverse - log float data to EEPROM and then capture with avrdude.

Last I checked, the compiler treats the long value:
0x12345678
as consecutive bytes of:
0x78, 0x56, 0x34, 0x12

Hello,
Thanks to all your great contributions, I have succeeded to do what I wanted. Here is my code:

/*
Display Float Number under the Ieee 754 format
F. Auger, sept 2009
*/

void SerialPrintFloatIeee754(float RealNumber)
{
byte* ArrayOfFourBytes;
ArrayOfFourBytes = (byte*) &RealNumber;

if (ArrayOfFourBytes[3]>=128)
Serial.print("1 ");
else
Serial.print("0 ");

Serial.print((ArrayOfFourBytes[3]/64)%2, BIN);
Serial.print((ArrayOfFourBytes[3]/32)%2, BIN);
Serial.print((ArrayOfFourBytes[3]/16)%2, BIN);
Serial.print((ArrayOfFourBytes[3]/8)%2, BIN);
Serial.print((ArrayOfFourBytes[3]/4)%2, BIN);
Serial.print((ArrayOfFourBytes[3]/2)%2, BIN);
Serial.print( ArrayOfFourBytes[3]%2, BIN);
Serial.print((ArrayOfFourBytes[2]/128)%2, BIN);

Serial.print(" ");
Serial.print((ArrayOfFourBytes[2]/64)%2, BIN);
Serial.print((ArrayOfFourBytes[2]/32)%2, BIN);
Serial.print((ArrayOfFourBytes[2]/16)%2, BIN);
Serial.print((ArrayOfFourBytes[2]/8)%2, BIN);
Serial.print((ArrayOfFourBytes[2]/4)%2, BIN);
Serial.print((ArrayOfFourBytes[2]/2)%2, BIN);
Serial.print( ArrayOfFourBytes[2]%2, BIN);

Serial.print((ArrayOfFourBytes[1]/128)%2, BIN);
Serial.print((ArrayOfFourBytes[1]/64)%2, BIN);
Serial.print((ArrayOfFourBytes[1]/32)%2, BIN);
Serial.print((ArrayOfFourBytes[1]/16)%2, BIN);
Serial.print((ArrayOfFourBytes[1]/8)%2, BIN);
Serial.print((ArrayOfFourBytes[1]/4)%2, BIN);
Serial.print((ArrayOfFourBytes[1]/2)%2, BIN);
Serial.print( ArrayOfFourBytes[1]%2, BIN);

Serial.print((ArrayOfFourBytes[0]/128)%2, BIN);
Serial.print((ArrayOfFourBytes[0]/64)%2, BIN);
Serial.print((ArrayOfFourBytes[0]/32)%2, BIN);
Serial.print((ArrayOfFourBytes[0]/16)%2, BIN);
Serial.print((ArrayOfFourBytes[0]/8)%2, BIN);
Serial.print((ArrayOfFourBytes[0]/4)%2, BIN);
Serial.print((ArrayOfFourBytes[0]/2)%2, BIN);
Serial.println( ArrayOfFourBytes[0]%2, BIN);
return;
}

float x;

void setup()
{
Serial.begin(9600);
x = 1.50;
}

void loop()
{
Serial.print("x=");
SerialPrintFloatIeee754(x);

delay(2000);
}

This code is rather long, because the serial.print function does not print the leading useless zeros on the left, so I had to do it by myself.
Any advise to improve this code will be welcome.

Best regards,

Hello,
Here is my final version:

/*
Real Number Display
F. Auger, sept 2009
*/

float x;

void SerialPrintBit(byte TheByte, byte TheBit)
{
Serial.print((TheByte>>TheBit)%2, BIN);
return;
}
void SerialPrintIeee754(float RealNumber)
{
char i,j;
byte* ArrayOfFourBytes;
ArrayOfFourBytes = (byte*) &RealNumber;

SerialPrintBit(ArrayOfFourBytes[3],7);
Serial.print(" ");
for (i=6;i>=0;i=i-1)
{SerialPrintBit(ArrayOfFourBytes[3],i);}
SerialPrintBit(ArrayOfFourBytes[2],7);
Serial.print(" ");
for (i=6;i>=0;i=i-1)
{SerialPrintBit(ArrayOfFourBytes[2],i);}

for (j=1;j>=0;j=j-1)
{for (i=7;i>=0;i=i-1)
{SerialPrintBit(ArrayOfFourBytes[0],i);}
}

Serial.println(" ");

return;
}

void setup()
{
Serial.begin(9600);

x = 1.0;
}

void loop()
{
Serial.print("RealNumber=");
SerialPrintIeee754(x);
x=-x*0.9;
delay(2000);
}

// RealNumber=0 01111111 00000000000000000000000

I'm seemingly too late here...however wikipedias explanation is excelent:

I think the OP was more interested in the journey than the answer

Hmmm...

My guess is that this:

for (j=1;j>=0;j=j-1)
  {for (i=7;i>=0;i=i-1)
   {SerialPrintBit(ArrayOfFourBytes[0],i);}
  }

Should be this:

for (j=1;j>=0;j=j-1){
  for (i=7;i>=0;i=i-1)
    {SerialPrintBit(ArrayOfFourBytes[j],i);}
}

:-?

[ I have not tested it, and the code might be correct, I just think it looked like a potential error ]