Quick logic question (Sine function)

Hi, I think I'm finally almost done with this program. I can get my result somewhat close but I think I know what's the problem. The purpose of the program is to compute the Sine function w/o Math.sin().

Code java:

for (int i = 0; i <= MaxE; i++)
{
fact = fact*(2*i+1);
Sine += Math.pow(-1,i)*Math.pow(Rad,2*i+1)/fact;
}

MaxE is a user input of the maximum exponent that the series will go up to

Rad is the user input for the radians

When I input 30 (degrees), the sine should be .49999999... or roughly .5 but after the calculations, the program out puts 0.47827350788327533. What I think the program is doing is calculating the factorials as

1! = 1

3! = 1 * 3

5! = 1 * 3 * 5

7! = 1 * 3 * 5 * 7

etc..

instead of doing a normal factorial

1! = 1

3! = 1 * 2 * 3

5! = 1 * 2 * 3 * 4 * 5

etc..

I was just curious if my thought process was correct, if so, I was wondering if any could give me a hint as to what I should do to fix this.

*edit* By the way, my idea for solving this problem was trying to create another factorial but instead of finding the odd numbers, rewrite another line to do even numbers and the multiply the two, would this work?

here's the whole code if you were wondering why I said 30 degrees

Code java:

import java.io.*;
public class d
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
String s;
double Rad, Sine;
int MaxE, fact;
Rad = 0.0;
Sine = 0.0;
fact = 1;
System.out.println("Degrees (d) or Radians (r)? Enter (d) or (r)" );
s = keybd.readLine();
s = s.toLowerCase();
s = s.trim();
if (s.equals("d"))
{
System.out.println("Enter a value (Degrees): ");
Rad = Double.parseDouble(keybd.readLine());
Rad = (Math.PI * Rad) / 180;
}
else if (s.equals("r"))
{
System.out.println("Enter a value (Radians): ");
Rad = Double.parseDouble(keybd.readLine());
}
System.out.println("Enter the desired maximum exponent: ");
MaxE = Integer.parseInt(keybd.readLine());
for (int i = 0; i <= MaxE; i++)
{
fact = fact*(2*i+1);
Sine += Math.pow(-1,i)*Math.pow(Rad,2*i+1)/fact;
}
System.out.println("The value of Sine is " + Sine);
System.out.println("Check: Sine is " + Math.sin(Rad));
}
}

Thank you and any help is appreciated! Also, I apologize if this isn't the right forum for this topic

Re: Quick logic question (Sine function)

I believe the problem is in your loop. For factorials, I think it is best to use a recursive method. I recommend reading this. If you scroll down a bit, you can find a nice example of a factorial method which implements recursion.

Re: Quick logic question (Sine function)

factorials can be calculated much easier using a loop/iterative method.

There's only one factorial, where you multiply every integer between 1 and n (not just the odd or even ones).

Also, what exponent are you taking the taylor series out to? That could effect your results (either by mathematical error, or due to truncation error). This is highly unlikely, though.

edit:

nvm didn't see that there wasn't a factorial function. Yeah, you're not computing the factorial correctly. I would recommend actually writing a factorial method that would calculate the factorial of a number rather than try to compute it "inline".

Re: Quick logic question (Sine function)

This might be a long shot, but I'm guessing you're taking Bajic?

If you do, email him your code and ask him. He responds relatively quickly, and is very pointed in what needs to be changed.

Re: Quick logic question (Sine function)

Thank you everyone for letting me know that my factorial function was definitely wrong. I'll try and use a recursion method but I don't think I'll get it to work the first time, but I'll keep trying.

Quote:

Originally Posted by

**kg0nzales**
This might be a long shot, but I'm guessing you're taking Bajic?

If you do, email him your code and ask him. He responds relatively quickly, and is very pointed in what needs to be changed.

Yes, I am currently taking his class but I don't think I'll be turning this assignment in. I wasn't sure if he'd respond to my e-mail since I thought he'd want us to figure it out on our own. I'm kind of just doing this one to finally get it to work (and to learn how to use recursion).

Re: Quick logic question (Sine function)

Asking a professor can never hurt :P Most professors I've had have been quite generous in how much they'll help you (especially if they see you're making an honest attempt to learn). Having someone point out where your code isn't quite right is a **good thing** (more so if you understand why it's wrong, and if you know how to fix it).

Re: Quick logic question (Sine function)

Quote:

Originally Posted by

**helloworld922**
Asking a professor can never hurt :P Most professors I've had have been quite generous in how much they'll help you (especially if they see you're making an honest attempt to learn). Having someone point out where your code isn't quite right is a **good thing** (more so if you understand why it's wrong, and if you know how to fix it).

I'll try and ask them next time I have a problem. It just usually isn't my first choice because of my shy personality. It makes me feel really awkward and I always tend to go in front of their office and pace back and forth for several minutes deciding if I should go and talk to them or not. But anyways... the solutions for the assignment was posted, so now I have a definite answer. It seems I wasn't as close as I thought :(