Page 1 of 2
Floating point issue on Raspberry PI
Posted: Fri Nov 30, 2012 7:48 pm
by robu
Hi,
I would like to make some calculation on a raspberry pi, but I got problems with the floating point numbers
I created a simple example to demonstrate my problem
Code: Select all
chprintf((BaseSequentialStream *) &SD1, "This is an int: %d\n", 3);
chprintf((BaseSequentialStream *) &SD1, "This is a float: %f\n", 3.4);
char t[80];
float a = 342/23.0;
sprintf(t, "%f\n", a);
chprintf((BaseSequentialStream *) &SD1, t);
The output:
Code: Select all
This is an int: 3
This is a float: 0.00000
0.000000
What can be the problem?
Thanks,
Robert
Re: Floating point issue on Raspberry PI
Posted: Fri Nov 30, 2012 8:06 pm
by hazelnusse
Do you have CHPRINTF_USE_FLOAT defined? If you look at os/various/chprintf.c you'll notice a lot of code that is conditionally compiled only if CHPRINTF_USE_FLOAT is defined. I think you'd want to define that in either your Makefile (DDEFS or UDEFS), or maybe in chconf.h.
Luke
Re: Floating point issue on Raspberry PI
Posted: Fri Nov 30, 2012 8:16 pm
by robu
Hi,
I defined in the chconf.h
Code: Select all
#if !defined(CHPRINTF_USE_FLOAT) || defined(__DOXYGEN__)
#define CHPRINTF_USE_FLOAT TRUE
#endif
I tried also this
Code: Select all
char t[80];
float a = 342/23.0;
sprintf(t, "%f\n", a);
chprintf((BaseSequentialStream *) &SD1, t);
It seams that the problem isn't with the serial communication
Regards,
Robert
Re: Floating point issue on Raspberry PI
Posted: Fri Nov 30, 2012 9:12 pm
by mabl
You could also try to use sprintf, couldn't you?
Re: Floating point issue on Raspberry PI
Posted: Fri Nov 30, 2012 9:20 pm
by robu
As you can see in the previous post I just did.
Re: Floating point issue on Raspberry PI
Posted: Fri Nov 30, 2012 9:27 pm
by Giovanni
Hi,
It is likely a problem with the compiler, probably you have to select the right option for floating point. Which compiler are you using?
Giovanni
Re: Floating point issue on Raspberry PI
Posted: Fri Nov 30, 2012 9:33 pm
by robu
Hi,
I'm using Sourcery CodeBench Lite (2012.09-63) 4.7.2
Regards,
Robert
Re: Floating point issue on Raspberry PI
Posted: Sat Dec 01, 2012 9:03 am
by robu
Hi,
I tried also to enable the -mfloat-abi=softfp or -mfloat-abi=hard but in this case the system doesn't start
Code: Select all
gcc version 4.7.2 (Sourcery CodeBench Lite 2012.09-63)
COLLECT_GCC_OPTIONS='-c' '-mcpu=arm1176jzf-s' '-O2' '-ggdb' '-fomit-frame-pointer' '-falign-functions=16' '-mfloat-abi=soft' '-v' '-ffunction-sections' '-fdata-sections' '-fno-rtti' '-fno-exceptions' '-Wall' '-Wextra' '-D' 'CHIBIOS_DEBUG' '-D' 'CHIBIOS' '-mno-thumb-interwork' '-MD' '-MP' '-MF' '.dep/main.o.d' '-I' '.' '-I' '../../os/ports/GCC/ARM' '-I' '../../os/ports/GCC/ARM/BCM2835' '-I' '../../os/kernel/include' '-I' '../../os/hal/include' '-I' '../../os/hal/platforms/BCM2835' '-I' '../../boards/RASPBERRYPI_MODB' '-I' '../../os/various' '-I' '../../os/contrib' '-I' '/home/robu/UbuntuOne/PMPCToolbox' '-o' 'build/obj/main.o' '-D' '__CS_SOURCERYGXX_MAJ__=2012' '-D' '__CS_SOURCERYGXX_MIN__=9' '-D' '__CS_SOURCERYGXX_REV__=63' '-fremove-local-statics'
/opt/arm_gcc/bin/../libexec/gcc/arm-none-eabi/4.7.2/cc1plus -quiet -v -I . -I ../../os/ports/GCC/ARM -I ../../os/ports/GCC/ARM/BCM2835 -I ../../os/kernel/include -I ../../os/hal/include -I ../../os/hal/platforms/BCM2835 -I ../../boards/RASPBERRYPI_MODB -I ../../os/various -I ../../os/contrib -I /home/robu/UbuntuOne/PMPCToolbox -iprefix /opt/arm_gcc/bin/../lib/gcc/arm-none-eabi/4.7.2/ -isysroot /opt/arm_gcc/bin/../arm-none-eabi -MD build/obj/main.d -MF .dep/main.o.d -MP -MQ build/obj/main.o -D__USES_INITFINI__ -D CHIBIOS_DEBUG -D CHIBIOS -D __CS_SOURCERYGXX_MAJ__=2012 -D __CS_SOURCERYGXX_MIN__=9 -D __CS_SOURCERYGXX_REV__=63 main.cpp -quiet -dumpbase main.cpp -mcpu=arm1176jzf-s -mfloat-abi=soft -mno-thumb-interwork -auxbase-strip build/obj/main.o -ggdb -O2 -Wall -Wextra -version -fomit-frame-pointer -falign-functions=16 -ffunction-sections -fdata-sections -fno-rtti -fno-exceptions -fremove-local-statics -o /tmp/ccA4z8rb.s
GNU C++ (Sourcery CodeBench Lite 2012.09-63) version 4.7.2 (arm-none-eabi)
compiled by GNU C version 4.3.2, GMP version 5.0.5, MPFR version 3.1.1-p2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Regard,
Robert
Re: Floating point issue on Raspberry PI
Posted: Sat Dec 01, 2012 2:21 pm
by robu
Hi
I made an other test,
Code: Select all
char test[80];
float a = 12.4;
sprintf(test, "%f\n", a);
chprintf((BaseSequentialStream *) &SD1, test);
if (a > 15)
chprintf((BaseSequentialStream *) &SD1, "Bigger\r\n");
else
chprintf((BaseSequentialStream *) &SD1, "Smaller\r\n");
a *= 2;
if (a > 15)
chprintf((BaseSequentialStream *) &SD1, "Bigger\r\n");
else
chprintf((BaseSequentialStream *) &SD1, "Smaller\r\n");
chprintf((BaseSequentialStream *) &SD1, "%f\r\n", a);
chprintf((BaseSequentialStream *) &SD1, "Number:%d\n\r", (int)(a * 100000));
And the output
Code: Select all
0.000000
Smaller
Bigger
0.00000
Number:2480000
Where could be the problem with the?
Re: Floating point issue on Raspberry PI
Posted: Sat Dec 01, 2012 4:56 pm
by gmb42
I think you might need to add
to your Makefile or at least ensure CHPRINTF_USE_FLOAT is defined somewhere that chprintf.h will see it.