public static double Exp ( double x , int nTerms ) { if ( nTerms < 2 ) return 1 + x ; if ( nTerms == 2 ) { return 1 + x + ( x * x ) / 2 ; } else { double mult = x * x ; double fact = 2 ; double result = 1 + x + mult / fact ; for ( int i = 3 ; i <= nTerms ; i ++ ) { mult *= x ; fact *= i ; result += mult / fact ; } return result ; } }
compute Exp using Taylor Series .