From ba2cca01968a47df2c00d4dd6a1d518ec9530176 Mon Sep 17 00:00:00 2001 From: zomjie Date: Thu, 14 Aug 2025 15:40:37 +0800 Subject: [PATCH] =?UTF-8?q?(cat=20<<=20'EOF'=20[201=5F3]=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90truncate/=E3=80=81truncate-quotient=E5=92=8Ctruncate-r?= =?UTF-8?q?emainder=E7=9A=84=E5=AE=9E=E7=8E=B0=E3=80=81=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=92=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在goldfish/scheme/base.scm中实现了truncate/、truncate-quotient和truncate-remainder函数 - 在goldfish/liii/base.scm中导出了新函数 - 在tests/goldfish/liii/base-test.scm中添加了完整的单元测试和文档 - 所有测试通过验证 修复子任务: - [x] truncate/ - [x] truncate-quotient - [x] truncate-remainder 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude EOF )) --- devel/201_3.md | 6 +- goldfish/liii/base.scm | 2 +- goldfish/scheme/base.scm | 8 +- tests/goldfish/liii/base-test.scm | 218 ++++++++++++++++++++++++++++++ 4 files changed, 229 insertions(+), 5 deletions(-) diff --git a/devel/201_3.md b/devel/201_3.md index 2dbe96aa..18fde3ce 100644 --- a/devel/201_3.md +++ b/devel/201_3.md @@ -29,9 +29,9 @@ + [ ] floor/ + [x] floor-quotient + [ ] floor-remainder -+ [ ] truncate/ -+ [ ] truncate-quotient -+ [ ] truncate-remainder ++ [x] truncate/ ++ [x] truncate-quotient ++ [x] truncate-remainder + [x] quotient + [x] remainder + [x] modulo diff --git a/goldfish/liii/base.scm b/goldfish/liii/base.scm index 9aa4521c..d719261e 100644 --- a/goldfish/liii/base.scm +++ b/goldfish/liii/base.scm @@ -26,7 +26,7 @@ ; R7RS 6.2: Numbers square exact inexact max min floor s7-floor ceiling s7-ceiling truncate s7-truncate round s7-round floor-quotient gcd lcm s7-lcm modulo exact-integer-sqrt - numerator denominator exact-integer? + numerator denominator exact-integer? truncate/ truncate-quotient truncate-remainder ; R7RS 6.3: Booleans boolean=? ; R7RS 6.4: list diff --git a/goldfish/scheme/base.scm b/goldfish/scheme/base.scm index 122050f2..c4b33b8d 100644 --- a/goldfish/scheme/base.scm +++ b/goldfish/scheme/base.scm @@ -22,7 +22,7 @@ ; R7RS 6.2: Numbers square exact inexact max min floor s7-floor ceiling s7-ceiling truncate s7-truncate round s7-round floor-quotient gcd lcm s7-lcm modulo boolean=? exact-integer-sqrt - numerator denominator exact-integer? + numerator denominator exact-integer? truncate/ truncate-quotient truncate-remainder ; R7RS 6.4: list pair? cons car cdr set-car! set-cdr! caar cadr cdar cddr null? list? make-list list length append reverse list-tail @@ -185,6 +185,12 @@ (define (floor-quotient x y) (floor (/ x y))) + (define (truncate/ x y) (truncate (/ x y))) + + (define (truncate-quotient x y) (truncate (/ x y))) + + (define (truncate-remainder x y) (- x (* (truncate-quotient x y) y))) + (define s7-modulo modulo) (define (modulo x y) diff --git a/tests/goldfish/liii/base-test.scm b/tests/goldfish/liii/base-test.scm index b9e93689..0ec8bbb7 100755 --- a/tests/goldfish/liii/base-test.scm +++ b/tests/goldfish/liii/base-test.scm @@ -1898,6 +1898,224 @@ wrong-type-arg (check-catch 'wrong-number-of-args (quotient 10)) (check-catch 'wrong-number-of-args (quotient 5 3 2)) +#| +truncate/ +用于计算两个数的截断除法,返回向零方向取整的商。 + +语法 +---- +(truncate/ dividend divisor) + +参数 +---- +dividend : real +被除数 +divisor : real +除数,不能为零 + +返回值 +----- +real? +向零方向取整的商 + +示例 +---- +egin{code} +(truncate/ 11 2) ⇒ 5 +(truncate/ -11 2) ⇒ -5 +(truncate/ 11 -2) ⇒ -5 +(truncate/ -11 -2) ⇒ 5 +(truncate/ 10 3) ⇒ 3 +(truncate/ 10 -3) ⇒ -3 +(truncate/ -10 3) ⇒ -3 +(truncate/ -10 -3) ⇒ 3 +\end{代码} + +与floor-quotient的区别 +-------------- +truncate/与floor-quotient的主要区别在于对负数除法的处理: +- truncate/:向零取整(截断除法),如(truncate/ -11 2) ⇒ -5 +- floor-quotient:向负无穷取整,如(floor-quotient -11 2) ⇒ -6 + +错误 +---- +division-by-zero +当除数为零时抛出错误。 +wrong-type-arg +当参数不是数字时抛出错误。 +|# + +(check (truncate/ 11 2) => 5) +(check (truncate/ -11 2) => -5) +(check (truncate/ 11 -2) => -5) +(check (truncate/ -11 -2) => 5) + +(check (truncate/ 10 3) => 3) +(check (truncate/ 10 -3) => -3) +(check (truncate/ -10 3) => -3) +(check (truncate/ -10 -3) => 3) + +(check (truncate/ 0 2) => 0) +(check (truncate/ 0 -2) => 0) + +(check (truncate/ 10.5 3.0) => 3.0) +(check (truncate/ 10.5 -3.0) => -3.0) +(check (truncate/ -10.5 3.0) => -3.0) +(check (truncate/ -10.5 -3.0) => 3.0) + +(check-catch 'division-by-zero (truncate/ 11 0)) +(check-catch 'division-by-zero (truncate/ 0 0)) +(check-catch 'wrong-type-arg (truncate/ 1+i 2)) +(check-catch 'wrong-type-arg (truncate/ 'hello 2)) +(check-catch 'wrong-number-of-args (truncate/ 10)) +(check-catch 'wrong-number-of-args (truncate/ 5 3 2)) + +#| +truncate-quotient +用于计算两个数的截断商 + +语法 +---- +(truncate-quotient dividend divisor) + +参数 +---- +dividend : real +被除数 +divisor : real +除数,不能为零 + +返回值 +----- +real? +向零方向取整的商,与truncate/结果相同 + +示例 +---- +egin{code} +(truncate-quotient 11 2) ⇒ 5 +(truncate-quotient -11 2) ⇒ -5 +(truncate-quotient 11 -2) ⇒ -5 +(truncate-quotient -11 -2) ⇒ 5 +(truncate-quotient 0 2) ⇒ 0 +\end{代码} + +与floor-quotient的区别 +-------------- +truncate-quotient与floor-quotient的主要区别在于对负数除法的处理: +- truncate-quotient:向零取整(截断除法),如(truncate-quotient -11 2) ⇒ -5 +- floor-quotient:向负无穷取整,如(floor-quotient -11 2) ⇒ -6 + +错误 +---- +division-by-zero +当除数为零时抛出错误。 +wrong-type-arg +当参数不是数字时抛出错误。 +|# + +(check (truncate-quotient 11 2) => 5) +(check (truncate-quotient -11 2) => -5) +(check (truncate-quotient 11 -2) => -5) +(check (truncate-quotient -11 -2) => 5) + +(check (truncate-quotient 10 2) => 5) +(check (truncate-quotient 10 -2) => -5) +(check (truncate-quotient -10 2) => -5) +(check (truncate-quotient -10 -2) => 5) + +(check (truncate-quotient 10 3) => 3) +(check (truncate-quotient 10 -3) => -3) +(check (truncate-quotient -10 3) => -3) +(check (truncate-quotient -10 -3) => 3) + +(check (truncate-quotient 0 2) => 0) +(check (truncate-quotient 0 -2) => 0) + +(check (truncate-quotient 10.5 3.0) => 3.0) +(check (truncate-quotient 10.5 -3.0) => -3.0) +(check (truncate-quotient -10.5 3.0) => -3.0) +(check (truncate-quotient -10.5 -3.0) => 3.0) + +(check-catch 'division-by-zero (truncate-quotient 11 0)) +(check-catch 'division-by-zero (truncate-quotient 0 0)) +(check-catch 'wrong-type-arg (truncate-quotient 1+i 2)) +(check-catch 'wrong-type-arg (truncate-quotient 'hello 2)) +(check-catch 'wrong-number-of-args (truncate-quotient 10)) +(check-catch 'wrong-number-of-args (truncate-quotient 5 3 2)) + +#| +truncate-remainder +用于计算两个实数的截断余数 + +语法 +---- +(truncate-remainder dividend divisor) + +参数 +---- +dividend : real +被除数 +divisor : real +除数,不能为零 + +返回值 +----- +real? +截断除法的余数,满足:(truncate-remainder x y) = x - y * (truncate-quotient x y) + +示例 +---- +egin{code} +(truncate-remainder 11 2) ⇒ 1 +(truncate-remainder -11 2) ⇒ -1 +(truncate-remainder 11 -2) ⇒ 1 +(truncate-remainder -11 -2) ⇒ -1 +(truncate-remainder 10 3) ⇒ 1 +(truncate-remainder 10 -3) ⇒ 1 +(truncate-remainder -10 3) ⇒ -1 +(truncate-remainder -10 -3) ⇒ -1 +(truncate-remainder 0 5) ⇒ 0 +\end{代码} + +性质 +---- +truncate-remainder满足以下性质: +- (truncate-remainder x y) = (- x (* y (truncate-quotient x y))) +- 结果的符号与被除数相同 +- |truncate-remainder| < |divisor| + +错误 +---- +division-by-zero +当除数为零时抛出错误。 +wrong-type-arg +当参数不是数字时抛出错误。 +|# + +(check (truncate-remainder 11 2) => 1) +(check (truncate-remainder -11 2) => -1) +(check (truncate-remainder 11 -2) => 1) +(check (truncate-remainder -11 -2) => -1) + +(check (truncate-remainder 10 3) => 1) +(check (truncate-remainder 10 -3) => 1) +(check (truncate-remainder -10 3) => -1) +(check (truncate-remainder -10 -3) => -1) + +(check (truncate-remainder 0 2) => 0) +(check (truncate-remainder 0 -2) => 0) + +(check (truncate-remainder 5 1) => 0) +(check (truncate-remainder -5 -1) => 0) + +(check-catch 'division-by-zero (truncate-remainder 11 0)) +(check-catch 'division-by-zero (truncate-remainder 0 0)) +(check-catch 'wrong-type-arg (truncate-remainder 1+i 2)) +(check-catch 'wrong-type-arg (truncate-remainder 'hello 2)) +(check-catch 'wrong-number-of-args (truncate-remainder 10)) +(check-catch 'wrong-number-of-args (truncate-remainder 5 3 2)) + #| remainder 计算两个实数相除的余数。 -- Gitee