Suspicious value in vpa

14 views (last 30 days)
Vaibhav Kumar
Vaibhav Kumar on 28 Jul 2017
Commented: Steven Lord on 28 Jul 2017
How does vpa returns suspicious values like
vpa(1e50, 60) produces 100000000000000000000000000000000000000000000000000.0 which is fine,
while
vpa(1.1e50, 60) produces 110000000000000008392746825201075703624461468041216.0.
Where does "...8392746825201075703624461468041216" come from?

Accepted Answer

Sean de Wolski
Sean de Wolski on 28 Jul 2017
vpa('1.1e50', 60)
ans =
110000000000000000000000000000000000000000000000000.0
When you say 1.1e50 not in single quotes, it converts it to a double (~16 digits) and then to vpa. If the number can't be evenly represented in double it shows up like above.
  3 Comments
John D'Errico
John D'Errico on 28 Jul 2017
Edited: John D'Errico on 28 Jul 2017
With the caveat that vpa(1e50,60) DID work. Note that 1e50 is as much not representable exactly in a numeric form as is 1.1e50.
This is likely due to some magical trickery on the part of vpa.
For example, vpa(1/3) survives perfectly.
vpa(1/3)
ans =
0.33333333333333333333333333333333
>> vpa(1/3,100)
ans =
0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
>> sym(1/3)
ans =
1/3
This is because the symbolic TB intercepts 1/3 as what was intended surely to be EXACTLY interpreted as 1/3, not as MATLAB stores it as in double form, which, when fully written out in decimal is:
sprintf('%0.55f',1/3)
ans =
'0.3333333333333333148296162562473909929394721984863281250'
So I imagine the writers of symbolic toolbox have some special code, designed to NOT fail for simple inputs. Unfortunately, 1.1e50 is too much of a stretch for the magic.
So, this succeeds:
sym(113/301)
ans =
113/301
But this one is too much to handle:
sym(1379/146852)
ans =
5413200892234501/576460752303423488
Steven Lord
Steven Lord on 28 Jul 2017
That "special code" or "magic" is the sym function. vpa calls sym if you pass in a plain double array, and the default conversion technique used by sym, 'r', does the following.
When sym uses the rational mode, it converts floating-point
numbers obtained by evaluating expressions of the form p/q,
p*pi/q, sqrt(p), 2^q, and 10^q for modest sized integers p
and q to the corresponding symbolic form. This effectively
compensates for the round-off error involved in the original
evaluation, but might not represent the floating-point value
precisely. If sym cannot find simple rational approximation,
then it uses the same technique as it would use with the flag 'f'.
The double precision value 1/3 is of the form p/q for modest sized integers p and q (p = 1, q = 3) while 1.1e50 does not match any of the five forms given in that documentation. [If you really want the symbolic expression that matches the exact double precision value, use conversion technique 'f'. That generate N*2^e for integers N and e.]
>> r = sym(1/3, 'r')
r =
1/3
>> f = sym(1/3, 'f')
f =
6004799503160661/18014398509481984

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!