Floating point issue on Raspberry PI

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Floating point issue on Raspberry PI

Postby robu » Fri Nov 30, 2012 7:48 pm

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

hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Floating point issue on Raspberry PI

Postby hazelnusse » Fri Nov 30, 2012 8:06 pm

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

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: Floating point issue on Raspberry PI

Postby robu » Fri Nov 30, 2012 8:16 pm

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

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: Floating point issue on Raspberry PI

Postby mabl » Fri Nov 30, 2012 9:12 pm

You could also try to use sprintf, couldn't you?

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: Floating point issue on Raspberry PI

Postby robu » Fri Nov 30, 2012 9:20 pm

As you can see in the previous post I just did.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Floating point issue on Raspberry PI

Postby Giovanni » Fri Nov 30, 2012 9:27 pm

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

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: Floating point issue on Raspberry PI

Postby robu » Fri Nov 30, 2012 9:33 pm

Hi,

I'm using Sourcery CodeBench Lite (2012.09-63) 4.7.2

Regards,
Robert

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: Floating point issue on Raspberry PI

Postby robu » Sat Dec 01, 2012 9:03 am

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

robu
Posts: 25
Joined: Sat Nov 24, 2012 3:39 pm

Re: Floating point issue on Raspberry PI

Postby robu » Sat Dec 01, 2012 2:21 pm

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?

gmb42
Posts: 31
Joined: Tue Oct 02, 2012 8:09 pm
Location: Aberdeenshire, UK

Re: Floating point issue on Raspberry PI

Postby gmb42 » Sat Dec 01, 2012 4:56 pm

I think you might need to add

Code: Select all

UDEFS = -DCHPRINTF_USE_FLOAT=TRUE
to your Makefile or at least ensure CHPRINTF_USE_FLOAT is defined somewhere that chprintf.h will see it.


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 41 guests