Trouble with sine

Hello, I have to write a scientific calculator program and I'm not allowed to use any maths libabries (not even <math.h>) I am doing very well so far, but I'm having trouble calculating the sine of a number. Here is my get_sine function
1
2
3
4
5
6
float get_sine(float f_operand)
{
    return f_operand - get_exponentiation(f_operand, 3) / get_factoral(3) + 
    get_exponentiation(f_operand, 5) / get_factoral(5) - 
    get_exponentiation(f_operand, 7) / get_factoral(7);
}
I tested this with 7 and I got -73.5097, but my calculator (and every other calculator) says that the sine of 7 is 0.6569... (I am using radians). I looked on wikipedia to find out how to calculate the sine, and I did exactly what is said and I'm still getting the wrong number. Does anybody know what I'm doing wrong?

Thankyou.
Last edited on
This approximation of sin will work only within a narrow range. The further you get from that narrow range, the more inaccurate it is.

Here is a graph showing your approximation.

http://www.wolframalpha.com/input/?i=plot+%28x+-+%28x%5E3%29%2F%283%21%29+%2B+%28x%5E5%29%2F%285%21%29+-+%28x%5E7%29%2F%287%21%29%29+from+x%3D-10+to+x+%3D+10++y+%3D+-2+to+y+%3D+2

As you can see, outside of the range x= -pi to x = pi, it's very wrong.

However, you can be a bit smarter about this. You know that sin pi = sin 3pi = sin 5pi = sin 7pi etc etc, so whatever value the user enters, you can add or subtract 2pi to it until it is within the accurate range of your approximation, and then calculate the answer.

For example, user enters 7. You subtract 2pi, giving you (7-2pi) = 0.717, and then you put THAT into your approximation, and get an answer of 0.65, as expected.

The problem here is not your code; it's that you don't quite understand what you're calculating. This is actually a very neat demonstration of what programming really is; all that junk about memorising syntax and making libraries link is just learning how to hold the tools. This is about understanding your problem and using the tools available to solve them.

Last edited on
Moschops is right. You have to tear the problem apart. Taking a look at his link there are a couple of things to test and calculate.

Is the number between 0 and 2PI? What can be done if not.
How precise should the result be?
Calculate the faculty.
Loop with alternating sign.
Increasing the exponent.

After all your work is done you got the solution.
sin (x) == sin (x+2pi)

sin (x) does NOT equal sin (x + pi)


You must add or subtract 2pi, not pi.

Edit: Oh, he removed his post with his finished code.
Last edited on
Sorry about that Moschops, I thought I was being stupid :) here it is again
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float get_sine(float f_operand)
{
    float f_tempOperand = f_operand;
    while (f_tempOperand > f_pi)
    {
        f_tempOperand -= f_pi;
    }
    while (f_tempOperand < -f_pi)
    {
        f_tempOperand += f_pi;
    }
    return f_tempOperand - get_exponentiation(f_tempOperand, 3) / get_factoral(3) + 
    get_exponentiation(f_tempOperand, 5) / get_factoral(5) - 
    get_exponentiation(f_tempOperand, 7) / get_factoral(7);
}
Thankyou everyone for helping :)
Last edited on
Right, but since sin(x) == -sin(-x) you can simply calculate these values.
1
2
3
4
5
6
7
 {
        f_tempOperand -= 2 * f_pi;
    }
    while (f_tempOperand < -f_pi)
    {
        f_tempOperand +=2 *  f_pi;
    }
Topic archived. No new replies allowed.