Lately I have had a lot of trouble with the behaviour in a program I'm working on. Finally now I'm able to reproduce some weird behaviour wrt. floating point conversion. See the code snippet below which is the true code. The "0" prefix comes from the send_n function.
I am running the arm-elf-gcc toolchain, uC is an Atmel AT91R40807:
arm-elf-gcc ver 2.96 arm-elf-as arm-elf-ld arm-elf-objcopy ver 2.10
(Sorry, I don't know which library versions I use, and also don't know how to find it out. Please advise.)
When I run the code below the following gets printed
0 Check : 39.37 0 Check : 39.374996 0 Check : 30.00 0 Check : 39.375000 0 Check : 39.38 0 Check : 39.375004If I change the formatting from %7.2f to %7.3f everything works like a charm, and the following is printed on the serial terminal
0 Check : 39.375 0 Check : 39.374996 0 Check : 39.375 0 Check : 39.375000 0 Check : 39.375 0 Check : 39.375004I have had no luck googling for a solution/explanation, but of course, I might have been looking in the wrong direction.
All and any suggestions are welcome. Also, please don't hesitate to ask for further information.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void showfloatbug(int n) { union longfloat { float flow; unsigned long l; } u;
u.l = 0x421d7fff; snprintf(dbuf,sizeof(dbuf)," Check : %7.2f",u.flow); send_n(n,dbuf); snprintf(dbuf,sizeof(dbuf)," Check : %f",u.flow); send_n(n,dbuf);
u.l = 0x421d8000; snprintf(dbuf,sizeof(dbuf)," Check : %7.2f",u.flow); send_n(n,dbuf); snprintf(dbuf,sizeof(dbuf)," Check : %f",u.flow); send_n(n,dbuf);
u.l = 0x421d8001; snprintf(dbuf,sizeof(dbuf)," Check : %7.2f",u.flow); send_n(n,dbuf); snprintf(dbuf,sizeof(dbuf)," Check : %f",u.flow); send_n(n,dbuf); } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BR,