フィルターのクリア

How do I increase precision in my variables

81 ビュー (過去 30 日間)
Marco Stenborg Petterson
Marco Stenborg Petterson 2018 年 12 月 9 日
回答済み: John D'Errico 2018 年 12 月 9 日
I am working with a large number of observations and I noticed that after a certain threshold I am having some problems of approximation.
An example of the kind of problem I think I have is the following:
rng(1991)
test=rand(100000,1);
out1=sum(test);
out2=ones(1,100000)*test;
out1-out2
ans =
2.4738e-10
I sum a large vector once using the function provided by Matlab and once with an equivalent matrix formulation. Which of the two should I trust? Is there a way to increase the precision of one of the two such that they coincide.
The problem I am actually considering is more complicated and involves taking a gradient of a likelihood. I noticed that there was then a disparity between the numerical gradient calculated by Matlab and the variable assigned to the analytical formula of the gradient I calculated. I am using a HMCSampler and therefore Matlab gives me a warning that the two do not coincide.
I would very much appreciate it if someone could give me some hints on how to approach this difference.
Thank you very much in advance.

採用された回答

Rik
Rik 2018 年 12 月 9 日
This has to do with the way Matlab stores non-integer values: as floating point numbers. With the eps function you can retrieve the precision: 2.2204e-16
So in you case the value you are getting is within the 'rounding' error. There usually isn't a solution, except to avoid comparing values that span a large number of order of magnitude. You can use functions like ismembertol to mitigate the problems this causes.
Sometimes it is possible to remove a step in your calculation that converts to small number and back, which may cause issues with rounding errors becoming significant.

その他の回答 (1 件)

John D'Errico
John D'Errico 2018 年 12 月 9 日
  1. You cannot increase the precision of a variable. Double is it. Unless you want to DECREASE precision, and then you can go to a single.
  2. No matter what, even if the precision were increased to be 100 digits or more, there will still be differences, because of the nature of floating point arithmetic.
So the answer is to never trust the least significant bits of a number computed in floating point. At least not unless you know enough about floating point arithmetic to know why you should never trust those bits. Its sort of a circular thing.
The solution is to learn to use tolerances, and not test for exact equality between floating point numbers.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by