Math function not found

Status
Not open for further replies.

johnnyfp

Well-known member
Hi,

I'm trying to use a few math functions, in this example the square function, that I can see are in the math.h library (in hardware\tools\avrteensy\avr\include\math.h) but the compiler or more specifically the linker does not seem to find it.

Code:
#include <math.h>
#include "math.h"

void setup() {                
  float ff = 10;
  float xy = 0;
  xy = square(ff); 
}


void loop() {
;             
}

is an example code. I've tried to include the math library as quotes or angle or both, to no avail.

I keep getting
Code:
error: 'square' was not declared in this scope
or
Code:
undefined reference to `square'
.

I can see that the linker is getting the
Code:
-larm_cortexM4l_math -lm
parameter passed to it.

I've tried this on 1.6.5/1.24 and 1.0.6/1.20 on two different machines.

Have I missed something? Because this compiles fine for a Teensy 2.0 but not the Teensy3.0/3.1/LC

Thanks
 
perhaps its sqrt() you want ?

As far as I know there is no square() function in the math libraries
 
That is not a standard function in the math library, just use x*x :)
There is a pow funtion but for squaring that wont make your code faster or leaner.
 
Trouble is, I'm porting something from an AVR base to and ARM base and it works fine in AVR. And it's not just square that is missing there are a few others not working as well (Kept it simple in this forum post).

My understanding is that unless it's actual hard asm or avr specific ports, and registries then everything else should work. I can see the function in the teensy math header and it does get past the compiler, but it doesn't get linked.
So I'm assuming that it's something missing from the arm_cortexM4l_math linker library. Or I've missed an option or something.
 
No. sqrt is square root. square is squared or x*x

That isn't a standard math function. Perhaps it is something added to the ATmega libraries? It is simple to add via an inline function.

Note, in terms of math libraries, you should use the version that have a 'f' suffix (i.e. sqrtf, sinf, cosf, etc.). On the ATmega chips, the compiler makes double the same as float (which violates ISO/ANSI specifications BTW). On the ARM chips, float is 32-bits and double is 64-bits. If you use a double version of the library (i.e. sqrt) with float arguments, the compiler will have to convert the 32-bit float to 64-bit, call the function, and then convert it back to 32-bits. The library function will be slower because it has to calculate the extra bits of precision. Future versions of the Teensy will likely have hardware support for 32-bit floating point (but not 64-bit floating point). So if you get in the habit of only using float, adding a 'f' suffix to all constants, and using the float version of the math functions, your code will run much faster, when it comes ou.t
 
double or floats really needs a careful decision. In many projects float is sufficient for sure.
But if we run any algorithm where we risk accumulation of roundoff error, for instance PID
controllers, the tradeoff between float speed and double accuracy must be carefully considered.

Not saying any is right, or wrong, its up to the designer to choose whats right for the project.
 
Looks like avr-libc does define square() in math.h. Documentation is here:

http://www.nongnu.org/avr-libc/user-manual/group__avr__math.html#gad11825ff1d6095a08b193c564d524aef

Notice the words "Note This function does not belong to the C standard definition."

You're probably going to have to add something like this:

Code:
#ifndef __AVR__
#define square(x, y) ((x) * (y))
#endif

This is just one of the tiny quirks of avr-libc adding convenient but non-standard stuff, and people actually using it. Simple and only slightly inconvenient to work around when porting to other systems.
 
Ok. That's, I suppose, is good to know and that I'm just haven't done something stupid like missed an option on the build settings.

Will add these and other missing math functions as a seperate library for the teensy 3.1

Thanks everyone.
 
Sigh!
Code:
error: conflicting types for 'square'

I'm not liking this C merlarky. Bring back cobol!
 
Sigh!
Code:
error: conflicting types for 'square'

I'm not liking this C merlarky. Bring back cobol!

lol, sorry, shouldn't laugh: Any chance you could follow the forum rule and give us something to pop in our IDE's to try to help with this more?

Personally I favor Paul's #define fix method for these extra functions you want - if it is a group of reasonably advanced functions then maybe it is worth a library but stuff that can be formed in one line #define scenarios probably should be.
 
I used an inline statement instead of the define, as there where other math functions already defined in the port as such, so was keeping it all the same.

But have now reverted to the define, which works fine. I appologise for my ranting comment, maybe there is a special tag or something that I could use to mark a comment as a rant, to be ignored. :rolleyes:
 
maybe there is a special tag or something that I could use to mark a comment as a rant, to be ignored. :rolleyes:

You needn't apologise for the likes of me Johnny! I liked your rant, found it funny indeed.

<rant>I've seen people use mock html for all sorts of things in forums</rant> and I think it works well enough. <joke quality="lame" length="too-long">just kidding, as if I would tell a long joke that is lame!</joke>
 
Status
Not open for further replies.
Back
Top