From ee51c3ca997cfcf3418517a2841da07cb9723c92 Mon Sep 17 00:00:00 2001 From: Tim Guo Date: Mon, 20 Apr 2015 10:16:33 -0700 Subject: [PATCH 1/3] remove redundant condition in ex2-72 --- exercise/ex2-72.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercise/ex2-72.c b/exercise/ex2-72.c index 3b805ce..f1e7b8e 100644 --- a/exercise/ex2-72.c +++ b/exercise/ex2-72.c @@ -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)); } From b386ed27ba42799216eaced88dcfeede1b1f2442 Mon Sep 17 00:00:00 2001 From: Tim Guo Date: Wed, 29 Apr 2015 12:35:53 -0700 Subject: [PATCH 2/3] improve grammar --- exercise/ex2-79.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/exercise/ex2-79.c b/exercise/ex2-79.c index ddc5339..7c83402 100644 --- a/exercise/ex2-79.c +++ b/exercise/ex2-79.c @@ -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); From dbbbedef6dc945fc83db372b6cf20081e58bdefb Mon Sep 17 00:00:00 2001 From: Tim Guo Date: Sun, 3 May 2015 23:26:56 -0700 Subject: [PATCH 3/3] correct answers for ex2-88 --- exercise/ex2-88.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercise/ex2-88.txt b/exercise/ex2-88.txt index 42edb86..b4e7669 100644 --- a/exercise/ex2-88.txt +++ b/exercise/ex2-88.txt @@ -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