We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
I tried to write a recursion function to calc a number. There seems to have no syntax or runtime error, but I just could not get the result right (result I got are all zeros). I need your help to inspect my codes here. Thanks a lot!
FloatList RSVs = new FloatList();
void setup() {
for (int i = 20; i < 100; i++) {
float ram = random(-20, 120);
RSVs.append(ram);
}
println(calcK(3));
getKs();
printArray(Ks);
}
FloatList Ks = new FloatList();
void getKs() {
for (int i = 0; i < RSVs.size(); i++) {
Ks.append(calcK(i));
}
}
float calcK(int RSVIndex) {
float K;
if (RSVIndex >= 0) {
K = (6-1)/6*calcK(RSVIndex-1)+1/6*RSVs.get(RSVIndex);
} else {
K = 50;
}
return K;
}
Answers
(6-1)/6
= 0 & 0 *calcK(RSVIndex-1)
= 01/6
= 0 & 0 *RSVs.get(RSVIndex)
= 00 + 0 = 0 thus K = 0!
https://forum.Processing.org/two/discussions/tagged/division
https://forum.Processing.org/two/discussions/tagged/float
@GoToLoop, Thanks a lot! How careless I am to miss it