PDA

Click to See Complete Forum and Search --> : For loop


prog_tom
Feb 10th, 2004, 11:24 PM
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??

CornedBee
Feb 11th, 2004, 03:57 AM
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.

prog_tom
Feb 11th, 2004, 10:40 PM
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
Feb 11th, 2004, 11:14 PM
And is there anyway to make the For () statement increase 4 each time instead of 1??

prog_tom
Feb 12th, 2004, 12:46 AM
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.)

crptcblade
Feb 12th, 2004, 06:36 AM
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.

crptcblade
Feb 12th, 2004, 06:36 AM
Originally posted by prog_tom
And is there anyway to make the For () statement increase 4 each time instead of 1??


for (int j = 5; j <= 10; j+=4){
sin_var4 += Math.pow(var4,j);
}

crptcblade
Feb 12th, 2004, 06:37 AM
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.

CornedBee
Feb 13th, 2004, 03:55 AM
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.