Search code examples
calgorithmrecursiongame-physics

Simple Recursion Help


I have a simple recursive function that calculates a simple pendulum swing decay, using the ratio of height1:height2 of 0.98.

The function has a base case of 0.0, but for some reason it turns into infinite self-calls!

Can anyone spot what I'm missing?

Code:

float swingDecay (float value) {


     if ( value == 0.00 ) {
          return value;
     }

     else { 
          return swingDecay (value * 0.98);  }     
}

mIL3S www.milkdrinkingcow.com


Solution

  • Due to floating point calculations never being exact in floating point math you never get to value == 0.00. You might want to try something like value < 0.0000001 or something like that and tweak it where it works.