Need guidance with stacks

I'm fairly new to C++ and need help. This is my hyperbolic sin/cos recursive function. How do I place the angle on a sine or cosine stack that represents a call to the sine or cosine? When the program returns, I want to examine the stack for how many times the hyp sine was called and how many times hyp sine/cosine was called vs. the value I'm trying to find.

Also, how would I go about putting the results in a table? I want the range of values from -1 to 1 in .1 radian increments.

How would I find out if the number of function calls agree with what I predict it should be?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
using namespace std;

float h(float);
float g(float);

int main(int argc, char** argv) {
    //Testing out recursive trig functions
    float angDeg=45;
    float angRad=angDeg*atan(1)/45;
    cout<<"Angle = "<<angDeg<<" sinh = "<<sinh(angRad)<<
            " our h = "<<h(angRad)<<endl;
    cout<<"Angle = "<<angDeg<<" cosh = "<<cosh(angRad)<<
            " our g = "<<g(angRad)<<endl;
    //Exit stage right
    return 0;
}

float h(float angR){
    float tol=1e-6;
    if(angR>-tol&&angR<tol)return angR+angR*angR*angR/6;
    return 2*h(angR/2)*g(angR/2);
}
float g(float angR){
    float tol=1e-6;
    if(angR>-tol&&angR<tol)return 1+angR*angR/2;
    float b=h(angR/2);
    return 1+2*b*b;
}
You could count your function calls with global variables, incrementing them each time you enter the particular function.

Why are you playing with angles in degrees and the value of pi if you are dealing with HYPERBOLIC sinh and cosh? They aren't trig functions: they are the odd and even parts of exp(x).

It looks like you are using the double-argument formulae to do the recursion. You might like to call your functions something a little closer to sinh and cosh than h and g. To detect the base case you can also simplify to if ( abs(angR) < tol ) before you apply the first two terms of the Maclaurin series for each function.

You will get a much more accurate answer (and a substantially bigger range) if you use double rather than float.

If you want a table of values then call your sinh and cosh replacements in a loop from main().

Clever method, though: I like it!

Last edited on
Topic archived. No new replies allowed.