|
-
Feb 12th, 2004, 01:59 AM
#1
Thread Starter
Fanatic Member
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
-
Feb 12th, 2004, 06:17 PM
#2
Thread Starter
Fanatic Member
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
-
Feb 13th, 2004, 05:36 AM
#3
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.
-
Feb 13th, 2004, 11:41 AM
#4
Thread Starter
Fanatic Member
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
-
Feb 13th, 2004, 02:06 PM
#5
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.
-
Feb 13th, 2004, 03:02 PM
#6
Thread Starter
Fanatic Member
Wow, amazing. I like your
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
-
Feb 13th, 2004, 05:29 PM
#7
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.
-
Feb 14th, 2004, 12:29 AM
#8
Thread Starter
Fanatic Member
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
-
Feb 14th, 2004, 06:23 AM
#9
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.
-
Feb 14th, 2004, 04:03 PM
#10
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....
-
Feb 14th, 2004, 10:20 PM
#11
Thread Starter
Fanatic Member
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
-
Feb 15th, 2004, 05:31 AM
#12
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.
-
Feb 15th, 2004, 06:27 AM
#13
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..
-
Feb 15th, 2004, 08:38 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|