## 第三週測驗 ### 測驗一 ```c /* ceiling division without needing floating-point operations. */ static size_t ceil_div(size_t n, size_t d) { return (n+d-1)/d; } ``` 這是採取 [`include/linux/math.h`](https://github.com/torvalds/linux/blob/master/include/linux/math.h) 當中整數除法並向上取整的巨集實作,它的原理是歸納所有可能的餘數,例如 ${n = d \times q + r}$ (其中商數為 $q$ ,餘數為 $r$ ),會發現到餘數會介於 $0$ 以及 $d-1$ 之間。因此當我們將數字加上 $d-1$ 時,會發現在 C 語言的整數除當中,原先餘數為 0 的被除數會得到相同的商數,但是其他被除數在加了 $d-1$ 後會有向上取整數的效果。達成我們無條件進位的要求。 ```c #define INTMAX BBBB void mpi_set_u64(mpi_t rop, uint64_t op) { size_t capacity = ceil_div(64, 31); mpi_enlarge(rop, capacity); for (size_t n = 0; n < capacity; ++n) { rop->data[n] = op & INTMAX; op >>= 31; } for (size_t n = capacity; n < rop->capacity; ++n) rop->data[n] = 0; } ``` 這裡帶到了不同精確度的整數實作,以上述的函式來說是 bitops 中的 set 應用在無號64位元的整數。在多精確度整數的實作當中,可以發現它是以