Results 1 to 14 of 14

Thread: Selfmade Sine function :p

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Talking Selfmade Sine function :p

    Like it?
    Last edited by prog_tom; Feb 12th, 2004 at 06:18 PM.

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  2. #2

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post Updated

    Code:
    /*+++++++++++++++++++++++++++++++++
     * Sin(x) 
     * Tom Zhang
     * 
     * Demonstrates power of math without
     * calling the java.lang package
     *++++++++++++++++++++++++++++++++*/
    
    public class Mathematica{
      public static void main(String[] args){
        /*+++++++++++++++++++++++++++
          DECLARATION OF VARIABLES **
        /*++++++++++++++++++++++++++*/
          int a;
          double b = 0, d = 0;
          final double c = 0.1;
    
        /*+++++++++++++++++++++++++++
          MAIN
        /*++++++++++++++++++++++++++*/
             System.out.println("++++++++++++++++++++++++++++++++++++" +
                                '\n' + "++ approximation of Sin(" + c + ") by Tom Zhang ++" + '\n' +           
    
                                "++++++++++++++++++++++++++++++++++++");
        /*++++++++++++++++++++++++++++
          The "odd" and positive terms
        /*++++++++++++++++++++++++++++*/ 
          for (a = 1; a <= 100; a++,a++,a++,a++) {
             b += Degree(c,a)/(Factorial(a));
             System.out.println("(" + a + "): " + b + "." + " Factorial: " + (Factorial(a)) + ".");
          }
        /*++++++++++++++++++++++++++++
          The "odd" and negative terms
        /*++++++++++++++++++++++++++++*/      
          for (a = 3; a <= 100; a++,a++,a++,a++) {
             d += Degree(c,a)/(Factorial(a));
             System.out.println("(" + a + "): " + (d*(-1)) + "." + " Factorial: " + (Factorial(a)) + 
    
    ".");
          }
          System.out.println(b + (d*(-1)));
      }
      public static double Factorial(int a) {
         double j = 1.0;
         for(int i = 1; i < a; i++) {
         	j *= i;
         }
      return j*a;
      }
      public static double Degree(double a, double b) {
      	double j = 1.0;
      	for (int i = 1; i <= b; i++) {
      		j *= a;
      	}
      	return j;
      }
    }
    Added in the raise to power function to get rid of the java.lang package.

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Don't like it at all.
    I think your biggest problem is that you calculate the Factorial from scratch every time, but there are some other places for improvement too, like unnecessary locals. It would also be more efficient to unify the two loops.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    Could you teach me please? I am new to java. How do I make it more efficient?

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    /*+++++++++++++++++++++++++++++++++
    * Sin(x) 
    * Tom Zhang
    * 
    * Demonstrates power of math without
    * calling the java.lang package
    *++++++++++++++++++++++++++++++++*/
    
    // Rewritten by Sebastian "CornedBee" Redl
    
    public class Mathematica {
    	public static void main(String[] args) {
    		// Removed. Variables are declared when needed. At least I do it ;)
    		// Variable names have been changed to have meaning, too.
    		final double input = 0.1;
    
    		// Any specific reason for having the \n as separate chars?
    		System.out.println("++++++++++++++++++++++++++++++++++++\n" +
    						   "++ approximation of Sin(" + input + ") by Tom Zhang ++\n" +
    						   "++++++++++++++++++++++++++++++++++++");
    
    		// Unified to a single loop. There's really no reason to
    		// separate this.
    		double sign = -1.0;
    		double running_fac = 1.0;
    		double sum = 0.0;
    		for (int a = 1; a <= 100; a += 2) {
    			running_fac *= a;
    			sign = -sign;
    			// In C++, you could do some trickery to speed this up.
    			// In Java, it should be possible too, in theory. But
    			// practically it's just not feasible.
    			sum += sign*(Pow(input, a) / running_fac);
    			System.out.println("(" + a + "): " + (sign*sum) + "." + " Factorial: " + (running_fac) + ".");
    		}
    
    
    		System.out.println(sum);
    	}
    
    	// For simple powers
    	public static double Pow(double a, int b) {
    		double r = 1.0;
    		while(b > 0) {
    			b -= 1;
    			r *= a;
    		}
    		return r;
    	}
    }
    Untested, but it should do about the same as your code, except for the order of the parts.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    Wow, amazing. I like your
    Code:
    sign = -sign;
    Everytime it runs the code it goes +- +-... amazing. Simple but amazing, I wish I had more insight, perhaps take me a while

    And i used \n to make new lines.

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, but you had it as a separate char. I don't know if the compiler optimizes away constant string concatenations, but in general string concatenation is quite slow in Java (though it will be faster in 1.5) and should be reduced.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    Originally posted by CornedBee
    Yes, but you had it as a separate char. I don't know if the compiler optimizes away constant string concatenations, but in general string concatenation is quite slow in Java (though it will be faster in 1.5) and should be reduced.

    Could you show me the best way to make a new line in Java? I am new, so anything from you will be greatly appreciated from me.

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I showed you in my code. Directly put the \n in the string.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    If you only need the sin values from the 360 degrees and not like 250,123, a look up table like the one in the Quace 2 engine can be preety fast....

  11. #11

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    Originally posted by NoteMe
    If you only need the sin values from the 360 degrees and not like 250,123, a look up table like the one in the Quace 2 engine can be preety fast....
    A Sine table? Usually Sine tables have 0 -> 360 deg. Since 250 and 123 are in 360deg, it would be included as well.

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But 250,123 (or 250.123 if that is your convention) is not.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hehe....sorry for that one....in Norway we use , as the decimal seperator.......CornedBee got it right I think.


    PS: You guys can't belive how many bugs I have had after doing that in programming..

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I had a bug in a .Net app because the framework, set to the German locale, wouldn't accept . as the decimal point in a settings file and I didn't know how to do locale-unaware floating point parsing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width