CW 6.3 SE Library Math Function Problems

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CW 6.3 SE Library Math Function Problems

Jump to solution
1,928 Views
nhpete
Contributor III


Disclaimer - I am a relative newbie to both CW and the C programming language - be gentle!

 

My next project will utilize an S08 target and will require small amounts of low-accuracy (32 bit) floating point math as well as a few trigonometric operations. To that end I have been experimenting with both floating point routines and trig functions in CW 6.3 SE and have encountered a few problems.

 

1. I have found that the only way to get the library math functions SQRT, SIN, COS to work at all was to declare prototype functions prior to their use. Based on my limited C knowledge, I didn't think one had to declare prototype functions when using standard library functions.  What am I doing wrong here?

 

2. While SIN and COS seem to produce the correct answers, they also seem to be overwriting numerous previously declared variables when they execute.  When I step through the functions themselves while monitoring the memory locations of the previously declared variables, things are ok until the final return statements in the trig functions are executed at which point all sorts of garbage is written into the low memory where the previously declared variables (used to) reside.  Interestingly enough, the TAN function does not seem to be guilty of this activity.  Of course, I don't need to use the TAN function.  What I am doing wrong here?

 

The "new project wizard" in CW chose all include files in the project based on my answers to its questions.  It chose ansifs.lib for the math functions. I have included the main.c file with this request.  If others are needed, let me know.

 

Any help would be GREATLY appreciated.

 

Pete

Original Attachment has been moved to: main.c.zip

Labels (1)
0 Kudos
1 Solution
590 Views
kef
Specialist I

For prototypes you need to include math.h.

#include <math.h>

Functions from math.h need quite a lot of stack space and default PRM file specifies 80 bytes or so. Try increasing that number to 300 or more.

View solution in original post

0 Kudos
3 Replies
591 Views
kef
Specialist I

For prototypes you need to include math.h.

#include <math.h>

Functions from math.h need quite a lot of stack space and default PRM file specifies 80 bytes or so. Try increasing that number to 300 or more.

0 Kudos
590 Views
nhpete
Contributor III

Edward:

Each of your answers was right on the money.

I included math.h to main.c and I no longer have to include prototypes for the math functions.

I increased the stack size to 300 in the .prm file and no longer overwrite my other variables.

You have been extreemly helpful.  Thank you.

By the way, I also noted that my variables seem to be initialized to zero prior to any operations.  I don't believe they were prior to the addition of that file.  Is that something done by math.h as well?


Thanks much.

Pete

0 Kudos
590 Views
kef
Specialist I


Pete,

standard ANSI C startup routine should always initialize static storage variables. Including math.h shouldn't have effect on this. Stack overflow could make some variables overwritteb and make you thinking that some variables were not initialized.

0 Kudos