0

I'm quite good enough with Java syntax and just decided to put it to work by creating a code for sine based on an algorithm I created earlier. I know Math.sin helps you evaluate sine but I just for the fun, decided to go on an create my own source code. However, angles between 60° and 120° and also between 240° and 300° give wrong answers and I have no idea why. Could someone please help me find the error? I've tried everything to detect it but failed.


    import java.util.Scanner;

    public class Sine {
       public static void main(String[] args) {
          // This code solves sine according yo the general expansion of sine
          // sin x = x - x³/3! +x^5/5! - x^7/7! +...

          Scanner scanner = new Scanner(System.in);
          double answer = scanner.nextDouble();
          scanner.close();
          answer = simplify(answer);
          answer = converttoradian(answer);
          answer = continued(answer);
          System.out.println(answer);
       }

       // This Makes all the angles that are more than 360
       // To become less than 360 and Generates the simplified
       // Angles for obtuse and reflex angles

       static double simplify(double x) {
          if (x >= 360) {
             x = x - 360;
             return simplify(x);
          }
          else if (x <= -360) {
             x = x + 360;
             return simplify(x);
          }
          else if (x > 90 && x <= 270) {
             x = 180 - x;
             return x;
          }
          else if (x >= 270) {
             x = x - 360;
             return x;
          }
          else if (x <= -90 && x > -270) {
             x = -x - 180;
             return x;
          }
          else if (x <= -270) {
             x = x + 360;
             return x;
          }
          else {
             return x;
          }
       }

       // Simple enough and explains itself
       // Converts the angles to radian

       static double converttoradian(double d) {
          d *= Math.PI;
          d /= 180.0;
          return d;
       }

       // This Method about to open generates each term and adds them together
       // The number of terms solved in this case is 33

       static double continued(double d) {
          double answer = 0.0;
          int index = 1;
          double one = d;
          for (int i = 0; i < 33; i++) {
             double result = 0.0;
             for (int x = 1; x < index; x++) {
                d = d * one;
             }
             long here = factorial(index);
             result = d / here;
             if ((index - 1) % 4 == 0) {
                answer = answer + result;
                index = index + 2;
             }
             else {
                answer = answer - result;
                index = index + 2;
             }
          }
          return answer;
       }

       // Evaluates factorials

       static long factorial(int n) {
          long one = 1;
          long m = (long) n;
          if (m == 0 || m == 1) {
             one = 1;
             return one;
          }
          else {
             while (m > 1) {
                one *= m;
                m--;
             }
             return one;
          }
       }
    }

WJS
  • 36,363
  • 4
  • 24
  • 39
Isaac
  • 23
  • 5

1 Answers1

1

There was a lot going on in your program and some unnecessary code. You were on the right track, though. I made some changes to simplify the calculations. You should be able to follow them.

Specifically.

  1. Alternate the sign. Start out with sign = 1, then set sign = -sign for subsequent terms.
  2. For the denominator and factorial, I just used the for loop, starting at 1 and incrementing by 2 to get 1,3,5,7
  3. For powers of the same value, I just multiplied d by a dSquared constant to achieve the same.
  4. I rewrote the factorial to make it simpler.
  5. To reduce large values of d I just used the remainder operator to get them less than 360.
  6. I added some print statements to show calculation progress and to ensure things were working correctly.
  7. And finally, the maximum factorial that will fit in a long is 20!. After that they turn negative due to overflow. So the number of terms needed to be reduced.

public class Sine {
   public static void main(String[] args) {

      // This code solves sine according yo the general expansion of sine
      // sin x = x - x³/3! +x^5/5! - x^7/7! +...

      for (double degrees = 0; degrees < 700; degrees += 17) {
         double simplified_degrees = simplify(degrees);
         System.out.println("simplified_degrees = " + simplified_degrees);
         double radians = converttoradian(simplified_degrees);
         System.out.println("radians = " + radians);

         double sin = continued(radians);
         System.out.println(sin);
         System.out.println(Math.sin(radians));
         System.out.println("------------------------------------------");
      }
   }
   // This Makes all the angles that are more than 360
   // To become less than 360 and Generates the simplified
   // Angles for obtuse and reflex angles
   static double simplify(double x) {

      x = x % 360;

      return x;
   }
   // Simple enough and explains itself
   // Converts the angles to radian

   static double converttoradian(double d) {
      return Math.PI / 180. * d;
   }
   // This Method about to open generates each term and adds them together
   // The number of terms solved in this case is 33
   static double continued(double d) {
      double result = 0;
      double sign = 1;
      double dSquared = d * d;

      int pow = 1;
      for (int pow = 1;  pow < 21; pow += 2) {

         long fact = factorial(pow);
         System.out.println("d = " + d + ", fact = " + fact + ", pow = " + pow
               + ", sign = " + sign);
         result = result + (d / fact) * sign;

         d *= dSquared; // effective powers 3, 5, 7,9
         sign = -sign; // alternate sign for every other term

      }
      return result;
   }
   // Evaluates factorials
   static long factorial(int n) {
      if (n == 0 || n == 1) {
           return 1;
      }
      long fact = 1;
      for (long i = 2; i <= n; i++) {
          fact *= i;
      }
      return fact;
    }
}
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Why in the world would you evaluate the factorial recursively? – Mad Physicist Oct 27 '19 at 17:26
  • @MadPhysicist. In practice I wouldn't. It just sort of popped out because it was easy. But you are correct. I wouldn't want to lead someone astray so I will change it. – WJS Oct 27 '19 at 17:29
  • Thanks so much. The angle doesn't need to be converted into its acute equivalent. – Isaac Nov 28 '19 at 05:09