检查浮点参数是否正确 [英] Checking that floating points arguments are correct

查看:131
本文介绍了检查浮点参数是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个类来表示Markov链(让我们把它命名为 MC )。它有一个构造函数,它接受状态转换矩阵(也就是 vector > ,我想这是一个好主意,一个矩阵(具有相同数目的行和列),并且实际上是一个过渡矩阵:其中的所有数字都是概率,即不小于 0.0 1.0 ,并且对于每一行,其元素的总和是 1.0 。浮点限制:例如, 0.3 + 0.3 + 0.3 + 0.1 的总和不等于 1.0 所以我看到了这个问题的两种可能的解决方案:

I want to write a class representing Markov chain (let's name it MC). It has a constructor, which takes the state transition matrix (that is, vector<vector<double>>. I suppose, it is a good idea to check it is really a matrix (has the same number of rows and columns) and is really a transition matrix: all the numbers in it are probabilities, that is, no less than 0.0 and no greater than 1.0, and for every row the sum of its elements is 1.0. However, there is a problem which arises from floating point limitations: for example, the sum 0.3 + 0.3 + 0.3 + 0.1 will not be equal to 1.0, so the check will not be that easy. So I see two possible solutions of that problem:


  1. 选择一些epsilon并与epsilon错误进行比较当然,它现在将接受一些违反转换矩阵属性的矩阵,但是一般来说,如果有人偶尔将一些坏数据传递到构造函数中,他会得到一个异常。

  2. 不检查任何东西,依靠类的用户,如果他传递的东西坏,这是完全他的错,这个类的行为将是意想不到的。

什么方法更好,更真实世界?

What approach is better and more "real-world"? I like the first, but again, not sure how should I choose epsilon.

推荐答案

做第二个。

你的类不是在汇总浮点数的列表和决定什么是接近到1,什么不是。您的用户是。你的类代表马可夫链。您将无法选择ε的值,以便您的类以有用的方式表示马尔科夫链。

Your class isn't in the business of summing up lists of floating-point numbers and deciding what's "close enough" to 1 and what isn't. Your user is. Your class represents Markov chains. You won't be able to choose a value of epsilon so that your class represents Markov chains in a useful way.

想想您要实现的操作。也许你将有一个函数,其命中概率分布的链的状态与链的转移矩阵。如果该函数检查输入概率分布是否是一些ε内的概率分布?

Think about the operations you're going to implement. Maybe you're going to have a function that hits a probability distribution on the states of the chain with the chain's transition matrix. Should that function check whether the input probability distribution is a probability distribution within some epsilon?

您的函数几乎肯定不会保留is a probability distribution属性;你会得到一些漂移,由于舍入误差远离概率分布的空间,你反复击中你的概率分布的马尔科夫链。

Your function almost certainly won't preserve the "is a probability distribution" property; you'll get some drift due to rounding error away from the space of probability distributions as you repeatedly hit your probability distribution by your Markov chain. You can correct this by normalising afterward, but that causes even more inaccuracy.

现在考虑给定一个马尔科夫链和一个整数k,返回由形成的马尔科夫链迭代输入链k次操作。你可以看到,这将累积四舍五入并遭受与与马可夫链的命中概率分布相同的问题。

Now think about the "given a Markov chain and an integer k, return the Markov chain formed by iterating the input chain k times" operation. You can see that this will accumulate roundoff and suffer from much the same problems as "hit probability distribution with Markov chain."

如果你只有一个在使用12小时后断裂的东西之间的选择和不必要的不​​准确的东西?

Wouldn't it suck if you only had a choice between stuff that breaks after 12 hours of use and stuff that's unnecessarily inaccurate?

(检查方阵矩阵参数的矩形性和矩阵当然是完全合理的。 )

(Checking the squareness and matrixness of the square matrix argument is, of course, totally reasonable.)

这篇关于检查浮点参数是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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