Like it? :)
Printable View
Like it? :)
Added in the raise to power function to get rid of the java.lang package.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;
}
}
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.
Could you teach me please? I am new to java. How do I make it more efficient?
Untested, but it should do about the same as your code, except for the order of the parts.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;
}
}
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 whileCode:sign = -sign;
And i used \n to make new lines.
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.
Quote:
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.
I showed you in my code. Directly put the \n in the string.
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.Quote:
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....
But 250,123 (or 250.123 if that is your convention) is not.
Hehe....sorry for that one....in Norway we use , as the decimal seperator....:D...CornedBee got it right I think.
PS: You guys can't belive how many bugs I have had after doing that in programming..:D
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.