什么是浮点/舍入误差的简单示例? [英] What is a simple example of floating point/rounding error?
问题描述
使用浮点变量时,我听说过错误。现在我想解决这个难题,我想我得到一些舍入/浮点错误。所以我终于要弄清楚浮点错误的基础。
什么是浮点/舍入误差的一个简单例子(最好是在C ++中) p>
编辑:例如,我有一个具有成功概率p的事件。我做这个事件10次(p不改变,所有试验是独立的)。 2次成功试验的概率是多少?我有这样编码:
double p_2x_success = pow(1-p,(double)8)* pow double)2)*(double)choose(8,2);这是一个浮点错误的机会吗?
=
$ b < d2>解决方案
for(double d = 0; d!= 0.3; d + = 0.1); // never terminated
I've heard of "error" when using floating point variables. Now I'm trying to solve this puzzle and I think I'm getting some rounding/floating point error. So I'm finally going to figure out the basics of floating point error.
What is a simple example of floating point/rounding error (preferably in C++) ?
Edit: For example say I have an event that has probability p of succeeding. I do this event 10 times (p does not change and all trials are independent). What is the probability of exactly 2 successful trials? I have this coded as:
double p_2x_success = pow(1-p, (double)8) * pow(p, (double)2) * (double)choose(8, 2);
Is this an opportunity for floating point error?
for(double d = 0; d != 0.3; d += 0.1); // never terminates
这篇关于什么是浮点/舍入误差的简单示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!