Checking that floating points arguments are correct

隐身守侯 提交于 2019-12-23 17:16:37

问题


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. Choose some epsilon and compare with epsilon error. Of course it will now accept some matrices violating the transition matrix property, but in general, if someone occasionally passes some bad data into the constructor, he will get an exception.
  2. Don't check anything, rely on the class' user, if he passes something bad, it is completely his fault, and the behavior of the class will be unexpected.

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


回答1:


Do the second one.

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?

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.

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."

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.)



来源:https://stackoverflow.com/questions/15984406/checking-that-floating-points-arguments-are-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!