tommsch
tommsch

Reputation: 688

Compare symbolic variables

I am trying to compare two symbolic variables (numbers). The whole problem boils down to the following code.

R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))

Both comparisons should return 1, but the second returns 0.


My solution:

digits(51);
R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))

Upvotes: 0

Views: 117

Answers (1)

Marco
Marco

Reputation: 2092

Why you encounter this problem

vpa evaluates symbolic inputs with variable-precision floating-point arithmetic (32 significant digits by default)... So what's happening in your case is

>> R = vpa(0.555555555555555555555555555)

R =

0.55555555555555555555555555555556

>> R*(1-sym(10^(-50)))

ans =

0.55555555555555555555555555555556

32 digits are definitely not enough to store the actual value of 1-10^(-50).

How to fix it

Without stressing with vpa() you can declare both R and R * (1 - 10^(-50)) as symbolics (in fact 0.5555555... = 5/9), and compare them:

>> R = str2sym('5/9');
>> X = str2sym('5/9 * (1 - 10^(-50))');
>> isAlways(R > X)

ans =

  logical

   1

Upvotes: 2

Related Questions