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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Quick logic question (Sine function)

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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);
    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

    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
    Last edited by Actinistia; March 21st, 2011 at 01:54 PM.


  2. #2
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default 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.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

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

    Actinistia (March 24th, 2011)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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".
    Last edited by helloworld922; March 21st, 2011 at 11:12 PM.

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

    Actinistia (March 24th, 2011)

  6. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #5
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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 View Post
    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. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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. #7
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Quick logic question (Sine function)

    Quote Originally Posted by helloworld922 View Post
    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

Similar Threads

  1. [SOLVED] Sine function, can't get factorial to work
    By Actinistia in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 27th, 2012, 12:24 PM
  2. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  3. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 09:30 AM
  4. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 10:43 PM
  5. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM