SICP 1.3.3 二分法作为方程的根

本文介绍了一个关于abs函数括号闭合错误的问题,并通过具体的Scheme代码示例展示了如何定位和解决这类错误。文中涉及的函数包括half-interval-method等,旨在帮助读者理解并掌握调试技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次所犯的错误是abs函数括号闭合错了,不懂得怎么调Bug,有点烦。
(define (search f neg-point pos-point)
  (let ((mid-point (average neg-point pos-point)))
    (if (close-enough? neg-point pos-point)
      mid-point
      (let ((test-value (f mid-point)))
        (cond ((positive? test-value) (search f neg-point mid-point))
              ((negative? test-value) (search f mid-point pos-point))
              (else mid-point))))))

(define (close-enough? x y)
  (< (abs (- x y)) 0.001))

(define (average x y)
  (/ (+ x y) 2))

(define (half-interval-method f a b)
  (let ((a-value (f a))
        (b-value (f b)))
    (cond ((and (negative? a-value) (positive? b-value)) 
           (search f a b))
          ((and (negative? b-value) (positive? a-value))
           (search f b a))
          (else 
            (error "Values are not of opposite sign" a b)))))

(newline)
(display (half-interval-method sin 2.0 4.0))

(newline)
(display (half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))
                               1.0
                               2.0))

(newline)
(display (half-interval-method sin 2.0 3.0))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值