# Is this a symbolic math bug?

1 view (last 30 days)
Igor on 9 Feb 2011
1. Let's a=log(2), I want create with only 5 digits:
>>digits(5);b=sym(a,'d')
b = 0.69315
But what is another way? This way isn't good while - by command digits I effect global changes -- and I am forced to return: digits(32)
2. Perhaps one wants to include periodic ratio Suppose a working example: >> b=sym(0.69696969696969,'r') true result 69/99=23/33, but many signs are needed -- on contrary: >> b=sym(0.6969,'r') b = 6969/10000
How to easy include, for example, 1.23(45) as ratio?
3. >> syms x positive;
>> x=solve('y=-1')
x = -1
But why was quiet reaction? How to clarify the attribute "positive" (in order to machine feel)?
4. And critical and scandal bug:
>> sym(0.6915 - 0.69,'d')
ans = 0.0015000000000000568434188608080149
at this: >>0.6915000-0.69
ans = 0.001500000000000
and also you may see:
>> sym(0.6915 - 0.69,'r')
ans = 211106232533/140737488355328
>> 211106232533/140737488355328
ans = 0.001500000000000

Show 1 older comment
Andrew Newell on 9 Feb 2011
I don't understand question 2.
Doug Hull on 9 Feb 2011
It is a best practice to post ONE question per question. It keeps things orderly.
James Tursa on 9 Feb 2011
This is the decimal representation of the exact value you are using:
>> num2strexact(0.6915 - 0.69)
ans =
1.50000000000005684341886080801486968994140625e-3
The fact that MATLAB only displays 15 significant decimal digits of this does not mean that the underlying number is exactly the displayed result.

Igor on 10 Feb 2011
Thanks. To continue:
Q1 Solution based on your idea (oh.. I am stupid :)- ): >>a=log(2);a=sym(vpa(a,5))
Q2: to clarify -- notice for developer I want such reaction: >>a=sym('1.23(45)','r')
a= 679/550
MATLAB MUST tranform periodic kind of rational numbers in math: 1.234545454545..=1.23(45)=1.23+0.01*45/99=...=679/550
A valid script on this topic from another forum user (but I don't like it)
----
function numrat = forMatigor(numch)
% numch -> char ?????: numch = '1.23(45)'
I = regexp(numch,'[.(]');
J = length(numch)-1;
numrat = rats(eval([numch(1:I(2)-1) repmat( numch( I(2)+1:J),1,1+ceil((20-diff(I)+1)/(J-I(2))))]));
----
Also MATLAB would realize reverse: for example -- '1/3' -> '0.(3)'
Q3: yes, here is different (>>syms x positive):
solve('x=-1') VS solve('y=-1')
if both commands run without assigning to x -- it seems results are equavalent:
ans
-1
I don't understand difference... YES -- I HAVE CLARIFYED solve() uses directly noted variable or create it on flying, and assigning x=.. gives us new var. at same name
Q4: The conclusion become obvious. But no good construction. In general:
Let's x,y real anyway created, needed (x-y) as symbolic ratio. I wish be guaranteed:
sym(0.5-0.8) --> -3/10 %normal
sym(0.6915 - 0.69) --> 211106232533/140737488355328 %wrong
sym(rats(0.5)-rats(0.8)) --> [ 0, 0, 0, 0, 0, 0, -3, 0, -3, 0, 0, 0, 0, 0] %what is it?
sym(0.69315)-sym(0.69) --> 63/20000 % best way
>>a=log(2);a=sym(vpa(a,5));a-sym(0.69)
0.0031471805598357605049386620521545 % again wrong

Igor on 10 Feb 2011
Two new questions --
Q5
How to easy convert symbolic d-form into r-form?
Q6: new bug?
>> digits(32)
>> vpa(pi,100)
ans =
3.141592653589793115997963468544185161590576171875
>> vpa pi 100
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
Andrew Newell on 10 Feb 2011
Igor, can you please make a new question for each of these issues? This sprawling post is hard to deal with.
James Tursa on 10 Feb 2011
In the case of (0.5-0.8) the sym function is trying to "guess" what you meant by the input. Again, the input is not exactly 0.3, it is:
>> num2strexact(0.5-0.8)
ans =
-0.3000000000000000444089209850062616169452667236328125
So in this case it guessed that you really wanted an exact 0.3 result even though the input was *not* exactly 0.3. But in the other case:
>> num2strexact(0.6915 - 0.69)
ans =
1.50000000000005684341886080801486968994140625e-3
the sym function just wasn't clairvoyant enough to guess what you wanted for a result. Don't blame syms for being "wrong" just because it didn't guess what you wanted as well as you had hoped. Reformulate your code properly instead.

Andrew Newell on 9 Feb 2011
I think what you want in question 1 is
b = vpa(log(sym('2')),5);
For question 3, x should be the variable in SOLVE:
syms x positive
x = solve('x=-1',x)
For question 4, try
vpa(sym('0.6915')-sym('0.69'),5)
ans =
0.0015
To get the right answer for the fraction, use:
vpa(sym('211106232533/140737488355328'))
You were calculating it in double precision, which isn't accurate enough.
EDIT: I have incorporated a couple of suggestions from @Walter and another from @James.

Walter Roberson on 9 Feb 2011
Andrew, you are letting things be evaluated by Matlab in double precision before having them pass in to the symbolic engine.
b = vpa(log(sym('2')),5);
vpa(sym('211106232533/140737488355328'))
Andrew Newell on 9 Feb 2011
Good point, @Walter. I prefer doing my symbolic calculations directly in Maple - fewer pitfalls.