필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by