0

I want to find pi in MATLAB and when I do compare it with the pi that is already embodied in MATLAB. So when I write

while(p~=pi)             

the loop seems endless because it keeps testing for all the digits that the MATLAB pi has.

So when I wrote:

p=3.1416;
if p==pi
  disp('yes');
else
  disp('no');
end

the answer naturally was no. So I want to find a way to keep only five digits after the point and test with that, test for pi=3.14159.

Can anyone help?

SCFrench
  • 8,244
  • 2
  • 31
  • 61
system
  • 81
  • 1
  • 2
  • 4

3 Answers3

6
if abs(p-pi) <= 1e-5
  disp yes;
else
  disp no;
end

See this Stack Overflow answer for details.

Community
  • 1
  • 1
SCFrench
  • 8,244
  • 2
  • 31
  • 61
0

Look at the function round2 on the File Exchange. It lets you round to a specific number of decimal places. E.g. for your example:

if round2(p,1e-5) == round2(pi,1e-5),
    disp('yes');
end
n00dle
  • 5,949
  • 2
  • 35
  • 48
  • Note that round2 does NOT actually round to a specified number of decimal places. round2(1.23,.1) actually produces the number 1.20000000000000017763568394002504646778106689453125 –  May 26 '11 at 19:29
  • The problem of course is that one cannot represent the number 1.2 exactly in matlab. ALWAYS beware the limits of floating point precision. –  May 26 '11 at 19:31
  • Ah good point. But surely if pi and p are the same, then the closest FP value from round2 should also the same? – n00dle May 26 '11 at 20:08
  • There is another flaw with your reasoning. One can have two distinct numbers that round2 will send to different results, yet they are indeed closer than the tolerance.>> x = [1.199 1.201]; >> round2(x,0.00725) ans = 1.1963 1.2035 –  May 27 '11 at 10:50
  • That example is entirely dependant on your input rounding value. if you did >> T = [1.199 1.201] >> round2(T,0.01) ans = 1.2000 1.2000. If you then compare the answers, you get: >> ans(1)==ans(2) ans = 1 – n00dle May 27 '11 at 11:15
  • Yes, it is dependent on the tolerance. And that is EXACTLY the problem with using round2. You can get an arbitrary result that may be the same or not, having changing only the tolerance, and that by a small amount. >> round2(x,0.0072) ans = 1.2024 1.2024 >> round2(x,0.00725) ans = 1.1963 1.2035 >> round2(x,0.0073) ans = 1.1972 1.2045 >> round2(x,0.0074) ans = 1.1988 1.1988 –  May 27 '11 at 18:25
0

To compare floating point numbers one should use eps. something along the lines

if abs(p-pi)<=eps .... same

I've also seen 2*eps used in place of eps. But the above is the better way to compare floating points numbers. In your case, it becomes

while abs(p-pi)>2*eps ..... end

--Nasser

Nasser
  • 12,849
  • 6
  • 52
  • 104
  • 1
    Technically, the most accurate use of [EPS](http://www.mathworks.com/help/techdoc/ref/eps.html) would be to pass it the magnitude of the values you are comparing. Calling EPS with no argument will return the distance from `1.0` to the next largest double-precision number (about `2.2204e-016`), but `eps(pi)` will give you a floating-point relative accuracy twice as large (about `4.4409e-016`). – gnovice May 27 '11 at 04:23
  • good point. That is the reason why some use 2*eps as I mentioned. But I see now that your suggestion to do eps(magnitude) is better than doing 2*eps. – Nasser May 27 '11 at 06:43