Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Quick logic question (Sine function)

1. ## 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().

```for (int i = 0; i <= MaxE; i++)
{
fact = fact*(2*i+1);
}```

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

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

```import java.io.*;
public class d
{
public static void main(String args[]) throws IOException
{

String s;
int MaxE, fact;
Sine = 0.0;
fact = 1;

System.out.println("Degrees (d) or Radians (r)? Enter (d) or (r)" );
s = s.toLowerCase();
s = s.trim();

if (s.equals("d"))
{
System.out.println("Enter a value (Degrees): ");
}

else if (s.equals("r"))
{
}

System.out.println("Enter the desired maximum exponent: ");

for (int i = 0; i <= MaxE; i++)
{
fact = fact*(2*i+1);
}
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

2. ## 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.

3. ## The Following User Says Thank You to vanDarg For This Useful Post:

Actinistia (March 24th, 2011)

4. ## 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".

5. ## The Following User Says Thank You to helloworld922 For This Useful Post:

Actinistia (March 24th, 2011)

6. ## 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.

7. ## 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.

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).

8. ## Re: Quick logic question (Sine function)

Asking a professor can never hurt 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).

9. ## Re: Quick logic question (Sine function)

Originally Posted by helloworld922
Asking a professor can never hurt 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