All mock tests are FREE till 31 December — register to save your progress

Consider the Java function: public static float f(long m, long n) { float result = (float)m / (float)n; if (m < 0 || n < 0) return 0.0f; else result -= f(m * 2, n * 3); return result; } Which value best approximates f(1, 3)?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: A. 0.2

Explanation

f(1,3) = 1/3 − 2/9 + 4/27 − … until the longs overflow to negative and return 0. The alternating geometric series sums to (1/3)/(1 + 2/3) = 0.2.

More Programming Languages and Computer Graphics questions