math.h bug

When I try and use a sin function in a program (with "#include <math.h>) I get these compile errors:

/Applications/*Development Apps./arduino-0007/tools/avr/bin/../lib/gcc/avr/4.0.2/../../../../avr/lib/libc.a(fixsfsi.o): In function `__fixsfsi':

../../../libm/fplib/fixsfsi.S:57: multiple definition of `__fixsfsi'

/Applications/*Development Apps./arduino-0007/tools/avr/bin/../lib/gcc/avr/4.0.2/avr5/libgcc.a(_sf_to_si.o):/Users/nick/Documents/Electronics/Software/avr-gcc/4.0.2/build/gcc/fp-bit.c:1404: first defined here

Couldn't determine program size: tools/avr/bin/avr-size: '/tmp/build23472.tmp/test.hex': No such file

Any ideas? And any idea who nick is? I don't have a user by that name on my machine.

Also interestingly the sqrt function compiles fine - just not sin or cos.

I tried commenting out some of the offending libraries with no success, it just complained about a missing library.

Paul Badger

Apparently it's a bug in avr-gcc (the compiler that Arduino uses to transform your code into a program that can run on the microcontroller): AVR C Runtime Library - Bugs: bug #18169, multiple definition of __fixsfsi [Savannah]

According to that page, the solution is to link with -lm, which you would need to recompile the Arduino environment to do. I'm going to try to add more flexibility into the compilation process used by Arduino to allow this sort of thing, but unless other people are having similar problems, it probably won't be a high priority.

What are you trying to do? Maybe we can help you find another way to work around the problem.

Well I was going to use a sine function to generate a lookup table at runtime. But I guess I can generate the data then just put it into the program.

I like the pow function for conditioning sensor data too but haven't tried to see if that one works. It just seems wierd to me that sqrt should work but the trig functions don't.

Hi!
I also needed to use the sin function, and didn't suceed because the sin function expects degrees in radians and putting the radians function returned the following error:
DEG_TO_RAD was not declared in this scope
also the sin function always returned a zero value, maybe because I was using an integer instead of a float. By the way what's the status on floats, is it supported by Arduino? the reference webpage lacks information on it.
Thanks!

It's starting to sound like there are enough people trying to use floats and do math on the Arduino that we need to support it better.

Yes, floats should work, they just make your sketch take up a lot more program space on the Arduino board. You can't print them with the Serial commands however.

To convert degrees to radians, multiply by pi (3.14159) and divide by 180:

float rad = deg * 3.14159 / 180;

I just came across this trying to convert a serial input to double using atof

Am keen to get a fix, so registering my interest :slight_smile:

Output
/cygdrive/d/apps/arduino-0007/tools/avr/bin/../lib/gcc/avr/4.0.2/../../../../avr/lib/avr5/libc.a(floatsisf.o): In function __floatunssisf': ../../../libm/fplib/floatsisf.S:65: multiple definition of __floatsisf'

/cygdrive/d/apps/arduino-0007/tools/avr/bin/../lib/gcc/avr/4.0.2/avr5/libgcc.a(_si_to_sf.o):/home/hbarragan/t/gcc/gcc/fp-bit.c:1334: first defined here

/cygdrive/d/apps/arduino-0007/tools/avr/bin/../lib/gcc/avr/4.0.2/../../../../avr/lib/avr5/libc.a(fixsfsi.o): In function __fixsfsi': ../../../libm/fplib/fixsfsi.S:57: multiple definition of __fixsfsi'

/cygdrive/d/apps/arduino-0007/tools/avr/bin/../lib/gcc/avr/4.0.2/avr5/libgcc.a(_sf_to_si.o):/home/hbarragan/t/gcc/gcc/fp-bit.c:1404: first defined here

Wait, so what you are saying is that math.h does not work at all?

That's bad news! I was planning to use sqrt and sine and cosine to compute some GPS stuff!

When will this be fixed?

-Z-

Actually, I think I fixed this for Arduino 0008. At least, I can compile a sketch that uses atof() and sin(), which I think didn't work before.

Ah, that does seem to work alright, and including a sin() didn't increase the sketch size as much as I thought it would! Sorry I didn't realize I was reading an old thread.

Thanks, and keep up the great work!

-Z-