java - Inifinte Recursive loop I can't Solve? -
i developing program recursively generates frequencies of tones in scale. each frequency twelfth root of 2 higher previous one. program must use recursion (ugh, education). when use method, initial tone repeated on , on , on again. why 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); }
why that?
because of java's integer division. specifically, 1/12
becomes 0
, not 0.083333333
, because int
must yielded. 2
raised power 0
1
, y
same z
.
use double
literals force floating-point division.
1.0/12.0
Comments
Post a Comment