Skip to content
This repository was archived by the owner on Jun 28, 2019. It is now read-only.

Corrections and improvements #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercise/ex2-72.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
*/
void copy_int(int val, void *buf, int maxbytes)
{
if (maxbytes > 0 && maxbytes >= sizeof(int))
if (maxbytes >= sizeof(int))
memcpy(buf, (void *)&val, sizeof(val));
}
16 changes: 9 additions & 7 deletions exercise/ex2-79.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@
* 1. 5/(8*x)
* 2. (5/8)*x
*
* 对于第一种情况,x != 0 时,所得的结果是 0。x == 0 时,结果是没有定义的。对于这
* 种求值,没有意义。因此,下面我们将题目理解为求值 (5/8)*x。
* 对于第一种情况,x != 0 时,所得的结果是 0。x == 0 时,结果是没有定义的。
* 因此,下面我们将题目理解为求值 (5/8)*x。
*
* 基本的思路是,5*x/8 = x/2 + x/8 + f
*
* f 的取值是 x/2 和 x/8 的小数部分之和 >=1 时,f=1。对于负数的情况,小数点之和
* <= -1,则 f = -1。否则,f=0。
* f 的取值是:
* x为正数,且x/2 和 x/8 的小数部分之和 >=1 时,f=1。
* x为负数,且小数部分之和 <= -1时, f = -1。
* 否则,f=0。
*
* 对于正数的情况,求两个整数的小数点之和就是截断两个数的最低三位,然后右移 3 位。
* 对于正数的情况,求两个整数的小数部分之和就是截断两个数的最低三位,然后右移 3 位。
*
* 考虑负数的情况,我们可以发现,最低三位的数值+(-8)就是余数的数值(此时余数小于
* 0)。对两个整数都这样处理,我们就可以得到两个负数的余数之和。然后加上偏执值右
* 移 3 位,得到的就是小数点之和。只有当符号位和最低三位数值都是 1 时,才会有
* 移 3 位,得到的就是小数部分之和。只有当符号位和最低三位数值都是 1 时,才会有
* f=-1。
*/
int fiveeighths(int x)
{
int sign = (x & INT_MIN) == INT_MIN;
int sign = ((unsigned)x)>>31;
int lowest_one = x & 1;
int lowest_three = !!(x & 7);

Expand Down
6 changes: 3 additions & 3 deletions exercise/ex2-88.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
================================================================================

A. 0x20001
B. 1
C. dx:1, dy: 1e10, dz: -1e10 (浮点运算无结合性
B. x: 1, y: INT_MAX
C. 1(double可以精确存下3*INT_MIN至3*INT_MAX间的任意整数
D. INT_MAX
E. 1
E. x: 0, y: 1