While loop condition to execute until certain number of decimal places of accuracy is achieved

조회 수: 9 (최근 30일)
Hi so I am trying to run something in a while loop until a certain number of decimal place is achieve:
so lets say
a = 0.003
b = 0.02
and the absolute difference between them is:
difference = abs(a-b)
and I want to increment a whilst leaving b constant until I get an Accuracy of 3 decimal places for the difference between them.
How would I go about doing this?
Thanks.
  댓글 수: 2
Liam Ryan
Liam Ryan 2019년 10월 12일
Meaning do it until the variable difference has an accurraccy of 3 decimal places.
So for instance:
if you start of with a = 0.003
and b = 0.2
the difference is: abs(0.003-0.2) = 0.1970.
But I want the difference between them to be really small, so I want the difference to be:
0.000AAAAA
where those AAAAA represents the non zero numbers.
So for 4 decimal place accuracy: want:
0.0000AAAA
Thanks

댓글을 달려면 로그인하십시오.

답변 (1개)

David Hill
David Hill 2019년 10월 12일
Increment by half the difference or whatever you want.
d=1;
while d>.001
a=a+(a-b)/2;
d=abs(a-b);
end
  댓글 수: 2
Liam Ryan
Liam Ryan 2019년 10월 12일
hi so does d>.001make them have an accuraccy of 3 or 2 decimal places?
Thanks
Image Analyst
Image Analyst 2019년 10월 12일
3. It will keep going until the first 3 decimal places of the difference are 0 and there is only a non-zero difference in the 4th or later decimal places.

댓글을 달려면 로그인하십시오.

카테고리

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