sicp每日一题[2.10]

embedded/2024/9/22 17:46:16/

Exercise 2.11

In passing, Ben also cryptically comments: “By testing the signs of the endpoints of the intervals, it is possible to break m u l − i n t e r v a l mul-interval mulinterval into nine cases, only one of which requires more than two multiplications.” Rewrite this procedure using Ben’s suggestion.
After debugging her program, Alyssa shows it to a potential user, who complains that her program solves the wrong problem. He wants a program that can deal with numbers represented as a center value and an additive tolerance; for example, he wants to work with intervals such as 3.5±0.15 rather than [3.35, 3.65]. Alyssa returns to her desk and fixes this problem by supplying an alternate constructor and alternate selectors:

(define (make-center-width c w)(make-interval (- c w) (+ c w)))
(define (center i)(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)(/ (- (upper-bound i) (lower-bound i)) 2))

Unfortunately, most of Alyssa’s users are engineers. Real engineering situations usually involve measurements with only a small uncertainty, measured as the ratio of the width of the interval to the midpoint of the interval. Engineers usually specify percentage tolerances on the parameters of devices, as in the resistor specifications given earlier.


这道题目难度并不大,就是有点麻烦,要把每一种情况的上界和下界都分析出来。至于区间采用置信区间的表示方法,只要用宽度的一半除以中间值就行了。

; 把 w 改为百分比形式
(define (make-center-width center percent)(let ((tolerance (* center (/ percent 100))))(make-interval (- center tolerance) (+ center tolerance))))(define (center i)(average (lower-bound i) (upper-bound i)))(define (percent i)(* (/ (/ (- (upper-bound i) (lower-bound i)) 2)(center i))100))(define (display-interval-tolerance i) (newline)(display (center i))(display " ± ")(display (percent i))(display "%")); 记 x 的下界为 x1,上界为 x2;记 y 的下界为 y1,上界为 y2
; 将他们按 x1 x2 y1 y2 从左到右排序,并按位置记为 1, 2, 3, 4
; 则按照 x 和 y 的区间端点数字的符号,可以分成以下9种情况:
; - - - -   min: 2*4, max: 1*3
; - - - +   min: 1*4, max: 1*3
; - - + +   min: 1*4, max: 2*3
; - + - -   min: 2*3, max: 1*3
; - + - +   min: min(1*4, 2*3), max: max(1*3, 2*4)
; - + + +   min: 1*4, max: 2*4
; + + - -   min: 2*3, max: 1*4
; + + - +   min: 2*3, max: 2*4
; + + + +   min: 1*3, max: 2*4
(define (mul-interval x y)(let ((x1 (lower-bound x))(x2 (upper-bound x))(y1 (lower-bound y))(y2 (upper-bound y))(p1 (* (lower-bound x) (lower-bound y)))    ; 即 1*3(p2 (* (lower-bound x) (upper-bound y)))    ; 即 1*4(p3 (* (upper-bound x) (lower-bound y)))    ; 即 2*3(p4 (* (upper-bound x) (upper-bound y))))   ; 即 2*4(cond ((and (negative? x2) (negative? y2)) (make-interval p4 p1))((and (negative? x2) (negative? y1) (positive? y2)) (make-interval p2 p1))((and (negative? x2) (positive? y1)) (make-interval p2 p3))((and (negative? x1) (positive? x2) (negative? y2)) (make-interval p3 p1))((and (negative? x1) (positive? x2) (negative? y1) (positive? y2)) (make-interval (min p2 p3) (max p1 p4)))((and (negative? x1) (positive? x2) (positive? y1)) (make-interval p2 p4))((and (positive? x1) (negative? y2)) (make-interval p3 p2))((and (positive? x1) (negative? y1) (positive? y2)) (make-interval p3 p4))((and (positive? x1) (positive? y1)) (make-interval p1 p4)))))(define a (make-interval -6.12 7.48))
(define b (make-interval -4.465 4.935))
(display-interval (mul-interval a b))
(display-interval-tolerance (mul-interval a b)); 执行结果
[-33.3982, 36.9138]
1.7577999999999996 ± 2000.0000000000007%

http://www.ppmy.cn/embedded/112280.html

相关文章

13. 神经网络基本骨架--nn.Module

nn.Module的基本使用 1. 什么是nn.Module nn.Module模块式神经网络模型的基类,它提供了一些很简单方便的结构,来构建一个神经网络 在pytorch的官方文档(https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module)中提供了一个基于 nn.Module 的简单…

平安养老险阜阳中心支公司开展金融教育宣传专项活动

为全面深入开展“金融教育宣传月”的各项工作,不断完善金融惠民利民举措,提升金融服务质效,帮助基层群众增强维权意识、防非反诈的自我保护能力。近日,平安养老保险股份有限公司(以下“平安养老险”)阜阳中…

基础GAN生成式对抗网络(pytorch实验)

(Generative Adversarial Network) 一、理论 https://zhuanlan.zhihu.com/p/307527293?utm_campaignshareopn&utm_mediumsocial&utm_psn1815884330188283904&utm_sourcewechat_session 大佬的文章中的“GEN的本质”部分 二、实验 1、数…

什么是内存溢出,golang是如何解决内存溢出的

什么是内存溢出? 内存溢出(Memory Overflow)是指程序在运行时超出了分配给它的内存限制,从而导致程序异常或崩溃的现象。通常,内存溢出是由于以下原因引起的: 内存泄漏:程序分配了内存但没有及…

QXml 使用方法

VS2019 QT 编译工具链问题解决 使用winqtdeploy.exe 打包环境就可以正常运行,缺少某一个运行库引起的 简易使用python脚本编译运行 Python3 中的 slots 和 QT 中的 slots 宏定义重复, 放在不同的文件中进行调用可以避免 还是比较习惯从源码包引入(方便定…

生产环境下Nuxt3如何设置部署端口号?

Nuxt3默认的端口号3000.如果我们在一台服务器中部署多个Nuxt应用,都是3000端口必然会冲突,所以需要修改默认的端口号。在官网文档中,介绍的端口号修改方式是修改env环境变量。这个方式在Vercel或者一些serverless环境非常方便,在自…

C# List定义和常用方法

栏目总目录 List的定义 列表&#xff08;List&#xff09;是一种常用的集合类型&#xff0c;它属于System.Collections.Generic命名空间。列表是一个有序集合&#xff0c;可以包含重复的元素&#xff0c;并且可以根据索引访问元素。 List< T > List<T> 是一个泛…

富文本中去掉 HTML 和 CSS 样式,只保留纯文本

要从富文本中去掉 HTML 和 CSS 样式&#xff0c;只保留纯文本&#xff0c;可以使用以下几种方法&#xff1a; 1. 纯 JavaScript 方法 你可以使用 JavaScript 的 innerText 或 textContent 来提取文本&#xff0c;而忽略 HTML 标签和样式。 function stripHtml(html) {var te…