Q: Assume x[i] is a single floating pointing number (“float”) complying with IEEE 754. Calculate the geomean of x[i] without FPU.
Ans.
int geomean(float *x, int n) // n is the amount of x[i] {}
$$
\text{GM} = \sqrt[n]{x_1 \cdot x_2 \cdots x_n}
$$
解題想法
我的想法是,將輸入的浮點數轉換成定點數,使用定點數進行所有乘法運算,計算整體乘積。最後使用 牛頓法 求出乘積的 $n$ 次方根,得到近似的幾何平均值。