|
-
Feb 11th, 2004, 12:24 AM
#1
Thread Starter
Fanatic Member
For loop
Code:
import java.math.*;
class Stuff{
public static void main(String[] args) {
int var4;
var4 *= (3.14/180); // convert from degrees to radians;
double sin_var4;
for (int j = 5; j <= 10; j++){
sin_var4 += Math.pow(var4,j);
}
System.out.println(sin_var4);
}}
Doesn't work... sigh always says sin_var4 0.
For those of you who might ask what sin_var4 is... (or what I am trying to do).
I am trying to write the defination of Sine function, which in Maclaurin series is x - x^3/3! + x^5/5! - ... x^n/n! + x^n/n! ... etc
and also seems like the compiler says sin_var4 is undefined.. why??

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 11th, 2004, 04:57 AM
#2
var4 lacks an initial value. Also it should be a double, as int lacks the precision you need. Use the PI constant from the Math class instead of 3.14, it's more exact. There is no java.math package, Math is in java.lang and therefore doesn't need an import. sin_var4 lacks an initial value too.
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 11th, 2004, 11:40 PM
#3
Thread Starter
Fanatic Member
Why must I define it to be double? Because PI is infinitely digited? If I use 3.14 I can use float right? Since the range is enough and save space.
And why must I initialize a default value? Doesn't it have a default value of 0.0 when I define it?

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 12th, 2004, 12:14 AM
#4
Thread Starter
Fanatic Member
And is there anyway to make the For () statement increase 4 each time instead of 1??

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 12th, 2004, 01:46 AM
#5
Thread Starter
Fanatic Member
and say i have some variable x inside a for(){x += 1;} condition, how can I view that x outside the bracket?(i need x because is a sum of all stuff.)

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Feb 12th, 2004, 07:36 AM
#6
Originally posted by prog_tom
And why must I initialize a default value? Doesn't it have a default value of 0.0 when I define it?
Everything in Java is defaulted to null. If you try to use a variable before assigning it an initial value, the compiler should yell at you.
Its been a while since I played with Java, so I can't tell if what you've done is acceptable or not.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 12th, 2004, 07:36 AM
#7
Originally posted by prog_tom
And is there anyway to make the For () statement increase 4 each time instead of 1??
Code:
for (int j = 5; j <= 10; j+=4){
sin_var4 += Math.pow(var4,j);
}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 12th, 2004, 07:37 AM
#8
Originally posted by prog_tom
and say i have some variable x inside a for(){x += 1;} condition, how can I view that x outside the bracket?(i need x because is a sum of all stuff.)
Declare it outside of the for block.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Feb 13th, 2004, 04:55 AM
#9
Originally posted by prog_tom
1) Why must I define it to be double? Because PI is infinitely digited? If I use 3.14 I can use float right? Since the range is enough and save space.
2) And why must I initialize a default value? Doesn't it have a default value of 0.0 when I define it?
1) Float would probably suffice. There's no datatype with infinite digits (duh!), so it's only a matter of precision. However:
You currently declare it as int.
sin_var4 is double, and it's always a good idea to have variables in a calculation have the same type. The repeated conversion from float to double would in the long run take so long that it is worth the 4 additional bytes of the double to save this time. And you get better precision, too.
2) No, variables are not initialized.
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
|