(球拍/方案)扣除收益率令人难以置信的小幅度 [英] (Racket/Scheme) Subtraction yields result off by incredibly small margin

查看:140
本文介绍了(球拍/方案)扣除收益率令人难以置信的小幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正忙于如何设计程序 - 使用计划/球拍;我在R5RS版本的Scheme中碰到了一个非常奇怪的特性。

当进行一个简单的减法运算时,尽管使用带有小数点精度的值,将会是预期的。



例如,给出以下减法操作:

 ( -  5.00 4.90)
=> 0.09999999999999965

当一个人应该期待一个干净的0.10?整数减法按预期工作;

 > ( -  5 4)
=> 1
> ( - 5 4.9)
=>例如,练习3.1.1




下一步是为每个函数组成示例。确定有多少与会者能够以3.00美元,4.00美元和5.00美元的票价支付演出。使用这些示例来制定一个通用规则,显示如何根据票价计算出席者人数。根据需要补充更多的例子。

这个例子中的规则是从票价中扣除每$ 0.10,另外还有15个人参加。所以,像这样的快速功能将工作..

 (define(attendees ticket-price)
(+ 120( *(/( - 5.00票价)0.10)15)))

然而, (相当讨厌)..

 > (出勤率4.90)
=> 134.99999999999994

所以在尝试从教科书(非常相同)的解决方案之后..



$ $ p $ code $(定义(出勤价格)
(+(*(/ 15 .10)( - 5.00价格))
120))

我得到了完全相同的错误。这是我的本地系统特定的东西吗?我很好奇 - 事实上,我可以简单地使用某种形式的四舍五入来纠正这一点。但我宁愿知道为什么它是第一次发生的!



除此之外,我建议任何对Lisp感兴趣的人看看HTDP。这似乎是一个相当温柔的文字,从掠过它,非常手。我喜欢SICP - 但那更像是一个长期的事情;我似乎花了好几天的时间做阅读,然后练习,然后赶上网上的讲座。 HTDP似乎更像是'哦,我有15分钟的备用 - 我会看看HTDP '。

解决方案

浮点数并不总是以期望的确切值存储。见例如浮点危险



如果你需要精确的计算,你可以在你的情况下使用整数算术,通过存储价值在美分而不是美元。

 (define (出席者票价 - 美分)
(+ 12000(*(/( - 500票价 - 美分)10)1500)))


I'm currently messing around with "How To Design Programs" - using Scheme/Racket; I've come across a really peculiar feature in the R5RS version of Scheme.

When conducting a simple subtraction, albeit using values with decimal point accurace, answers are minutely off what would be expected.

For example; given the following subtraction operation:

(- 5.00 4.90)
=> 0.09999999999999965

When one should surely be expecting a clean 0.10? Whole number subtraction works as expected;

> (- 5 4)
 => 1
> (- 5 4.9)
 => 0.09999999999999965

Take for example, Exercise 3.1.1:

The next step is to make up examples for each of the functions. Determine how many attendees can afford a show at a ticket price of $3.00, $4.00, and $5.00. Use the examples to formulate a general rule that shows how to compute the number of attendees from the ticket price. Make up more examples if needed.

The rule in the example that was for every $0.10 taken off of the ticket prices, 15 more people would attend. So a quick function like this would work..

(define (attendees ticket-price)
  (+ 120 (* (/ (- 5.00 ticket-price) 0.10) 15)))

However, this returns the (rather annoying)..

> (attendance 4.90)
 => 134.99999999999994

So after trying the (pretty much identical) solution from the text book..

(define (attendance price)
  (+ (* (/ 15 .10) (- 5.00 price))
    120))

I got exactly the same error. Is this something specific with my local system? I'm pretty curious - in reality I could simply use some form of rounding to correct this; but I'd rather know why it's occurring first!

Apart from that, I advise anyone interested in Lisp to look at HTDP. It appears to be quite a gentle text from skimming it, very hands on. I do like SICP - but thats more of a long term thing; I seem to spend a good few days doing the reading, then the exercises, then catching up with lectures online. HTDP seems to be a bit more like 'Oh, I've got 15 minutes spare - I'll look at HTDP'.

解决方案

Floating-point numbers are not always stored as the exact values you expect. See e.g. The Perils of Floating Point.

If you want exact calculations, you could use integer arithmetic in your case by storing the values in cents instead of dollars.

(define (attendees ticket-price-cents)
  (+ 12000 (* (/ (- 500 ticket-price-cents) 10) 1500)))

这篇关于(球拍/方案)扣除收益率令人难以置信的小幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆