GATE / CS ARCHIVE

GATE CSE 2003 / QUESTION 1

Exponential-series accumulation

Programming in C · Control Flow & Loop Tracing · Full GATE CSE 2003 paper

GATE 2003, Q1 1 mark · MCQ
Report issue

Control Flow & Loop Tracing

Question 1, GATE 2003: For large positive integer y, what does this C function approximate?
float f(float x, int y) { float p, s; int i; for(s=1,p=1,i=1; i<y; i++){p*=x/i; s+=p;} return s; }
A. x^y; B. e^x; C. ln(1+x); D. x^x.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. The recurrence for p is p₀=1 and pᵢ=pᵢ₋₁x/i. Induction gives pᵢ=xⁱ/i!. The accumulator includes the initial term 1, so it contains exactly y terms, not y+1. Comparing with the Taylor expansion eˣ=1+x+x²/2!+⋯ identifies the limit.

2. Initially p=1 and s=1. On iteration i, p is multiplied by x/i, so it becomes xi/i!, which is added to s. The returned value is the truncated series sum from i=0 to y−1 of xi/i!, approaching ex for fixed x as y increases. Thus B in the mathematical arithmetic model; finite floating-point range/roundoff can limit the actual approximation.

Original paper, page 1