# [2020q1](http://wiki.csie.ncku.edu.tw/linux/schedule) 第 6 週測驗題 ###### tags: `linux2020` :::info 目的: 檢驗學員對 bitwise、數值系統和浮點數的認知 ::: --- ### 測驗 `1` 考慮到以下浮點數除法程式: (fdiv.c) ```cpp #include <stdio.h> #include <stdlib.h> double divop(double orig, int slots) { if (slots == 1 || orig == 0) return orig; int od = slots & 1; double result = divop(orig / D1, od ? (slots + D2) >> 1 : slots >> 1); if (od) result += divop(result, slots); return result; } ``` 假設 `divop()` 的第二個參數必為大於 `0` 的整數,而且不超過 `int` 型態能表達的數值上界。請補完程式碼。 ==作答區== `D1` = ? * `(a)` 4 * `(b)` 3 * `(c)` 2 * `(d)` 1 `D2` = ? * `(a)` 4 * `(b)` 3 * `(c)` 2 * `(d)` 1 :::success 延伸問題: 1. 解釋上述程式碼運作原理; 2. 以編譯器最佳化的角度,推測上述程式碼是否可透過 [tail call optimization](https://en.wikipedia.org/wiki/Tail_call) 進行最佳化,搭配對應的實驗; 3. 在 Linux 核心原始程式碼找出類似的技巧; ::: ---
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up