zoukankan      html  css  js  c++  java
  • SICP_2.7-2.11

    
    
    (define (make-interval x y)
      (cons x y))
    
    (define (add-interval x y)
      (make-interval (+ (lower-bound x) (lower-bound y))
                    (+ (upper-bound x) (upper-bound y))))
    
    ;;;;;;;;;;;;;;;2.7
    (define (upper-bound inter)
      (cdr inter))
    
    (define (lower-bound inter)
      (car inter))
    ;;;;;;;;;;;;;;2.8
    (define (sub-interval x y)
      (add-interval x
                    (make-interval (- (upper-bound y))
                                  (- (lower-bound y)))))
    
    ;;;;;;;;;;;;;;2.9
    (define (width-interval x)
      (/ (- (upper-bound x) (lower-bound x)) 2))
    
    ;(width-interval (add-interval x y)) (1)
    ;(width-interval (sub-interval x y))  (2)
    ;(+ (width-interval x) (width-interval y)) (3)
    ;(- (width-interval x) (width-interval y)) (4)
    ;
    ;(1) (3)是等价的
    ;(width-interval (mul-interval x y)) (5)
    ;(* (width-interval x) (width-interval y)) (6)
    ;(5) (6) 不等价
    
    ;;;;;;;;;;;2.10
    (define (div-interval x y)
      (if (< (* (upper-bound y) (lower-bound y)) 0)
          (error "division interval isn't right")
          (mul-interval x
                        (make-interval (/ 1.0 (upper-bound y))
                                       (/ 1.0 (lower-bound y))))))
    
    ;;;;;;;;;2.11
    (define (mul-interval x y)
      (let ((xl (lower-bound x))
            (xu (upper-bound x))
            (yu (upper-bound y))
            (yl (lower-bound y)))
        (cond ((and (< xl 0) (< xu 0)) (cond ((and (< yl 0) (< yu 0))
                                              (make-interval
                                               (* xu yu) (* xl yl)))
                                              ((and (< yl 0) (> yu 0))
                                              (make-interval
                                               (* xl yu) (* xl yl)))
                                              ((and (> yl 0) (> yu 0))
                                              (make-interval
                                               (* xl yu) (* xu yl)))))
              ((and (< xl 0) (> xu 0)) (cond ((and (< yl 0) (< yu 0))
                                              (make-interval
                                               (* xu yl) (* xl yl)))
                                             ((and (< yl 0) (> yu 0))
                                              (make-interval
                                               (* xl yu) (* xu yu)))
                                             ((and (> yl 0) (> yu 0))
                                              (make-interval
                                               (* xl yu) (* xu yu)))))
              ((and (> xl 0) (> xu 0)) (cond ((and (< yl 0) (< yu 0))
                                              (make-interval
                                               (* xu yl) (* xl yu)))
                                             ((and (< yl 0) (> yu 0))
                                              (make-interval
                                               (* xu yl) (* xu yu)))
                                             ((and (> yl 0) (> yu 0))
    (make-interval (
    * xl yl) (* xu yu))))))) ;;;;;;;;;;;;;;;改成这种形式可能是因为当乘法代价比较高时 ;;;;;;;;;;;;;;;可以提高效率
    Yosoro
  • 相关阅读:
    (HDOJ 2034)人见人爱AB
    (UVa 100) The 3n + 1 problem
    (HDOJ 2099)整除的尾数
    (UESTCOJ 1004)8球胜负(eight)
    asp.net中对数据库表插入null空值的问题
    利用模版生成html页
    如何取得IP/用户名等信息
    SQL SERVER的数据类型
    在IIS与.net框架中配置中把扩展名.aspx改成自定义的扩展名.mspx
    c#中什么情况下用(int)什么情况下用Convert.ToInt32?
  • 原文地址:https://www.cnblogs.com/tclan126/p/6379366.html
Copyright © 2011-2022 走看看