 following code snippet running on a MicroBlaze processor with Floating Point enabled:
float test2=22.29f;
int main (void)
{ float test = 22.0f;
xil_printf("test = 0x%08x\r\n",test);
xil_printf("test2 = 0x%08x\r\n",test2);
float data=0;
xil_printf("data1 %08x\n\r",data);
data=5.0/4.0;
xil_printf("data2 %08x\n\r",data);
data=101.0000;
xil_printf("data2 %08x\n\r",data);
However, the result is as follows:
test = 0x00000000
test2 = 0x80000000
data1 00000000
data2 00000000
data2 00000000
What is the cause of this?
There are two errors in the above code:
1. The function xil_printf() does not support printing of floating point numbers. You will need to use the printf statement to print floating numbers.
Please refer to page 6 of the OS and Libraries collection document:
2. %x specifier only converts integers to hex. It does not work with floating point numbers.
To work around this, take the address of the float, cast the pointer to an integer pointer (which has no effect on the bit pattern of the data), and then dereference it so that the data is interpreted as an integer but without changing the bit pattern.
Therefore, the above code should be modified as follows:
#include <stdio.h>
#include "platform.h"
void print(char *str);
float test2=22.29f;
int main()
{
init_platform();
float test = 22.0f;
printf("test = 0x%8x\r\n",*(int *)&test);
printf("test2 = 0x%8x\r\n",*(int *)&test2);
float data=0;
printf("data1 0x%8x\n\r",*(int *)&data);
data=5.0/4.0;
printf("data2 0x%8x\n\r",*(int *)&data);
data=101.0000;
printf("data2 0x%8x\n\r",*(int *)&data);
return 0;
}
The result will be as follows:
test = 0x41b00000
test2 = 0x41b251ec
data1 0x 0
data2 0x3fa00000
data2 0x42ca0000