Reputation: 235
I am developing a program that recursively generates the frequencies of the tones in a scale. Each frequency is the twelfth root of two higher than the previous one. The program must use recursion (ugh, education). When I use this method, I just get the initial tone repeated over and over and over again. Why is that?
public static void scale(double x, double z){
double y;
if(z == x){
y = z * Math.pow(2, (1/12));
System.out.println(y);
scale (y, y);
}
else if(z >= (2 * x) - 1 || z <= (2 * x) + 1){
y = z;
System.out.println();
}
else{
y = z * Math.pow(2, (1/12));
scale (y, y);
}
System.out.println(y);
}
Upvotes: 1
Views: 56
Reputation: 178303
Why is that?
Because of Java's integer division. Specifically, 1/12
becomes 0
, not 0.083333333
, because an int
must be yielded. Then 2
raised to the power 0
is 1
and y
is the same as z
.
Use double
literals to force floating-point division.
1.0/12.0
Upvotes: 6